From 911d5e0b34f7c183787aea78c7067955196e10bb Mon Sep 17 00:00:00 2001 From: wxy Date: Fri, 20 Mar 2026 17:07:23 +0800 Subject: [PATCH 1/2] feat(app): support searching application list by API Key --- api/app/controllers/app_controller.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/api/app/controllers/app_controller.py b/api/app/controllers/app_controller.py index e9b539df..1eb45b89 100644 --- a/api/app/controllers/app_controller.py +++ b/api/app/controllers/app_controller.py @@ -57,6 +57,7 @@ def list_apps( page: int = 1, pagesize: int = 10, ids: Optional[str] = None, + api_key: Optional[str] = None, db: Session = Depends(get_db), current_user=Depends(get_current_user), ): @@ -65,10 +66,28 @@ def list_apps( - 默认包含本工作空间的应用和分享给本工作空间的应用 - 设置 include_shared=false 可以只查看本工作空间的应用 - 当提供 ids 参数时,按逗号分割获取指定应用,不分页 + - 当提供 api_key 参数时,查找该 API Key 关联的应用 """ + from sqlalchemy import select as sa_select + from app.models.api_key_model import ApiKey + workspace_id = current_user.current_workspace_id service = app_service.AppService(db) + # 通过 API Key 搜索:查出关联的 app id,复用 ids 分支返回 + if api_key: + matched = db.execute( + sa_select(ApiKey.resource_id).where( + ApiKey.workspace_id == workspace_id, + ApiKey.api_key.like(f"%{api_key}%"), + ApiKey.resource_id.isnot(None), + ) + ).scalars().all() + app_ids = [str(rid) for rid in matched] + items_orm = app_service.get_apps_by_ids(db, app_ids, workspace_id) if app_ids else [] + items = [service._convert_to_schema(app, workspace_id) for app in items_orm] + return success(data=items) + # 当 ids 存在且不为 None 时,根据 ids 获取应用 if ids is not None: app_ids = [app_id.strip() for app_id in ids.split(',') if app_id.strip()] From bf5c4628c35541434be0b228141c4dae417e30e7 Mon Sep 17 00:00:00 2001 From: wxy Date: Fri, 20 Mar 2026 18:02:03 +0800 Subject: [PATCH 2/2] fix: use exact match instead of LIKE for api_key lookup, reuse ids branch flow --- api/app/controllers/app_controller.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/api/app/controllers/app_controller.py b/api/app/controllers/app_controller.py index 1eb45b89..3ba9c3a9 100644 --- a/api/app/controllers/app_controller.py +++ b/api/app/controllers/app_controller.py @@ -74,19 +74,16 @@ def list_apps( workspace_id = current_user.current_workspace_id service = app_service.AppService(db) - # 通过 API Key 搜索:查出关联的 app id,复用 ids 分支返回 + # 通过 API Key 搜索:精确匹配,将 resource_id 注入 ids 走统一分页流程 if api_key: - matched = db.execute( + matched_id = db.execute( sa_select(ApiKey.resource_id).where( ApiKey.workspace_id == workspace_id, - ApiKey.api_key.like(f"%{api_key}%"), + ApiKey.api_key == api_key, ApiKey.resource_id.isnot(None), ) - ).scalars().all() - app_ids = [str(rid) for rid in matched] - items_orm = app_service.get_apps_by_ids(db, app_ids, workspace_id) if app_ids else [] - items = [service._convert_to_schema(app, workspace_id) for app in items_orm] - return success(data=items) + ).scalar_one_or_none() + ids = str(matched_id) if matched_id else "" # 当 ids 存在且不为 None 时,根据 ids 获取应用 if ids is not None: