From 270ddc6487a1494afc7e2029ac717640835bacb7 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Fri, 23 May 2025 13:32:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=A3=80=E6=9F=A5=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E5=B7=B2=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/ApproveController.php | 6 +-- app/Http/Controllers/Api/FileController.php | 53 ++++--------------- .../Controllers/Api/ProjectController.php | 14 +---- app/Http/Controllers/Api/SystemController.php | 12 ++--- app/Models/File.php | 38 +++++++++++++ app/Module/Apps.php | 23 ++++++++ 6 files changed, 78 insertions(+), 68 deletions(-) diff --git a/app/Http/Controllers/Api/ApproveController.php b/app/Http/Controllers/Api/ApproveController.php index c7c4479ff..ec1695014 100755 --- a/app/Http/Controllers/Api/ApproveController.php +++ b/app/Http/Controllers/Api/ApproveController.php @@ -35,9 +35,7 @@ class ApproveController extends AbstractController public function __construct() { - if (!Apps::isInstalled('approve')) { - throw new ApiException('应用「Approval」未安装'); - } + Apps::isInstalledThrow('approve'); $this->flow_url = env('FLOW_URL') ?: 'http://approve'; } @@ -77,7 +75,7 @@ class ApproveController extends AbstractController * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ - public function procdef__all() + public function procdef__all() { User::auth(); $data['name'] = Request::input('name'); diff --git a/app/Http/Controllers/Api/FileController.php b/app/Http/Controllers/Api/FileController.php index 2a17c8416..d311491cc 100755 --- a/app/Http/Controllers/Api/FileController.php +++ b/app/Http/Controllers/Api/FileController.php @@ -11,7 +11,6 @@ use App\Models\FileContent; use App\Models\FileLink; use App\Models\FileUser; use App\Models\User; -use App\Module\Apps; use App\Module\Base; use App\Module\Timer; use App\Module\Ihttp; @@ -504,29 +503,16 @@ class FileController extends AbstractController return Base::retError('参数错误'); } // + if ($down == 'no') { + File::isNeedInstallApp($file->type); + } + // if ($only_update_at == 'yes') { return Base::retSuccess('success', [ 'id' => $file->id, 'update_at' => Carbon::parse($file->updated_at)->toDateTimeString() ]); } - // - if ($down != 'yes') { - // office - if (in_array($file->type, ['word', 'excel', 'ppt']) && !Apps::isInstalled('office')) { - return Base::retError('应用「OnlyOffice」未安装'); - } - // drawio - if ($file->type == 'drawio' && !Apps::isInstalled('drawio')) { - return Base::retError('应用「Drawio」未安装'); - } - // mind - if ($file->type == 'mind' && !Apps::isInstalled('minder')) { - return Base::retError('应用「Minder」未安装'); - } - } - // - // $builder = FileContent::whereFid($file->id); if ($history_id > 0) { @@ -595,16 +581,12 @@ class FileController extends AbstractController $contentArray = Base::json2array($content); $contentString = $contentArray['xml']; $file->ext = 'drawio'; - if (!Apps::isInstalled('drawio')) { - return Base::retError('应用「Drawio」未安装'); - } + File::isNeedInstallApp($file->type); break; case 'mind': $contentString = $content; $file->ext = 'mind'; - if (!Apps::isInstalled('minder')) { - return Base::retError('应用「Minder」未安装'); - } + File::isNeedInstallApp($file->type); break; case 'txt': case 'code': @@ -656,9 +638,7 @@ class FileController extends AbstractController { User::auth(); // - if (!Apps::isInstalled('office')) { - return Base::retError('应用「OnlyOffice」未安装'); - } + File::isNeedInstallApp('office'); // $config = Request::input('config'); $token = \Firebase\JWT\JWT::encode($config, env('APP_KEY') ,'HS256'); @@ -685,9 +665,7 @@ class FileController extends AbstractController { $user = User::auth(); // - if (!Apps::isInstalled('office')) { - return Base::retError('应用「OnlyOffice」未安装'); - } + File::isNeedInstallApp('office'); // $id = intval(Request::input('id')); $status = intval(Request::input('status')); @@ -808,19 +786,8 @@ class FileController extends AbstractController $history_id = intval(Request::input('history_id')); // $file = File::permissionFind($id, $user); - // office - if (in_array($file->type, ['word', 'excel', 'ppt']) && !Apps::isInstalled('office')) { - return Base::retError('应用「OnlyOffice」未安装'); - } - // drawio - if ($file->type == 'drawio' && !Apps::isInstalled('drawio')) { - return Base::retError('应用「Drawio」未安装'); - } - // mind - if ($file->type == 'mind' && !Apps::isInstalled('minder')) { - return Base::retError('应用「Minder」未安装'); - } - + // + File::isNeedInstallApp($file->type); // $history = FileContent::whereFid($file->id)->whereId($history_id)->first(); if (empty($history)) { diff --git a/app/Http/Controllers/Api/ProjectController.php b/app/Http/Controllers/Api/ProjectController.php index 4cc89560e..50182bf49 100755 --- a/app/Http/Controllers/Api/ProjectController.php +++ b/app/Http/Controllers/Api/ProjectController.php @@ -41,7 +41,6 @@ use App\Models\ProjectTaskFlowChange; use App\Models\ProjectTaskVisibilityUser; use App\Models\ProjectTaskTemplate; use App\Models\ProjectTag; -use App\Module\Apps; /** * @apiDefine project @@ -1832,18 +1831,7 @@ class ProjectController extends AbstractController 'update_at' => Carbon::parse($file->updated_at)->toDateTimeString() ]); } - // office - if (in_array($file->ext, ['docx', 'xlsx', 'pptx']) && !Apps::isInstalled('office')) { - return Base::retError('应用「OnlyOffice」未安装'); - } - // drawio - if ($file->ext == 'drawio' && !Apps::isInstalled('drawio')) { - return Base::retError('应用「Drawio」未安装'); - } - // mind - if ($file->ext == 'mind' && !Apps::isInstalled('minder')) { - return Base::retError('应用「Minder」未安装'); - } + File::isNeedInstallApp($file->ext); // $data = $file->toArray(); $data['path'] = $file->getRawOriginal('path'); diff --git a/app/Http/Controllers/Api/SystemController.php b/app/Http/Controllers/Api/SystemController.php index e196fb8be..ef9a60bf1 100755 --- a/app/Http/Controllers/Api/SystemController.php +++ b/app/Http/Controllers/Api/SystemController.php @@ -302,9 +302,7 @@ class SystemController extends AbstractController { User::auth('admin'); // - if (!Apps::isInstalled('ai')) { - return Base::retError('应用「AI Robot」未安装'); - } + Apps::isInstalledThrow('ai'); // $type = trim(Request::input('type')); $filter = trim(Request::input('filter')); @@ -469,11 +467,9 @@ class SystemController extends AbstractController } } // 人脸识别 - if (in_array('face', $all['modes'])){ - if (!Apps::isInstalled('face')) { - return Base::retError('应用「Face check-in」未安装'); - } - } + if (in_array('face', $all['modes'])) { + Apps::isInstalledThrow('face'); + } } } if ($all['modes']) { diff --git a/app/Models/File.php b/app/Models/File.php index 1e9e590db..02cf385bd 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -3,6 +3,7 @@ namespace App\Models; use Request; +use App\Module\Apps; use App\Module\Base; use App\Tasks\PushTask; use App\Exceptions\ApiException; @@ -978,4 +979,41 @@ class File extends AbstractModel ]; Task::deliver(new PushTask($params)); } + + /** + * 根据文件类型判断是否需要安装应用 + * @param $type + * @return void + */ + public static function isNeedInstallApp($type): void + { + // 文件类型与应用的映射配置 + $fileTypeAppMapping = [ + // Office 应用映射 + [ + 'types' => ['word', 'excel', 'ppt', 'docx', 'xlsx', 'pptx'], + 'app_id' => 'office', + 'app_name' => 'OnlyOffice' + ], + // Drawio 应用映射 + [ + 'types' => ['drawio'], + 'app_id' => 'drawio', + 'app_name' => 'Drawio' + ], + // Minder 应用映射 + [ + 'types' => ['mind'], + 'app_id' => 'minder', + 'app_name' => 'Minder' + ] + ]; + + // 遍历配置检查是否需要安装应用 + foreach ($fileTypeAppMapping as $config) { + if (in_array($type, $config['types'])) { + Apps::isInstalledThrow($config['app_id']); + } + } + } } diff --git a/app/Module/Apps.php b/app/Module/Apps.php index f88ed175c..ee44e554d 100644 --- a/app/Module/Apps.php +++ b/app/Module/Apps.php @@ -2,6 +2,7 @@ namespace App\Module; +use App\Exceptions\ApiException; use App\Services\RequestContext; use Symfony\Component\Yaml\Yaml; @@ -33,4 +34,26 @@ class Apps return RequestContext::save($key, $installed); } + + /** + * 判断应用是否已安装,如果未安装则抛出异常 + * @param string $appId + * @return void + */ + public static function isInstalledThrow(string $appId): void + { + if (!self::isInstalled($appId)) { + $name = match ($appId) { + 'ai' => 'AI Robot', + 'face' => 'Face check-in', + 'appstore' => 'AppStore', + 'approve' => 'Approval', + 'office' => 'OnlyOffice', + 'drawio' => 'Drawio', + 'minder' => 'Minder', + default => $appId, + }; + throw new ApiException("应用「{$name}」未安装"); + } + } }