from fastapi import APIRouter, Depends, HTTPException from app.core.security import require_permission from app.repositories import app_channel_repository from app.schemas.app_channel import AppCreateRequest, ChannelSaveRequest from app.services.common_service import validate_channel_code router = APIRouter(tags=["admin-app-channel"]) @router.get("/admin/app/list") def admin_get_app_list(auth=Depends(require_permission("app:view"))): return {"list": app_channel_repository.list_apps()} @router.post("/admin/app/add") def admin_add_app(body: AppCreateRequest, auth=Depends(require_permission("app:manage"))): aid = body.app_id aname = body.app_name try: app_channel_repository.add_app_with_default_channels(aid, aname) except Exception as e: return {"msg": f"创建失败:{str(e)}"} return {"msg": "应用创建成功"} @router.get("/admin/channel/list") def admin_channel_list(app_id: str, include_disabled: bool = True, auth=Depends(require_permission("app:view"))): return {"list": app_channel_repository.list_channels(app_id, include_disabled)} @router.post("/admin/channel/save") def admin_channel_save(body: ChannelSaveRequest, auth=Depends(require_permission("channel:manage"))): app_id = body.app_id.strip() code = body.channel_code.strip() name = body.display_name.strip() enabled = bool(body.enabled) order = int(body.sort_order or 100) if not app_id or not validate_channel_code(code) or not name or len(name) > 64: raise HTTPException(status_code=400, detail="渠道参数无效;代码仅允许字母、数字、下划线和连字符") if not app_channel_repository.app_exists(app_id): raise HTTPException(status_code=404, detail="应用不存在") app_channel_repository.save_channel(app_id, code, name, enabled, order) return {"msg": "渠道已保存"}