修复页面bug

This commit is contained in:
2025-09-23 16:12:43 +08:00
parent 408b500b6a
commit 84b6a87ca8
9 changed files with 144 additions and 25 deletions
+101
View File
@@ -7,6 +7,82 @@
## 目录结构
## 构建与部署(多环境)
- 前置要求: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/
@@ -103,3 +179,28 @@ react-web-next/
- 单元测试、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。