fix(env): 生产地址防呆,避免开发地址打进正式包
- src/env.js:生产构建固定 PROD_API_BASE,开发地址只从 .env.development.local 注入,源码不再保留内网地址字面量 - 新增 scripts/check-build-env.cjs:扫描产物含内网地址或缺生产域名即中止构建,已挂到 build:h5 / build:app-plus,另有 npm run check:env - 新增 .env.example 模板;开发变量改用 mode 专属的 .env.development.local(.env.local 在 production 模式也会被加载并注入客户端代码) - README 补充接口地址与环境变量防呆说明
This commit is contained in:
parent
5ae92e0f63
commit
ba33f9c59a
11
.env.example
Normal file
11
.env.example
Normal file
@ -0,0 +1,11 @@
|
||||
# 环境变量模板:复制为 .env.development.local 并填写真实值(该文件不提交 git)
|
||||
#
|
||||
# 生效范围:仅 NODE_ENV=development 的构建(npm run dev:h5 / dev:app-plus)
|
||||
# 发布构建(npm run build:h5 / build:app-plus)固定使用 src/env.js 中的
|
||||
# PROD_API_BASE,不会读取本文件的地址,详见 src/env.js 顶部说明。
|
||||
#
|
||||
# ⚠️ 不要用 .env.local:它在 production 模式同样会被加载,并会被构建工具注入到
|
||||
# 客户端代码里,导致内网地址出现在正式包中(scripts/check-build-env.cjs 会中止构建)。
|
||||
# ⚠️ 不要把 VUE_APP_API_BASE_OVERRIDE 写进 .env* 文件:它只允许由命令行显式注入。
|
||||
VUE_APP_API_BASE_URL=http://your-server-ip:6060
|
||||
VUE_APP_IMG_URL=https://ye4s.file.gzxunren.com
|
||||
16
README.md
16
README.md
@ -116,6 +116,22 @@
|
||||
- **依赖镜像**:`.npmrc` 指向 `registry.npmmirror.com`。
|
||||
- `src/pages/componentsA|B|C` 是 uView 官方示例页(含 67 个 `.nvue` 文件),未被 `pages.json` 注册、不参与编译,可按需删除。
|
||||
|
||||
### 接口地址与环境变量(防呆)
|
||||
|
||||
| 场景 | 地址来源 | 命令 |
|
||||
| --- | --- | --- |
|
||||
| 发布构建(H5 / App) | `src/env.js` 的 `PROD_API_BASE`(固定,不采用 .env 文件里的地址) | `npm run build:h5` / `build:app-plus` |
|
||||
| 本地开发 | `.env.development.local` 的 `VUE_APP_API_BASE_URL`(git 不跟踪) | `npm run dev:h5` / `dev:app-plus` |
|
||||
| 临时指向指定后端 | 命令行显式注入 `VUE_APP_API_BASE_OVERRIDE`(不落盘) | 见下 |
|
||||
|
||||
- 首次拉取代码后:复制 `.env.example` 为 `.env.development.local` 并填写内网地址。
|
||||
- 临时改后端(不写任何文件):`npx cross-env UNI_PLATFORM=h5 NODE_ENV=development VUE_APP_API_BASE_OVERRIDE=http://192.168.1.10:6060 vue-cli-service uni-serve`
|
||||
- **两道防线**:
|
||||
1. 生产构建只认 `PROD_API_BASE`,源码里也不再保留任何内网地址字面量;
|
||||
2. `npm run build:h5` / `build:app-plus` 结束后自动执行 `scripts/check-build-env.cjs`,产物中出现 `192.168.x.x` / `127.0.0.1`,或缺少生产域名 `qimen.wms.gztbyc.com`,即**构建失败**。手动校验:`npm run check:env`。
|
||||
- ⚠️ 开发地址**不要**写进 `.env.local`:该文件在 production 模式同样会被加载,并会被构建工具注入到客户端代码里(会被上面的校验拦下,但没必要绕这一圈)。
|
||||
- 现场排查设备连的是哪个后端:可用 `pages/settings/networkTest` 页面查看/测试接口地址。
|
||||
|
||||
### 回退方式
|
||||
|
||||
原 HBuilderX 工程完整备份在 `d:\WMS_PDA_bak_20261002-1936`(含 `node_modules`、`.hbuilderx`、`template.h5.html`、`vue.config.js`、`package.json`、`package-lock.json`)。需要回退时,把备份内容覆盖回项目根目录即可。
|
||||
|
||||
@ -8,8 +8,9 @@
|
||||
"build": "npm run build:h5",
|
||||
"dev:h5": "cross-env NODE_ENV=development UNI_PLATFORM=h5 NODE_OPTIONS=\"--openssl-legacy-provider --require=./scripts/node-compat.cjs\" vue-cli-service uni-serve",
|
||||
"dev:app-plus": "cross-env NODE_ENV=development UNI_PLATFORM=app-plus NODE_OPTIONS=\"--openssl-legacy-provider --require=./scripts/node-compat.cjs\" vue-cli-service uni-build --watch",
|
||||
"build:h5": "cross-env NODE_ENV=production UNI_PLATFORM=h5 NODE_OPTIONS=\"--openssl-legacy-provider --require=./scripts/node-compat.cjs\" vue-cli-service uni-build",
|
||||
"build:app-plus": "cross-env NODE_ENV=production UNI_PLATFORM=app-plus NODE_OPTIONS=\"--openssl-legacy-provider --require=./scripts/node-compat.cjs\" vue-cli-service uni-build",
|
||||
"build:h5": "cross-env NODE_ENV=production UNI_PLATFORM=h5 NODE_OPTIONS=\"--openssl-legacy-provider --require=./scripts/node-compat.cjs\" vue-cli-service uni-build && node scripts/check-build-env.cjs dist/build/h5",
|
||||
"build:app-plus": "cross-env NODE_ENV=production UNI_PLATFORM=app-plus NODE_OPTIONS=\"--openssl-legacy-provider --require=./scripts/node-compat.cjs\" vue-cli-service uni-build && node scripts/check-build-env.cjs dist/build/app-plus",
|
||||
"check:env": "node scripts/check-build-env.cjs",
|
||||
"lint": "eslint . --fix",
|
||||
"test": "eslint . --fix"
|
||||
},
|
||||
|
||||
105
scripts/check-build-env.cjs
Normal file
105
scripts/check-build-env.cjs
Normal file
@ -0,0 +1,105 @@
|
||||
/**
|
||||
* 构建产物「接口地址」校验(防呆)
|
||||
*
|
||||
* 背景:本项目原 HBuilderX 工程里 src/env.js 的开发地址长期处于生效状态,
|
||||
* 打正式包时会连内网测试库(曾在姊妹项目 wms_worker_station 造成过生产事故)。
|
||||
* 本脚本在构建结束后扫描产物,做两道校验:
|
||||
* 1. 产物中不得出现内网地址(192.168.x.x / 127.0.0.1)——否则说明开发地址进了包;
|
||||
* 2. 产物中必须出现生产域名 PROD_HOST——否则说明生产地址根本没被编译进去
|
||||
* (例如误用了 override、或 env.js 被改坏)。
|
||||
* 任一条不满足即 exit 1,让 CI / 本地打包在「产物不可信」时就停下来。
|
||||
*
|
||||
* 用法:
|
||||
* node scripts/check-build-env.cjs # 自动扫描已存在的产物目录
|
||||
* node scripts/check-build-env.cjs dist/build/h5 # 只校验指定目录
|
||||
* node scripts/check-build-env.cjs --allow-missing-prod # 跳过第 2 条(特殊域名场景)
|
||||
*/
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const ROOT = path.resolve(__dirname, '..')
|
||||
const DEFAULT_TARGETS = [path.join(ROOT, 'dist', 'build', 'h5'), path.join(ROOT, 'dist', 'build', 'app-plus')]
|
||||
const SCAN_EXTENSIONS = ['.js', '.html', '.json', '.css']
|
||||
|
||||
// 生产接口域名:必须与 src/env.js 的 PROD_API_BASE 保持一致
|
||||
const PROD_HOST = 'qimen.wms.gztbyc.com'
|
||||
// 内网地址特征(开发环境地址一旦出现在产物里就是事故)
|
||||
const DEV_PATTERNS = [/192\.168\.\d{1,3}\.\d{1,3}/g, /127\.0\.0\.1/g]
|
||||
|
||||
const args = process.argv.slice(2)
|
||||
const allowMissingProd = args.includes('--allow-missing-prod')
|
||||
const explicitTargets = args.filter((arg) => !arg.startsWith('--'))
|
||||
|
||||
const log = (...rest) => console.log('[check-build-env]', ...rest)
|
||||
|
||||
function collectFiles(dir, out = []) {
|
||||
if (!fs.existsSync(dir)) return out
|
||||
for (const name of fs.readdirSync(dir)) {
|
||||
const full = path.join(dir, name)
|
||||
const stat = fs.statSync(full)
|
||||
if (stat.isDirectory()) {
|
||||
collectFiles(full, out)
|
||||
} else if (SCAN_EXTENSIONS.includes(path.extname(name).toLowerCase())) {
|
||||
out.push(full)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
const targets = (explicitTargets.length ? explicitTargets : DEFAULT_TARGETS)
|
||||
.map((target) => path.resolve(ROOT, target))
|
||||
.filter((dir) => fs.existsSync(dir))
|
||||
|
||||
if (!targets.length) {
|
||||
console.error('[check-build-env] ✗ 未找到任何构建产物目录,请先执行构建(npm run build:h5 / build:app-plus)')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
let failed = false
|
||||
|
||||
for (const dir of targets) {
|
||||
const files = collectFiles(dir)
|
||||
const rel = path.relative(ROOT, dir).replace(/\\/g, '/')
|
||||
if (!files.length) {
|
||||
log(`✗ ${rel} 为空目录,跳过`)
|
||||
continue
|
||||
}
|
||||
|
||||
const devHits = []
|
||||
let hasProdHost = false
|
||||
|
||||
for (const file of files) {
|
||||
const content = fs.readFileSync(file, 'utf-8')
|
||||
if (content.includes(PROD_HOST)) hasProdHost = true
|
||||
for (const pattern of DEV_PATTERNS) {
|
||||
const matches = content.match(pattern)
|
||||
if (matches) {
|
||||
devHits.push({ file: path.relative(dir, file).replace(/\\/g, '/'), hits: Array.from(new Set(matches)) })
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (devHits.length) {
|
||||
failed = true
|
||||
console.error(`[check-build-env] ✗ ${rel} 中发现内网地址,产物不可用于发布:`)
|
||||
devHits.forEach(({ file, hits }) => console.error(` - ${file}(${hits.join(', ')})`))
|
||||
console.error(' 排查:src/env.js 是否被改回开发地址 / 是否误用 VUE_APP_API_BASE_OVERRIDE')
|
||||
}
|
||||
|
||||
if (!hasProdHost && !allowMissingProd) {
|
||||
failed = true
|
||||
console.error(`[check-build-env] ✗ ${rel} 中未发现生产域名 ${PROD_HOST},产物不可用于发布`)
|
||||
console.error(' 排查:src/env.js 的 PROD_API_BASE 是否被改动;如属特殊场景请加 --allow-missing-prod')
|
||||
}
|
||||
|
||||
if (!devHits.length && (hasProdHost || allowMissingProd)) {
|
||||
log(`✓ ${rel} 校验通过(${files.length} 个文件,无内网地址${hasProdHost ? `,含生产域名 ${PROD_HOST}` : ''})`)
|
||||
}
|
||||
}
|
||||
|
||||
if (failed) {
|
||||
console.error('[check-build-env] ✗ 校验未通过,已中止')
|
||||
process.exit(1)
|
||||
}
|
||||
log('全部产物校验通过')
|
||||
27
src/env.js
27
src/env.js
@ -1,4 +1,25 @@
|
||||
// export const BASE_URL = 'http://qimen.wms.gztbyc.com' //生产环境接口地址
|
||||
export const BASE_URL = 'http://192.168.11.56:6060' //开发环境接口地址
|
||||
/**
|
||||
* 前端环境配置
|
||||
*
|
||||
* ⚠️ 防呆约定(曾有「开发地址被打进正式包」的隐患,勿随意改动):
|
||||
* 1. 生产构建(NODE_ENV=production)固定走 PROD_API_BASE,不采用任何 .env 文件里的地址,
|
||||
* 所以构建机上残留的开发地址不会进正式包;
|
||||
* 2. 开发地址只允许来自 `.env.development.local`(VUE_APP_API_BASE_URL,该文件 git 不跟踪),
|
||||
* 或命令行显式注入 `VUE_APP_API_BASE_OVERRIDE`(不写入任何 .env 文件,真机调试用);
|
||||
* 注意:不能写在 `.env.local`——那个文件在 production 模式同样会被加载并注入到客户端代码里;
|
||||
* 源码中也不保留内网地址字面量,否则不管走哪个分支都会被打进产物。
|
||||
* 3. 构建后由 `scripts/check-build-env.cjs` 扫描产物:含内网地址或缺少生产域名即中止构建。
|
||||
*/
|
||||
const PROD_API_BASE = 'http://qimen.wms.gztbyc.com' //生产环境接口地址
|
||||
|
||||
const isProd = process.env.NODE_ENV === 'production'
|
||||
// 命令行显式注入优先(不落盘,用于真机调试指向指定后端)
|
||||
const overrideApiBase = process.env.VUE_APP_API_BASE_OVERRIDE || ''
|
||||
// .env.development.local 注入的开发地址(仅 VUE_APP_ 前缀会被构建工具注入到代码里)
|
||||
const envFileApiBase = process.env.VUE_APP_API_BASE_URL || ''
|
||||
|
||||
export const BASE_URL = overrideApiBase || (isProd ? PROD_API_BASE : envFileApiBase) //接口根地址
|
||||
export const API_URL = `${BASE_URL}/system` //后台接口域名
|
||||
export const IMG_URL = 'https://ye4s.file.gzxunren.com' //全局网络图片地址变量,css背景图片地址变量在uni.scss
|
||||
export const IMG_URL = process.env.VUE_APP_IMG_URL || 'https://ye4s.file.gzxunren.com' //全局网络图片地址变量,css背景图片地址变量在uni.scss
|
||||
export const IS_PROD_BUILD = isProd //是否生产构建(排查现场问题时便于页面展示)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user