Files
37duAutomation/.trae/documents/中心机Vue管理前端与底座剥离.md
T
puddy 3f901d3882 feat: 搭建控制面管理前端并剥离底座管理功能
1. 新增controlplane/frontend Vue3管理前端,实现登录、账号CRUD、插件上传/管理、权限授权
2. 为controlplane/app.py添加CORS中间件并挂载前端静态产物
3. 重构环境变量加载,统一使用app/common/env.load_app_env()
4. 剥离webhost/的管理UI与代理接口,仅保留基础登录能力
5. 新增环境隔离规范文档与架构蓝图说明
2026-09-09 17:10:12 +08:00

60 lines
5.9 KiB
Markdown
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.
# 中心机 Vue 管理前端搭建 + 底座管理 UI 剥离
## Context(为什么做)
优先级已定:**先把中心机(控制面 controlplane)完整搭起来,包括管理前端**,部署机才好用接口、配权限、做插件分发。
当前现状:
- 控制面(`controlplane/`,8800)是纯 FastAPI API,**没有任何前端页面**、未挂载静态目录;目前没有 `/api/admin/password`(改自己密码)接口。
- 管理界面目前是**借底座(`webhost/`)前端**(ManagementView.vue)通过 `webhost` 后端的 `/api/admin/*` 代理展示,普通用户能碰到管理入口——正是要消除的暴露风险。
目标:
- 新建**控制面自己的 Vue 管理前端**(`controlplane/frontend`),挂在控制面 8800。
- **同一轮**把底座 `webhost` 的管理界面/管理代理**一并拆掉**(用户已拍板),普通用户接触不到管理入口。
- 保持**四方向零依赖**:控制面前端为独立 Vite+Vue 工程(从 webhost 组件移植,而非 import),控制面后端仍只依赖 fastapi/标准库。
## 边界(哪些必须保留)
底座用户执行插件仍走统一登录:`webhost` 后端 `/api/runs` 有 `_require_login` + 会话 cookie。因此底座后端的**身份/个人接口必须保留**,只拆**管理操作类**:
- **保留**(webhost 后端):`/api/admin/me`、`/api/admin/login`、`/api/admin/logout`、`/api/admin/password`
- **删除**(webhost 后端管理操作代理):`overview`、`admins`、`accounts`、`account`、`account/update`、`account/reset_pwd`、`account/delete`、`plugin/delete`、`account_plugins`、`pkg`
- 对应 `webhost/frontend`:保留 LoginPage + 顶部账号/登出 + `adminMe/adminLogin/adminLogout/adminPassword`;删除「控制面管理」入口与 ManagementView 渲染、删除 `adminOverview/adminUpload/adminGrant/adminAccounts/adminAccount*/adminPluginDelete`。
## 方案
### A. 控制面后端 `controlplane/app.py`
1. 新增 `CORSMiddleware`(`allow_origins=["*"]`),便于 dev 跨域(与控制面前端同源也就可浏览)。
2. 新增 **`POST /api/admin/password`**:入参 `{account, old_password, new_password}`,校验 `x_admin_user==account`(本人),`_verify_password` 旧密正确后 `_hash_password` 更新入库。复用已有 `_hash_password/_verify_password/_db`。
3. **挂载控制面前端产物**:文件末尾 `app.mount("/", StaticFiles(directory=<dist>, html=True), name="web")`;`dist` 路径兼容 **frozen**:源码取 `APP_DIR/frontend/dist`,exe 取 `sys._MEIPASS/frontend/dist`(与 DB 路径的 frozen 逻辑一致)。mount 放 app 定义之后,避免遮蔽 `/api/*`。
### B. 控制面前端(新工程)`controlplane/frontend/`
Vite + Vue3,独立工程,技术栈/样式**移植**自 `webhost/frontend`(亮色主题、CSS 变量),但不 import 它:
- 脚手架:`package.json`、`vite.config.js`(dev 代理 `/api`→`http://127.0.0.1:8800`,`outDir:'dist'`)、`index.html`、`src/main.js`、`src/style.css`(沿用 webhost 的 CSS 变量与亮色规范)。
- `src/store.js`:`{ current, isAdmin }`(登录态,sessionStorage 持久化)。
- `src/api.js`:**直连控制面**,登录用 `POST /api/admin/auth`;管理请求统一带 `X-Admin-User` header;封装:
`adminAuth/adminMe/adminOverview/adminAccounts/adminAccountCreate/Update/Reset/Delete/adminGrant/adminUpload(FormData → /api/admin/pkg)/adminPluginDelete/adminPassword/me`。
- `src/components/LoginPage.vue`:改调 `adminAuth`,成功后写 store.current/isAdmin。
- `src/components/ManagementView.vue`:**移植裁剪**自 webhost 同名组件——插件上传/列表/删除、账号增改/切管理员/重置密码/删除(禁自删/自撤管理)、按账号勾选授权、修改密码;数据统一经 `adminOverview`。
- `src/App.vue`:登录门 → 顶栏(品牌 + 当前账号 + 管理员徽标 + 退出)+ 管理主体。
- 构建:`cd controlplane/frontend && npm install && npm run build`,产物落在 `controlplane/frontend/dist`,由 A3 挂载。
### C. 底座 `webhost/` 剥离管理
- `backend/main.py`:删除 A 中列出的 10 个管理操作代理;保留身份/个人 4 个;编译校验。
- `frontend/src`:App.vue 移除「控制面管理」入口与 mgmt 视图渲染;删除 `components/ManagementView.vue`;api.js 删除管理操作函数(保留 me/login/logout/password);`npm run build` 重新产出 dist 以同步部署素材。
### D.(收尾/可选)重打中心机 exe
用 PyInstaller 把 `controlplane/frontend/dist` 作为 `datas` 打进 `37du-center.exe`,使中心机 exe 自带管理 UI(打开 8800 即见)。首版可先用源码形态起控制面,exe 重打作为下一步。
## 关键文件
- `controlplane/app.py`(+CORS、+password、+静态挂载)
- `controlplane/frontend/**`(新 Vue 工程)
- `webhost/backend/main.py`(删除管理代理)
- `webhost/frontend/src/App.vue`、`api.js`、`components/ManagementView.vue`(剥离)
- 复用:`controlplane/app.py` 的 `_hash_password/_verify_password/_db`;webhost `ManagementView.vue/LoginPage.vue/api.js/store.js` 的 UI 与交互逻辑(移植)。
## 验证
1. **控制面**:源码起 8800;`GET /health`、`GET /`(返回 SPA index)、`POST /api/admin/password`(改密并校验)、`GET /api/admin/overview`(X-Admin-User=puddy)均通。
2. **控制面前端 dev**:`npm run dev` → 登录 `puddy/123456` → 建号/改部门/切管理员/上传 zip/按账号授权/删除 全链路 CRUD 通过;删当前账号/撤自己管理被拦截。
3. **底座剥离**:`/api/meta` 正常;`/api/admin/overview` 返回 404(已删);`/api/admin/login`+`/api/admin/me` 仍可用(需控制面在);前端 `npm run build` 后访问 `/` 无「控制面管理」入口。
4. **四方向零依赖**:`controlplane/app.py` 仍只 import fastapi/标准库;`controlplane/frontend` 只引用自身;确认无新增跨目录 import。