Files
37duAutomation/desktop/webhost/backend/apis/meta.py
T
puddy 84d3e103ec refactor: 整体迁移控制面至MySQL + 替换UI框架为Element Plus
1. 移除naive-ui依赖,迁移前端至Element Plus
2. 重构后端数据库层,切换为MySQL替代SQLite
3. 调整项目部署架构,停止exe打包改为源码部署
4. 新增设备上报接口与相关数据库模型
5. 优化配置路径与SQL语法,替换为MySQL占位符格式
6. 调整站点名称与文案,统一为「37du 插件管理平台」
7. 新增SQLite到MySQL迁移脚本
2026-09-16 15:58:45 +08:00

105 lines
4.8 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, _cloud_plugin_manifest
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:
# 以「登录账号的授权插件集」作为加载白名单(替代启动时 CONTROLPLANE_ACCOUNT 的固定白名单),
# 保证给员工账号授权插件后登录即可见/可运行;控制面不可达(manifest=None)时保持原白名单。
manifest = _cloud_plugin_manifest(account)
if manifest is not None:
_st.set_allowed({p.get("id") for p in manifest})
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)