Files
gofly-platform-services/zlt-web/react-web-next/README.md
T
2025-09-23 16:12:43 +08:00

207 lines
8.2 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.
# react-web-next(重构版)
本项目是在不改动现有 `zlt-web/react-web` 的前提下,基于其功能与接口重新组织的“重构版”管理后台前端代码。目标:
- 保持所有管理页面功能等价(逐步迁移),显著提升结构与可读性;
- 为“每个页面”统一增加“租户 + 包含子租户”查询控制;
- 优化主题与布局,使界面更现代、更一致;
- 通过组件化与约定,降低后续页面维护成本。
## 目录结构
## 构建与部署(多环境)
- 前置要求:Node.js 16+、NPM 7+(或 Yarn/Pnpm)
- 可访问目标环境网关(如 `http://119.45.169.13:9900`)
### 环境变量约定
- `REACT_APP_ENV=dev|test|pre`:仅用于本地开发选择 `config/proxy.ts` 的代理分支。
- `GATEWAY_ORIGIN`:仅用于本地开发代理目标(统一转发 `/api-*`)。
- `API_PREFIX`:构建时注入到 Umi 全局 `request.prefix`,用于 `dist` 包直连网关。
- 测试/预发:设置为网关完整地址(如 `http://119.45.169.13:9900`)。
- 生产:推荐留空,采用同域反向代理 `/api-*`。
### 本地开发(走代理)
- 安装依赖:`npm install`
- 启动:`npm run start` | `npm run start:test` | `npm run start:pre`
- 临时覆盖代理网关:
- PowerShell:`$env:GATEWAY_ORIGIN="http://10.x.x.x:9900"; npm run start:test`
- CMD:`set GATEWAY_ORIGIN=http://10.x.x.x:9900 && npm run start:test`
说明:开发环境不注入 `API_PREFIX`,所有 `/api-*` 由 `config/proxy.ts` + `GATEWAY_ORIGIN` 转发。
### 多环境构建(输出 dist)
- 基础构建:`npm run build`
- 测试包:`npm run build:test`(默认 `API_PREFIX=http://119.45.169.13:9900`)
- 预发包:`npm run build:pre`
- 生产包:`npm run build:prod`(不注入前缀,推荐同域反代)
覆盖前缀示例:
```bash
API_PREFIX=http://10.x.x.x:9900 npm run build:test
$env:API_PREFIX="http://10.x.x.x:9900"; npm run build:test
```
### 部署方式
1) 跨域直连网关(需后端 CORS 放行)
- 使用 `build:test`/`build:pre` 注入网关地址(如 `http://119.45.169.13:9900`)。
- 将 `dist/` 上传到任意静态服务器(Nginx/OSS 等)。
- 确保后端 CORS 允许当前前端域名。
2) 同域反向代理(推荐生产)
- 使用 `build:prod`(`API_PREFIX` 为空)。
- 由 Nginx 同域托管静态资源并将 `/api-*` 反代到网关。
Nginx 示例:
```nginx
server {
listen 80;
server_name your.domain.com;
root /opt/apps/react-web-next/dist;
index index.html;
location / { try_files $uri $uri/ /index.html; }
location /api- { proxy_pass http://127.0.0.1:9900; }
}
```
### 部署总结(测试环境)
- 构建:`npm run build:test:mgnt`
- 等价:`cross-env APP_BASE=/mgnt REACT_APP_ENV=test API_PREFIX=http://119.45.169.13:9900 npm run build`
- 部署:将 `dist/` 上传至服务器,按上方示例以 `/mgnt/` 路径托管并开启 history 回退。
- 访问:`http://<服务器IP或域名>:9999/mgnt/`
- 验证:
- F12 Network 中 `*.async.js`/`*.js` 从 `/mgnt/` 根路径加载,状态 200、类型 `application/javascript`。
- 接口请求直连 `API_PREFIX`(测试网关),若跨域需在网关开启 CORS。
### 验证清单与常见问题
- 刷新不 404(`try_files` 生效)。
- CORS 报错:跨域直连需后端放行。
- Mixed Content:HTTPS 站点勿注入 `http://` 前缀。
- 资源 404:检查 `publicPath` 为 `./`,并确认 `dist/` 完整上传。
```
react-web-next/
├─ config/
│ ├─ config.ts # Umi 配置(启用 routes、hash history、proxy)
│ └─ routes.ts # 路由定义(按域分组)
├─ src/
│ ├─ layouts/
│ │ └─ BasicLayout.tsx # 基础布局(含统一头部操作区)
│ ├─ components/
│ │ └─ TenantScopeFilter/ # 统一的租户/子租户过滤器
│ ├─ pages/
│ │ ├─ Welcome.tsx # 示例页(接入租户过滤器)
│ │ └─ system/
│ │ └─ TenantManage/ # 重构后的租户管理页(树型新增等)
│ ├─ services/
│ │ └─ tenant.ts # 租户相关 API(复用后端接口)
│ ├─ utils/
│ │ ├─ request.ts # 请求封装与拦截
│ │ ├─ download.ts # 通用下载(支持 Content-Disposition)
│ │ └─ export.ts # 前端导出 Excel
│ ├─ app.tsx
│ └─ global.less
├─ package.json
└─ tsconfig.json
```
## 运行
1) 进入目录安装依赖:`npm install`(或 `pnpm i`/`yarn`)
2) 开发:
- dev(本地网关):`npm run start` 或 `npm run start:dev`
- test(测试网关):`npm run start:test`
- pre(预发网关):`npm run start:pre`
- 临时覆盖网关地址:
- PowerShell:`$env:GATEWAY_ORIGIN="http://10.x.x.x:9900"; npm run start:test`
- CMD:`set GATEWAY_ORIGIN=http://10.x.x.x:9900 && npm run start:test`
3) 构建:`npm run build`
> 说明:仅开发环境启用代理;生产由统一网关/容器转发。
## 迁移策略
- 按模块逐页迁移:
1. 将旧页逻辑拆分为“表格 + 表单 + 服务”三个层次;
2. 在表格 `toolBarRender` 中默认挂载 `TenantScopeFilter`;
3. 保留原有接口地址/参数命名,避免改动后端;
4. 体验层(样式/布局/反馈)用 Ant Design Pro 的推荐实践统一收敛。
- 公共模式:
- 统一请求封装(`src/utils/request.ts`)处理返回结构;
- 统一主题变量与间距;
- 统一“租户过滤”的行为:
- 选择租户 → 调用 `/tenant/switch?tenantId=X` 设置锚点;
- 勾选“包含子租户”→ 拦截器自动展开,无需改动每个接口;
- 支持“一键重置”回默认租户。
## 环境与代理
- 统一代理:`config/proxy.ts` 仅配置一条规则,将所有 `/api-*` 转发到统一网关 `GATEWAY_ORIGIN`。
- 环境选择:`REACT_APP_ENV=dev|test|pre` 选择 `proxy` 分支;默认 `dev`。
- 启动脚本:见 package.json 的 `start:*`(已内置常用网关地址)。
- 生产环境:不启用该代理,由 API 网关/容器转发到后端微服务。
## 下载与导出
- 下载(后端生成文件):
- 使用 `src/utils/download.ts` 的 `download(res, defaultName)`
- 示例:
```ts
const res = await request('/api-user/users/export', {
method: 'POST', responseType: 'blob', getResponse: true,
});
download(res, 'user.xls'); // 将从 Content-Disposition 解析文件名
```
- 直接 URL 下载(无 CORS 或简单文件):
- `downloadByUrl(url, filename?)`
- 前端导出 Excel(内存数据 → xlsx):
- 使用 `src/utils/export.ts` 的 `exportExcel({ data, header, fileName })`
- header 示例:`[{ title: '用户名', dataIndex: 'username' }]`
## 已完成
- 基础框架/布局、主题与请求封装;
- `TenantScopeFilter` 组件;
- 租户管理页(树型添加/编辑/删除)重构版;
- Welcome 示例接入过滤器;
- 路由与文档;
- 下载/导出工具与用户导出统一改造。
## 待办(按需推进)
- 逐步迁移原项目下系统、设备、订单等页面;
- 页面级权限点与菜单项的细化;
- 更精确的分页 total 透传(后端补齐后更新前端);
- 单元测试、E2E 用例补充。
如需我继续迁移具体页面,请告知优先级(如 系统/订单/设备 等模块)。
### 部署总结(生产环境)
- 构建:`npm run build:prod:mgnt`
- 等价:`cross-env APP_BASE=/mgnt REACT_APP_ENV=prod npm run build`
- Nginx:在 `/mgnt/` 托管静态资源,并增加接口反代:
```nginx
location = /mgnt { return 301 /mgnt/; }
location /mgnt/ {
alias /opt/apps/react-web-next/dist/;
index index.html;
try_files $uri $uri/ /mgnt/index.html;
}
location /api- {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9900; # 改为你的网关地址
}
```
- 访问:`http://<服务器>:9999/mgnt/`
- 验证:
- 资源从 `/mgnt/` 加载为 JS(200)
- 接口指向同域 `/api-*`,由 Nginx 反代到网关,无 CORS。