feat: 搭建控制面管理前端并剥离底座管理功能
1. 新增controlplane/frontend Vue3管理前端,实现登录、账号CRUD、插件上传/管理、权限授权 2. 为controlplane/app.py添加CORS中间件并挂载前端静态产物 3. 重构环境变量加载,统一使用app/common/env.load_app_env() 4. 剥离webhost/的管理UI与代理接口,仅保留基础登录能力 5. 新增环境隔离规范文档与架构蓝图说明
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
# 中心机 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。
|
||||
@@ -0,0 +1,195 @@
|
||||
# 中心机 Vue 管理前端搭建与底座管理 UI 剥离实施计划
|
||||
|
||||
## 概要
|
||||
|
||||
按照决策:**管理 UI 放到中心机自身,底座(webhost)不再带管理界面**。本计划完成:
|
||||
|
||||
1. 控制面后端补充 CORS、挂载前端静态产物
|
||||
2. 新建 `controlplane/frontend/` Vue 工程,实现完整管理界面(登录 + 账号CRUD + 插件管理 + 上传)
|
||||
3. 底座 `webhost/` 剥离管理相关代码,删除前端管理入口和界面
|
||||
4. 确保四方向零依赖:控制面前端独立,不与 webhost/frontend 耦合
|
||||
|
||||
---
|
||||
|
||||
## 当前状态分析
|
||||
|
||||
| 项 | 状态 | 说明 |
|
||||
|----|------|------|
|
||||
| `controlplane/app.py` 后端 API | ✅ 已完成 | 账号CRUD、插件上传删除、授权、密码修改全齐 |
|
||||
| `controlplane/app.py` CORS | ❌ 缺失 | 需要添加 CORSMiddleware |
|
||||
| `controlplane/app.py` 挂载前端 | ❌ 缺失 | 需要在末尾挂载 StaticFiles |
|
||||
| `controlplane/frontend/` 工程 | ❌ 不存在 | 需要从零新建 |
|
||||
| 前端功能:登录页 | ❌ 缺失 | 需要移植适配 |
|
||||
| 前端功能:管理台(插件/账号) | ❌ 缺失 | 需要移植并适配控制面直连 |
|
||||
| `webhost/` 管理代码剥离 | ❌ 未做 | 需要删除 ManagementView、移除侧边栏入口 |
|
||||
| PyInstaller 打包 | ⚠️ 已存在 | 需要确保 frontend/dist 打进 exe |
|
||||
|
||||
---
|
||||
|
||||
## 实施步骤
|
||||
|
||||
### A. 控制面后端 `controlplane/app.py`
|
||||
|
||||
1. **新增 `CORSMiddleware`**:
|
||||
- 在 `app = FastAPI(...)` 之后添加:
|
||||
```python
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
app.add_middleware(CORSMiddleware, allow_origins=["*"],
|
||||
allow_methods=["*"], allow_headers=["*"])
|
||||
```
|
||||
- 便于开发跨域,生产同域也不影响。
|
||||
|
||||
2. **验证 `POST /api/admin/password`**:
|
||||
- 检查发现已存在(行 311-332),功能正确:校验本人、旧密码、更新入库。无需修改。
|
||||
|
||||
3. **挂载控制面前端产物**:
|
||||
- 文件末尾(`if __name__ == "__main__":` 之前)添加:
|
||||
```python
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
# 挂载前端:源码模式 => controlplane/frontend/dist;exe模式 => sys._MEIPASS/frontend/dist
|
||||
if getattr(sys, "frozen", False):
|
||||
DIST = Path(sys._MEIPASS) / "frontend" / "dist"
|
||||
else:
|
||||
DIST = APP_DIR / "frontend" / "dist"
|
||||
|
||||
if DIST.exists():
|
||||
app.mount("/", StaticFiles(directory=str(DIST), html=True), name="web")
|
||||
```
|
||||
- APP_DIR 已有定义,直接复用。mount 放 `if __name__` 之前,避免遮蔽 `/api/*`。
|
||||
|
||||
---
|
||||
|
||||
### B. 新建控制面前端工程 `controlplane/frontend/`
|
||||
|
||||
复制 `webhost/frontend` 骨架,精简为纯管理台(不需要场景插件、平台登录这些底座功能):
|
||||
|
||||
#### B.1 基础配置
|
||||
|
||||
| 文件 | 操作 | 说明 |
|
||||
|------|------|------|
|
||||
| `package.json` | 新建 | 与 webhose 保持一致:Vue3 + Vite |
|
||||
| `vite.config.js` | 新建 | proxy `/api` 到 `http://127.0.0.1:8800` |
|
||||
| `index.html` | 新建 | 根入口,`<div id="app"></div>` |
|
||||
| `src/main.js` | 新建 | createApp 挂载 |
|
||||
| `src/style.css` | 复制 | 复用 webhost 的样式变量(亮色主题) |
|
||||
|
||||
#### B.2 源码结构
|
||||
|
||||
```
|
||||
controlplane/frontend/
|
||||
├─ package.json
|
||||
├─ vite.config.js
|
||||
├─ index.html
|
||||
└─ src/
|
||||
├─ main.js
|
||||
├─ style.css
|
||||
├─ api.js # API 封装:直连控制面 /api/*,不代理
|
||||
├─ store.js # 状态:current/isAdmin等
|
||||
├─ App.vue # 主布局:登录页 + 管理台
|
||||
└─ components/
|
||||
├─ LoginPage.vue # 登录页
|
||||
└─ ManagementView.vue # 管理主页(插件/账号/上传)
|
||||
```
|
||||
|
||||
#### B.3 各文件内容要点
|
||||
|
||||
- **`api.js`**:全部 API 直连 `'/api/xxx'`,去掉底座代理相关。包含:
|
||||
- `adminMe`, `adminLogin`, `adminLogout`, `adminPassword`
|
||||
- `adminOverview`, `adminAccounts`, `adminAccountCreate`, `adminAccountUpdate`, `adminAccountReset`, `adminAccountDelete`
|
||||
- `adminGrant`, `adminPluginDelete`, `adminUpload`
|
||||
|
||||
- **`store.js`**:只保留管理需要的状态(去掉 plugin/platform/log 相关):
|
||||
```javascript
|
||||
import { reactive } from 'vue'
|
||||
export const store = reactive({
|
||||
appName: '37du 内网插件控制面',
|
||||
isAdmin: false,
|
||||
current: (() => {
|
||||
try { return localStorage.getItem('cp_account') || '' } catch (_) { return '' }
|
||||
})(),
|
||||
})
|
||||
```
|
||||
|
||||
- **`App.vue`**:
|
||||
- 未登录 → 显示 `LoginPage`
|
||||
- 已登录 → 顶部品牌 + 当前账号/登出 + 主体 `ManagementView`
|
||||
- 精简布局(不需要左场景栏、右日志区),纯管理台。
|
||||
|
||||
- **`LoginPage.vue`**:复用 webhost 逻辑,认证成功跳管理台。
|
||||
|
||||
- **`ManagementView.vue`**:
|
||||
- 复用 webhost 的完整功能:上传插件、插件列表、账号管理、授权、修改密码
|
||||
- API 直连控制面,不需要底座代理。
|
||||
|
||||
---
|
||||
|
||||
### C. 底座 `webhost/` 剥离管理 UI
|
||||
|
||||
按照决策"本轮一并拆底座管理UI":
|
||||
|
||||
1. **`webhost/frontend/src/App.vue`**:
|
||||
- 删除 `<ManagementView v-else-if="store.view === 'admin'" />`
|
||||
- 删除侧边栏中"控制面管理"入口(`admin-entry` li)
|
||||
- 删除 `import ManagementView from './components/ManagementView.vue'`
|
||||
|
||||
2. **删除文件**:
|
||||
- `webhost/frontend/src/components/ManagementView.vue`
|
||||
|
||||
3. **`webhost/frontend/src/api.js`**:
|
||||
- 删除管理操作 API:`adminOverview`, `adminGrant`, `adminAccounts`, `adminAccountCreate`, `adminAccountUpdate`, `adminAccountReset`, `adminAccountDelete`, `adminPluginDelete`, `adminUpload`。
|
||||
- **保留** 登录/会话 API:`adminMe`、`adminLogin`、`adminLogout`、`adminPassword`(App.vue 的 checkSession/logout 与统一登录仍需要)。
|
||||
- 只保留业务插件执行相关 API 再加上述认证 API。
|
||||
|
||||
4. **`webhost/backend/main.py`**:
|
||||
- 删除管理操作代理端点:`admin_overview`、`admin_accounts`、`admin_account_create`、`admin_account_update`、`admin_account_reset`、`admin_account_delete`、`admin_plugin_delete`、`admin_grant`、`admin_upload`。
|
||||
- **保留** 认证相关:`admin_me`(App.vue 的 checkSession 判断 is_admin)、`admin_login`、`admin_logout`、`admin_password`(底座用户改自己密码)。
|
||||
|
||||
---
|
||||
|
||||
### D. 构建与打包
|
||||
|
||||
1. **安装前端依赖**:
|
||||
```bash
|
||||
cd controlplane/frontend
|
||||
npm install
|
||||
```
|
||||
|
||||
2. **构建生产产物**:
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
- 输出到 `controlplane/frontend/dist/`
|
||||
|
||||
3. **重新打包 exe**(如果需要):
|
||||
- PyInstaller 需要把 `controlplane/frontend/dist` 整个打包进 exe
|
||||
- 确保 `center_run.py` .spec 中包含该数据目录
|
||||
|
||||
---
|
||||
|
||||
## 验收标准
|
||||
|
||||
1. 启动控制面 `python controlplane/app.py` 后:
|
||||
- 访问 `http://127.0.0.1:8800` 能看到登录页
|
||||
- 用 `puddy / 123456` 能登录
|
||||
- 登录后能看到管理台,显示插件列表、账号列表
|
||||
- 能新建账号、改部门、切换管理员、重置密码、删除账号
|
||||
- 能给账号授权插件
|
||||
- 能上传新插件包
|
||||
- 能删除插件
|
||||
|
||||
2. 底座访问验证(配置 `CONTROLPLANE_URL=http://172.30.28.131:8800`):
|
||||
- 底座登录页正常跳转(经由底座后端代理到控制面认证)
|
||||
- 底座侧没有管理入口,符合安全预期
|
||||
|
||||
3. 四方向零依赖验证:
|
||||
- `controlplane/` 不 `import` `webhost/` 任何模块
|
||||
- `controlplane/frontend/` 独立构建,不引用 `webhost/frontend` 源码
|
||||
|
||||
---
|
||||
|
||||
## 风险与注意事项
|
||||
|
||||
- 路径兼容性:exe 模式下前端 dist 路径必须是 `sys._MEIPASS/frontend/dist`,确保打包正确
|
||||
- CORS:开发时需要,生产同域不需要但开着也不影响
|
||||
- 样式:复用 webhost 的 `style.css`,保持亮色主题一致
|
||||
Reference in New Issue
Block a user