// +---------------------------------------------------------------------- declare (strict_types = 1); namespace ywxapp\controller; use think\facade\Config; use think\facade\Request; use ywxapp\exceptions\UploadException; use ywxapp\service\FileStorageService; /** * UploadController 类 * * @author ywxapp */ class UploadController extends BaseController { /** * 不需要登录验证的方法 * @var array */ protected $noNeedLogin = ['*']; /** * 不需要权限验证的方法 * @var array */ protected $noNeedVerify = ['*']; /** * 控制器初始化 initialize * @return void */ protected function initialize() {} /** * 单文件上传 * @return \think\response\Json */ public function upload() { try { // 获取上传的文件 $file = Request::file('file'); if (! $file || ! $file->isValid()) { throw UploadException::fileNotExists(); } // 获取存储驱动(可从前端指定) $driver = Request::param('driver', Config::get('upload.default')); $path = Request::param('type', ''); $filename = Request::param('filename', ''); $storage = new FileStorageService(); $result = $storage->upload($file, $path . '/' . date('Ymd')); $this->result->success($result); } catch (\Exception $e) { $this->result->error($e->getMessage(), 500); } } /** * 多文件上传 * @return \think\response\Json */ // public function batchUpload() // { // try { // $files = Request::file('files'); // if (empty($files)) { // throw UploadException::fileNotExists(); // } // $driver = Request::param('driver', Config::get('upload.default')); // $path = Request::param('path', ''); // $uploadService = new UploadService($driver); // $results = $uploadService->batchUpload($files, $path); // // 检查是否有失败的上传 // $hasError = collect($results)->some(function ($item) { // return isset($item['error']); // }); // if ($hasError) { // return json($this->result->error('部分文件上传失败', 200, $results)); // } // return json($this->result->success($results)); // } catch (UploadException $e) { // return json($this->result->error($e->getMessage(), 400)); // } catch (\Exception $e) { // return json($this->result->error($e->getMessage(), 500)); // } // } /** * Base64上传 * @return \think\response\Json */ // public function base64Upload() // { // try { // $base64 = Request::param('base64', ''); // if (empty($base64)) { // throw UploadException::fileNotExists(); // } // // 解析base64 // if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $matches)) { // $ext = $matches[2]; // $data = base64_decode(str_replace($matches[1], '', $base64)); // // 创建临时文件 // $tempFile = tmpfile(); // fwrite($tempFile, $data); // $meta = stream_get_meta_data($tempFile); // $tempPath = $meta['uri']; // // 创建UploadedFile对象 // $file = new \think\file\UploadedFile( // $tempPath, // 'base64_image.' . $ext, // mime_content_type($tempPath), // filesize($tempPath), // UPLOAD_ERR_OK // ); // $driver = Request::param('driver', Config::get('upload.default')); // $path = Request::param('path', ''); // $uploadService = new UploadService($driver); // $result = $uploadService->upload($file, $path); // fclose($tempFile); // return json($this->result->success($result)); // } // throw new \Exception('无效的Base64数据'); // } catch (UploadException $e) { // return json($this->result->error($e->getMessage(), 400)); // } catch (\Exception $e) { // return json($this->result->error($e->getMessage(), 500)); // } // } }