diff --git a/.claude/settings.local.json b/.claude/settings.local.json index c0dddba3..ecf73e89 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -17,7 +17,8 @@ "Bash(lsof:*)", "Bash(findstr:*)", "Bash(npm run start:*)", - "Bash(npm start)" + "Bash(npm start)", + "Bash(npm run build:*)" ], "deny": [], "ask": [] diff --git a/zlt-web/react-web-next/README.md b/zlt-web/react-web-next/README.md index 264c7d04..ab24f481 100644 --- a/zlt-web/react-web-next/README.md +++ b/zlt-web/react-web-next/README.md @@ -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。 diff --git a/zlt-web/react-web-next/config/config.ts b/zlt-web/react-web-next/config/config.ts index c50932cc..e26ae5f4 100644 --- a/zlt-web/react-web-next/config/config.ts +++ b/zlt-web/react-web-next/config/config.ts @@ -8,11 +8,14 @@ import proxy from './proxy'; const { REACT_APP_ENV } = process.env as any; const isDev = process.env.NODE_ENV === 'development'; +// 构建期注入 API 前缀(生产包/测试包/预发包),开发环境走代理不加前缀 +const API_PREFIX = isDev ? '' : (process.env.API_PREFIX || ''); +const APP_BASE = isDev ? '/' : (process.env.APP_BASE || '/'); export default { hash: true, // 资源路径:开发用根路径,生产为相对路径 - publicPath: isDev ? '/' : './', + publicPath: isDev ? '/' : (APP_BASE.endsWith('/') ? APP_BASE : APP_BASE + '/'), antd: {}, // 页面 meta 标签 metas: [ @@ -36,8 +39,14 @@ export default { routes, // 历史模式 history: { type: 'browser' }, + base: APP_BASE, // Dev 代理 proxy: (proxy as any)[REACT_APP_ENV || 'dev'], + // 向业务代码注入构建期常量 + define: { + 'process.env.API_PREFIX': API_PREFIX, + 'process.env.APP_BASE': APP_BASE, + }, // 构建优化 nodeModulesTransform: { type: 'none' }, mfsu: false, diff --git a/zlt-web/react-web-next/package.json b/zlt-web/react-web-next/package.json index 06110698..0a36e31d 100644 --- a/zlt-web/react-web-next/package.json +++ b/zlt-web/react-web-next/package.json @@ -5,9 +5,15 @@ "scripts": { "start": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=dev GATEWAY_ORIGIN=http://127.0.0.1:9900 umi dev", "start:dev": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=dev GATEWAY_ORIGIN=http://127.0.0.1:9900 umi dev", - "start:test": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=test GATEWAY_ORIGIN=http://119.45.169.13:9900 umi dev", + "start:test": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=test GATEWAY_ORIGIN=http://local.goflytec.com:9900 umi dev", "start:pre": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=pre GATEWAY_ORIGIN=http://119.45.169.13:9900 umi dev", "build": "cross-env NODE_OPTIONS=--openssl-legacy-provider umi build", + "build:test": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=test API_PREFIX=http://119.45.169.13:9900 umi build", + "build:pre": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=pre API_PREFIX=http://119.45.169.13:9900 umi build", + "build:prod": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=prod umi build", + "build:test:mgnt": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=test APP_BASE=/mgnt API_PREFIX=http://local.goflytec.com:9900 umi build", + "build:pre:mgnt": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=pre APP_BASE=/mgnt API_PREFIX=http://local.goflytec.com:9900 umi build", + "build:prod:mgnt": "cross-env NODE_OPTIONS=--openssl-legacy-provider REACT_APP_ENV=prod APP_BASE=/mgnt umi build", "lint": "eslint --ext .ts,.tsx src", "tsc": "tsc --noEmit" }, diff --git a/zlt-web/react-web-next/src/app.tsx b/zlt-web/react-web-next/src/app.tsx index ec56f494..17862207 100644 --- a/zlt-web/react-web-next/src/app.tsx +++ b/zlt-web/react-web-next/src/app.tsx @@ -1,4 +1,3 @@ -// 避免类型耦合,使用 any 降级 import './global.less'; import React from 'react'; import { ThemeProvider } from './theme'; @@ -8,11 +7,12 @@ export function rootContainer(container: React.ReactNode, _args?: any) { return React.createElement(ThemeProvider, null, container); } -// 使用简化的 request 配置,避免与类型强绑定(兼容 umi 3) +const API_PREFIX = process.env.API_PREFIX || ''; export const request: any = { + prefix: API_PREFIX, errorConfig: { errorHandler(error: any) { - // 可在此处统一处理错误,如通知等 + // 统一错误处理 throw error; }, }, @@ -47,7 +47,6 @@ export const request: any = { ], }; -// 简单路由守卫:未登录访问受保护路由时跳回登录;已登录访问登录页时跳到欢迎页 export function onRouteChange({ location }: any) { try { const token = sessionStorage.getItem('access_token'); @@ -59,3 +58,4 @@ export function onRouteChange({ location }: any) { } } catch {} } + diff --git a/zlt-web/react-web/src/main/frontend/config/proxy.ts b/zlt-web/react-web/src/main/frontend/config/proxy.ts index 61b5280f..8022a190 100644 --- a/zlt-web/react-web/src/main/frontend/config/proxy.ts +++ b/zlt-web/react-web/src/main/frontend/config/proxy.ts @@ -9,43 +9,43 @@ export default { dev: { '/api-uaa/': { - target: 'http://119.45.169.13:9900', - // target: 'http://127.0.0.1:9900', + // target: 'http://119.45.169.13:9900', + target: 'http://local.goflytec.com:9900', changeOrigin: true, }, '/api-user/': { - target: 'http://119.45.169.13:9900', - // target: 'http://127.0.0.1:9900', + // target: 'http://119.45.169.13:9900', + target: 'http://local.goflytec.com:9900', changeOrigin: true, }, '/api-log/': { - target: 'http://119.45.169.13:9900', - // target: 'http://127.0.0.1:9900', + // target: 'http://119.45.169.13:9900', + target: 'http://local.goflytec.com:9900', changeOrigin: true, }, '/api-search/': { - target: 'http://119.45.169.13:9900', - // target: 'http://127.0.0.1:9900', + // target: 'http://119.45.169.13:9900', + target: 'http://local.goflytec.com:9900', changeOrigin: true, }, '/api-generator/': { - target: 'http://119.45.169.13:9900', - // target: 'http://127.0.0.1:9900', + // target: 'http://119.45.169.13:9900', + target: 'http://local.goflytec.com:9900', changeOrigin: true, }, '/api-file/': { - target: 'http://119.45.169.13:9900', - // target: 'http://127.0.0.1:9900', + // target: 'http://119.45.169.13:9900', + target: 'http://local.goflytec.com:9900', changeOrigin: true, }, '/api-order/': { - target: 'http://119.45.169.13:9900', - // target: 'http://127.0.0.1:9900', + // target: 'http://119.45.169.13:9900', + target: 'http://local.goflytec.com:9900', changeOrigin: true, }, '/api-device/': { - target: 'http://119.45.169.13:9900', - // target: 'http://127.0.0.1:9900', + //target: 'http://119.45.169.13:9900', + target: 'http://local.goflytec.com:9900', changeOrigin: true, }, }, diff --git a/zlt-web/react-web/src/main/frontend/package.json b/zlt-web/react-web/src/main/frontend/package.json index 31a6cd3d..15dd030f 100644 --- a/zlt-web/react-web/src/main/frontend/package.json +++ b/zlt-web/react-web/src/main/frontend/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "analyze": "cross-env ANALYZE=1 umi build", - "build": "umi build", + "build": "set NODE_OPTIONS=--openssl-legacy-provider && umi build", "deploy": "npm run build && npm run gh-pages", "dev": "npm run start:dev", "gh-pages": "gh-pages -d dist", diff --git a/zlt-web/react-web/src/main/frontend/src/app.tsx b/zlt-web/react-web/src/main/frontend/src/app.tsx index 04d86989..83cb7eee 100644 --- a/zlt-web/react-web/src/main/frontend/src/app.tsx +++ b/zlt-web/react-web/src/main/frontend/src/app.tsx @@ -311,4 +311,6 @@ export const request: RequestConfig = { }; }, }, + // 生产环境使用相对路径,开发环境使用代理 + baseURL: process.env.NODE_ENV === 'production' ? '/' : undefined, }; diff --git a/zlt-web/react-web/src/main/frontend/src/services/tenant.ts b/zlt-web/react-web/src/main/frontend/src/services/tenant.ts index 93cbd840..8395e79d 100644 --- a/zlt-web/react-web/src/main/frontend/src/services/tenant.ts +++ b/zlt-web/react-web/src/main/frontend/src/services/tenant.ts @@ -1,4 +1,4 @@ -import { request } from '@umijs/max'; +import { request } from 'umi'; export interface TenantTreeNode { tenantId: string;