From 7be1171004452a40f9aedc03d4f25db0c25d902b Mon Sep 17 00:00:00 2001 From: kuaifan Date: Thu, 15 May 2025 15:32:52 +0800 Subject: [PATCH] no message --- app/Http/Controllers/Api/AppsController.php | 25 ++++- app/Module/Apps/Apps.php | 115 ++++++++++++++++---- 2 files changed, 114 insertions(+), 26 deletions(-) diff --git a/app/Http/Controllers/Api/AppsController.php b/app/Http/Controllers/Api/AppsController.php index daa65e08d..7bf05039a 100755 --- a/app/Http/Controllers/Api/AppsController.php +++ b/app/Http/Controllers/Api/AppsController.php @@ -79,6 +79,28 @@ class AppsController extends AbstractController return Apps::appInfo($appName); } + /** + * @api {get} api/apps/entry 04. 获取应用入口点(限管理员) + * + * @apiVersion 1.0.0 + * @apiGroup apps + * @apiName entry + * + * @apiParam {String} [app_name] 应用名称 + * - 不指定则获取所有已安装的应用入口点 + * + * @apiSuccess {Number} ret 返回状态码(1正确、0错误) + * @apiSuccess {String} msg 返回信息(错误描述) + * @apiSuccess {Object} data 应用入口点信息 + */ + public function entry() + { + User::auth(); + // + $appName = Request::input('app_name'); + return Apps::getAppEntryPoints($appName); + } + /** * @api {post} api/apps/install 04. 安装应用(限管理员) * @@ -158,9 +180,6 @@ class AppsController extends AbstractController User::auth('admin'); // $url = Request::input('url'); - if (empty($url)) { - return Base::retError('应用url不能为空'); - } // 下载应用 $res = Apps::downloadApp($url); diff --git a/app/Module/Apps/Apps.php b/app/Module/Apps/Apps.php index 35b3fe2dd..a8f85b670 100644 --- a/app/Module/Apps/Apps.php +++ b/app/Module/Apps/Apps.php @@ -342,10 +342,24 @@ class Apps /** * 获取应用的入口点配置 * + * @param string|null $appName 应用名称,如果为null则获取所有已安装应用的入口点 + * @return array + */ + public static function getAppEntryPoints(?string $appName = null): array + { + if ($appName !== null) { + return self::getSingleAppEntryPoints($appName); + } + return self::getAllInstalledAppsEntryPoints(); + } + + /** + * 获取单个应用的入口点配置 + * * @param string $appName 应用名称 * @return array */ - public static function getAppEntryPoints(string $appName): array + private static function getSingleAppEntryPoints(string $appName): array { $baseDir = base_path('docker/appstore/apps/' . $appName); $entryPoints = []; @@ -354,36 +368,91 @@ class Apps return Base::retSuccess("success", $entryPoints); } - $configData = Yaml::parseFile($baseDir . '/config.yml'); - if (isset($configData['entry_points']) && is_array($configData['entry_points'])) { - foreach ($configData['entry_points'] as $entry) { - // 检查必需的字段 - if (!isset($entry['location']) || !isset($entry['url'])) { - continue; - } - - // 标准化入口点结构 - $normalizedEntry = [ - 'location' => $entry['location'], - 'url' => $entry['url'], - 'icon' => self::processAppIcon($appName, [$entry['icon']]), - 'label' => self::getMultiLanguageField($entry['label']), - ]; - - // 处理可选的UI配置 - foreach (['transparent', 'keepAlive'] as $option) { - if (isset($entry[$option])) { - $normalizedEntry[$option] = $entry[$option]; + try { + $configData = Yaml::parseFile($baseDir . '/config.yml'); + if (isset($configData['entry_points']) && is_array($configData['entry_points'])) { + foreach ($configData['entry_points'] as $entry) { + $normalizedEntry = self::normalizeEntryPoint($entry, $appName); + if ($normalizedEntry) { + $entryPoints[] = $normalizedEntry; } } - - $entryPoints[] = $normalizedEntry; } + } catch (ParseException $e) { + return Base::retError('配置文件解析失败:' . $e->getMessage()); } return Base::retSuccess("success", $entryPoints); } + /** + * 获取所有已安装应用的入口点配置 + * + * @return array + */ + private static function getAllInstalledAppsEntryPoints(): array + { + $allEntryPoints = []; + $baseDir = base_path('docker/appstore/apps'); + + if (!is_dir($baseDir)) { + return Base::retSuccess("success", $allEntryPoints); + } + + $dirs = scandir($baseDir); + foreach ($dirs as $dir) { + if ($dir === '.' || $dir === '..' || str_starts_with($dir, '.')) { + continue; + } + + if (!self::isInstalled($dir)) { + continue; + } + + $appEntryPoints = self::getSingleAppEntryPoints($dir); + if (Base::isSuccess($appEntryPoints)) { + $allEntryPoints = array_merge($allEntryPoints, $appEntryPoints['data']); + } + } + + return Base::retSuccess("success", $allEntryPoints); + } + + /** + * 标准化入口点配置 + * + * @param array $entry 入口点配置 + * @param string $appName 应用名称 + * @return array|null 标准化后的入口点配置,如果配置无效则返回null + */ + private static function normalizeEntryPoint(array $entry, string $appName): ?array + { + // 检查必需的字段 + if (!isset($entry['location']) || !isset($entry['url'])) { + return null; + } + + // 基础配置 + $normalizedEntry = [ + 'app_name' => $appName, + 'location' => $entry['location'], + 'url' => $entry['url'], + 'key' => $entry['key'] ?? substr(md5($entry['url']), 0, 16), + 'icon' => self::processAppIcon($appName, [$entry['icon'] ?? '']), + 'label' => self::getMultiLanguageField($entry['label'] ?? ''), + ]; + + // 处理可选的UI配置 + $optionalConfigs = ['transparent', 'keepAlive']; + foreach ($optionalConfigs as $config) { + if (isset($entry[$config])) { + $normalizedEntry[$config] = $entry[$config]; + } + } + + return $normalizedEntry; + } + /** * 获取应用配置信息 *