Files
37duAutomation/desktop/webhost/backend/apis/meta.py
T
puddy 1e533dd1b3 refactor: 完成项目目录重构与代码迁移
本次提交完成了整体项目的目录梳理与代码迁移:
1.  将根级controlplane、webhost等目录的代码迁移至desktop/对应子目录,统一桌面端架构
2.  移动并整理了插件目录结构,将插件统一放入根级plugins/
3.  调整了前端项目的路径与配置,统一前后端开发构建流程
4.  清理了废弃的旧版app/目录与冗余的构建中间文件
5.  更新了.gitignore与README.md,适配新的项目结构
2026-09-14 17:46:09 +08:00

100 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""webhost 宿主「业务元数据与平台登录」路由:/api/meta 聚合 + 平台真实登录/登出。"""
from __future__ import annotations
from fastapi import APIRouter, HTTPException, Request
from webhost.backend import auth as platform_auth
from webhost.backend import config
from webhost.backend import state as _st
from webhost.backend import meta as _meta
from webhost.backend.plugins import PLUGINS, load_plugins
from webhost.backend.core.plugin_sync import sync_plugins
from webhost.backend.core import shell_updater as _shell_updater
from webhost.backend.core.session import _require_login, _sess_account
router = APIRouter()
@router.get("/api/meta")
def meta(request: Request) -> dict:
account = _sess_account(request)
# 插件清单仅对已登录账号可见(未登录不泄露插件元数据);
# 已登录且能按云端权限(authorization)解析时,仅返回该员工被授权插件(§2.2 权限隔离)。
if account:
allowed = _meta._cloud_plugin_ids(account)
plugins = ([_meta._plugin_schema(p) for p in PLUGINS if p.id in allowed]
if allowed is not None # 云端按员工返回了授权集(也可能是空集=无权限)
else [_meta._plugin_schema(p) for p in PLUGINS]) # 无 token/云端不可达 → 离线兜底全量
else:
plugins = []
allowed_plats = _meta._account_allowed_platforms(account) if account else None
platforms = [{"id": pid, "label": m["label"],
"online": platform_auth.is_online(pid),
# 已登录时按账号白名单;未登录/中心机不可达默认展示
"allowed": (pid in allowed_plats) if allowed_plats is not None else True}
for pid, m in _st.PLATFORMS.items()]
return {
# controlplane_url 供前端管理员跳转控制面;未配置时为空(前端隐藏管理入口)
"controlplane": config.CONTROLPLANE_URL,
"appName": "37du 自动化工作台",
"dingtalkScan": bool(config.DINGTALK_APP_KEY),
"platforms": platforms,
"plugins": plugins,
}
@router.post("/api/plugins/sync")
def plugins_sync(request: Request) -> dict:
"""按当前登录账号权限自下载/更新插件,成功后重载注册表。
离线(控制面不可达)/未配置时返回空统计,前端直接沿用本地插件进入主界面。
"""
account = _require_login(request)
result = sync_plugins(account)
try:
load_plugins(_st.ALLOWED, reload=True)
except Exception as _exc: # noqa: BLE001 重载失败不阻断返回
import logging
logging.getLogger(__name__).warning("插件同步后重载失败:%s", _exc)
return result
@router.get("/api/shell/update-status")
def shell_update_status(request: Request) -> dict:
"""壳子更新状态:前端据此显示"发现新版本,点击重启更新"提示。"""
_require_login(request)
return _shell_updater.pending_status()
@router.post("/api/shell/apply")
def shell_apply(request: Request) -> dict:
"""应用壳子更新:运行新版安装包静默安装并退出进程(升级完成后自动重启新版本)。"""
_require_login(request)
_shell_updater.apply_update()
return {"ok": True} # 实际不会走到这里(apply 内 os._exit)
@router.post("/api/platforms/{pid}/login")
def platform_login(pid: str, request: Request) -> dict:
"""真实登录:需先通过统一账户登录(有会话),且该账号已获中心机平台授权。"""
account = _require_login(request)
plat = _st.PLATFORMS.get(pid)
if not plat:
raise HTTPException(404, "平台不存在")
allowed = _meta._account_allowed_platforms(account)
if allowed is None:
raise HTTPException(503, "控制面不可达,无法校验平台权限")
if pid not in allowed:
raise HTTPException(403, "无该平台访问权限,请联系管理员在控制面授权")
return platform_auth.login(pid)
@router.post("/api/platforms/{pid}/logout")
def platform_logout(pid: str, request: Request) -> dict:
"""登出平台:清除共享 cookies,使该平台登录态失效(需已登录统一账户)。"""
_require_login(request)
plat = _st.PLATFORMS.get(pid)
if not plat:
raise HTTPException(404, "平台不存在")
return platform_auth.logout(pid)