[ // 'class' => AccessControl::class, // 'rules' => [ // [ // 'allow' => true, // 'roles' => ['@'],// 登录 // ], // ], // ], // ]; // } public function actionUpload() { try { //上传权限验证 todo $post = Yii::$app->request->post(); //上传类型 $type = $post['upload_type'] ?? ''; if(!$type || !isset(Attachment::$uploadTypeExplain[$type])){ throw new Exception('上传类型有误'); } $mall_id = $post['mall_id'] ?? 5; $config = AttachmentStorageConfig::getConfig($mall_id); $upload = new UploadHelper($config, $type); $upload->verifyFile(); $upload->save(); return ResultHelper::json(200, '上传成功', $upload->getBaseInfo($mall_id)); } catch (\Exception $e) { return ResultHelper::json(404, $e->getMessage()); } } /** * Markdown 图片上传 * * @return array * @throws \yii\web\NotFoundHttpException */ public function actionImagesMarkdown() { try { $upload = new UploadHelper(Yii::$app->request->get(), Attachment::UPLOAD_TYPE_IMAGES); $upload->uploadFileName = 'editormd-image-file'; $upload->verifyFile(); $upload->save(); $info = $upload->getBaseInfo(); Yii::$app->response->format = Response::FORMAT_JSON; return [ 'success' => 1, 'url' => $info['url'], ]; } catch (\Exception $e) { return ResultHelper::json(404, $e->getMessage()); } } /** * base64编码的上传 * * @return array */ public function actionBase64() { try { // 保存扩展名称 $extend = Yii::$app->request->post('extend', 'jpg'); !in_array($extend, Yii::$app->params['uploadConfig']['images']['extensions']) && $extend = 'jpg'; $data = Yii::$app->request->post('image', ''); $upload = new UploadHelper(Yii::$app->request->post(), Attachment::UPLOAD_TYPE_IMAGES); $upload->verifyBase64($data, $extend); $upload->save(base64_decode($data)); return ResultHelper::json(200, '上传成功', $upload->getBaseInfo()); } catch (\Exception $e) { return ResultHelper::json(404, $e->getMessage()); } } /** * 合并 * * @return array */ public function actionMerge() { $guid = Yii::$app->request->post('guid'); $mergeInfo = Yii::$app->cache->get(UploadHelper::PREFIX_MERGE_CACHE . $guid); if (!$mergeInfo) { return ResultHelper::json(404, '找不到文件信息, 合并文件失败'); } try { $upload = new UploadHelper($mergeInfo['config'], $mergeInfo['type'], true); $upload->setPaths($mergeInfo['paths']); $upload->setBaseInfo($mergeInfo['baseInfo']); $upload->merge(); Yii::$app->cache->delete('upload-file-guid:' . $guid); return ResultHelper::json(200, '合并完成', $upload->getBaseInfo()); } catch (\Exception $e) { return ResultHelper::json(404, $e->getMessage()); } } /** * oss直传配置 * * @return array * @throws \Exception */ public function actionOssAccredit() { // 上传类型 $type = Yii::$app->request->get('type'); $typeConfig = Yii::$app->params['uploadConfig'][$type]; $path = $typeConfig['path'] . date($typeConfig['subName'], time()) . "/"; $oss = Yii::$app->uploadDrive->oss()->config($typeConfig['maxSize'], $path, 60 * 60 * 2, $type); return ResultHelper::json(200, '获取成功', $oss); } /** * 根据md5获取文件 * * @return array */ public function actionVerifyMd5() { $md5 = Yii::$app->request->post('md5'); $drive = Yii::$app->request->post('drive'); if ($file = Yii::$app->services->attachment->findByMd5($md5, $drive)) { $file['formatter_size'] = Yii::$app->formatter->asShortSize($file['size'], 2); $file['url'] = $file['base_url']; $file['upload_type'] = UploadHelper::formattingFileType($file['specific_type'], $file['extension'], $file['upload_type']); return ResultHelper::json(200, '获取成功', $file); } return ResultHelper::json(404, '找不到文件'); } /** * 资源选择器 * * @return array|string */ public function actionSelector() { $upload_type = Yii::$app->request->get('upload_type', Attachment::UPLOAD_TYPE_IMAGES); $keyword = Yii::$app->request->get('keyword', ''); $drive = Yii::$app->request->get('drive', ''); $mall_id = 1; $result = Yii::$app->services->attachment->getListPage($mall_id,$upload_type, $drive, $keyword); // 判断是否直接返回json格式 return $this->success('ok',$result); } }