Files
37duAutomation/desktop/webhost/backend/apis/meta.py
T
puddy b32f8e8fe2 feat: 新增xq0031_xq0032 ERP报表导出插件,完成功能与基建更新
1. 新增xq0031_xq0032插件:包含前后端源码、打包脚本、配置文件与插件元数据
2. 新增内网文件服务器组件:exe、启停脚本、打包脚本与配置
3. 新增敏感凭证RSA加密下发能力与存储结构
4. 新增大文件源管理表与相关API
5. 优化登录流程与会话管理,新增Chrome自动化安装工具链
6. 清理废弃插件与示例代码
7. 更新版本号至1.1.3,完善安装包与.gitignore配置
2026-09-17 12:42:44 +08:00

172 lines
7.5 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
from webhost.backend.core import licensing
from webhost.backend.core import chrome_manager as _chrome
router = APIRouter()
@router.get("/api/meta")
def meta(request: Request) -> dict:
account = _sess_account(request)
# 插件可见性 = 本地授权票据快照(统一真源,与运行门禁一致):仅展示票据内被授权插件;
# 票据不可用(缺失/过期且刷新失败)→ 空列表 + authExpired,前端提示联网刷新(超时不能使用)。
auth_expired = False
if account:
ok, lic = licensing.ensure_license(account)
if ok:
allowed = {p.get("pluginId") for p in (lic or {}).get("plugins", [])}
plugins = [_meta._plugin_schema(p) for p in PLUGINS if p.id in allowed]
else:
plugins = []
auth_expired = True
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 {
"appName": "37du 自动化工作台",
# 钉钉扫码 URL 由控制面代理构造,客户端零钉钉凭证;控制面配置了即可用扫码入口
"dingtalkScan": bool(config.CONTROLPLANE_URL),
"authExpired": auth_expired,
"platforms": platforms,
"plugins": plugins,
}
@router.post("/api/plugins/sync")
def plugins_sync(request: Request) -> dict:
"""按当前登录账号权限自下载/更新插件,成功后重载注册表。
离线(控制面不可达)/未配置时返回空统计,前端直接沿用本地插件进入主界面。
全程持 SYNC_LOCK:与启动后台同步串行,避免旧账号白名单覆盖本次同步结果。
"""
account = _require_login(request)
try:
# 以「登录账号的授权插件集」作为加载白名单(替代启动时 CONTROLPLANE_ACCOUNT 的固定白名单),
# 保证给员工账号授权插件后登录即可见/可运行;控制面不可达(manifest=None)时保持原白名单。
from webhost.backend.core.plugin_sync import SYNC_LOCK
with SYNC_LOCK:
result = sync_plugins(account)
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)
# 授权票据一并刷新:管理台新授的插件在「F5 同步」后立即可运行,
# 否则本地许可票据停留在上次登录时的快照,点执行会被 403 拒绝(须重登才恢复)。
try:
from webhost.backend.core.cpapi import _sync_licensing
_sync_licensing(account)
except Exception as _exc: # noqa: BLE001 票据刷新失败不阻断同步结果
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/check")
def shell_check(request: Request) -> dict:
"""手动检查壳子更新:只比对版本(秒级),不下载;有新版由前端提示用户确认下载。"""
_require_login(request)
_shell_updater.start_check()
return {"ok": True, "checking": True}
@router.post("/api/shell/download")
def shell_download(request: Request) -> dict:
"""开始下载新版安装包(后台流式下载,进度经 update-status 的 dl 字段轮询)。"""
_require_login(request)
_shell_updater.start_download()
return {"ok": True}
@router.post("/api/shell/apply")
def shell_apply(request: Request) -> dict:
"""应用壳子更新:运行新版安装包静默安装并退出进程(升级完成后自动重启新版本)。"""
_require_login(request)
_shell_updater.apply_update()
return {"ok": True} # 实际不会走到这里(apply 内 os._exit)
# ===================== 浏览器(Chrome)检测/下载/安装 =====================
@router.get("/api/chrome/status")
def chrome_status(request: Request) -> dict:
"""浏览器状态:是否已装、版本、需否下载、下载进度。前端据此显示"安装 Chrome"图标提示。"""
_require_login(request)
return _chrome.status()
@router.post("/api/chrome/check")
def chrome_check(request: Request) -> dict:
"""手动检测浏览器:刷新本机安装状态 + 中心机有无登记内网安装包。"""
_require_login(request)
_chrome.refresh()
return {"ok": True}
@router.post("/api/chrome/download")
def chrome_download(request: Request) -> dict:
"""开始后台下载 Chrome 安装包(进度经 /api/chrome/status 的 dl 字段轮询)。"""
_require_login(request)
_chrome.start_download()
return {"ok": True}
@router.post("/api/chrome/install")
def chrome_install(request: Request) -> dict:
"""静默安装已下载的 Chrome 安装包(后台进行,前端轮询 status.installed 感知成功)。"""
_require_login(request)
return _chrome.install()
@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, "平台不存在")
ok, _lic = licensing.ensure_license(account)
if not ok:
raise HTTPException(403, "授权已过期(最长离线 7 天),请联网刷新后重试")
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)