env.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * 小程序环境开关 —— **只改这一处** 即可切换本机 / 测试 / 生产 / 本机联调 H5
  3. *
  4. * | APP_ENV 值 | 用途 |
  5. * |------------------|------|
  6. * | `local` | 本机 backend + 本机 web-admin |
  7. * | `test` | 测试门户(API / H5 / 静态 均 9911) |
  8. * | `testLocalH5` | 测试 API + 本机 H5(仅联调 webview 样式;需本机 `pnpm dev` 且代理到测试 API) |
  9. * | `production` | 生产门户 https://w.shanxiachem.com |
  10. *
  11. * 上生产:改成 `export const APP_ENV = 'production'`,再执行 `npm run release:check-api`
  12. */
  13. export const APP_ENV = 'production'
  14. /** 微信小程序 AppID(生产正式号;测试号若不同可按环境再拆) */
  15. export const WECHAT_MP_APPID = 'wx7db1ddb2a90c9425'
  16. /**
  17. * 环境预设(由生产 Web https://w.shanxiachem.com/ 与部署文档核对)
  18. *
  19. * | 项 | 生产实测 |
  20. * |----|----------|
  21. * | Web | https://w.shanxiachem.com/ |
  22. * | API | https://w.shanxiachem.com/api |
  23. * | 静态资源 | https://w.shanxiachem.com/shanxia-static/ |
  24. * | MinIO 附件 | https://res.shanxiachem.com/ruoyi |
  25. */
  26. export const ENV_PRESETS = {
  27. local: {
  28. key: 'local',
  29. label: '本机',
  30. apiBaseUrl: 'http://127.0.0.1:8000/api',
  31. staticAssetsBaseUrl: 'http://121.43.228.102:9911/shanxia-static/',
  32. webOrigin: 'http://127.0.0.1:3006',
  33. minioBaseUrl: 'http://47.99.164.66:20004/zkxw'
  34. },
  35. test: {
  36. key: 'test',
  37. label: '测试',
  38. apiBaseUrl: 'http://121.43.228.102:9911/api',
  39. staticAssetsBaseUrl: 'http://121.43.228.102:9911/shanxia-static/',
  40. webOrigin: 'http://121.43.228.102:9911',
  41. minioBaseUrl: 'http://121.43.228.102:9911/ruoyi'
  42. },
  43. /** 测试业务数据 + 本机 H5 资源(改样式时用,不用于发测试包) */
  44. testLocalH5: {
  45. key: 'testLocalH5',
  46. label: '测试API+本机H5',
  47. apiBaseUrl: 'http://121.43.228.102:9911/api',
  48. staticAssetsBaseUrl: 'http://121.43.228.102:9911/shanxia-static/',
  49. webOrigin: 'http://127.0.0.1:3006',
  50. minioBaseUrl: 'http://121.43.228.102:9911/ruoyi'
  51. },
  52. production: {
  53. key: 'production',
  54. label: '生产',
  55. apiBaseUrl: 'https://w.shanxiachem.com/api',
  56. staticAssetsBaseUrl: 'https://w.shanxiachem.com/shanxia-static/',
  57. webOrigin: 'https://w.shanxiachem.com',
  58. minioBaseUrl: 'https://res.shanxiachem.com/ruoyi'
  59. }
  60. }
  61. const preset = ENV_PRESETS[APP_ENV] || ENV_PRESETS.local
  62. export const CURRENT_ENV = preset
  63. export const IS_PRODUCTION = preset.key === 'production'
  64. export const IS_TEST = preset.key === 'test'
  65. export const IS_TEST_LOCAL_H5 = preset.key === 'testLocalH5'
  66. export const IS_LOCAL = preset.key === 'local'
  67. export const API_BASE_URL = preset.apiBaseUrl
  68. export const STATIC_ASSETS_BASE_URL = preset.staticAssetsBaseUrl
  69. export const WEB_ORIGIN = preset.webOrigin
  70. export const MINIO_BASE_URL = preset.minioBaseUrl
  71. /** 历史 MinIO 公网前缀 → 展示时改写到当前 MINIO_BASE_URL */
  72. const LEGACY_MINIO_BASE_PREFIXES = [
  73. 'https://w.shanxiachem.com/ruoyi',
  74. 'http://w.shanxiachem.com/ruoyi',
  75. 'https://121.43.228.102:9911/ruoyi',
  76. 'http://121.43.228.102:9911/ruoyi',
  77. 'https://www.chengyantek.cn/zkxw',
  78. 'http://www.chengyantek.cn/zkxw',
  79. 'http://47.99.164.66:20004/zkxw',
  80. 'https://47.99.164.66:20004/zkxw'
  81. ]
  82. /** 拼 MinIO 完整访问地址;旧主机绝对 URL 改写到当前环境前缀 */
  83. export function resolveMinioUrl(url) {
  84. const trimmed = String(url || '').trim()
  85. if (!trimmed) return ''
  86. const base = String(
  87. (ENV_PRESETS[APP_ENV] || ENV_PRESETS.local).minioBaseUrl || ''
  88. ).replace(/\/$/, '')
  89. if (/^https?:\/\//i.test(trimmed)) {
  90. if (base && trimmed.toLowerCase().startsWith(base.toLowerCase())) {
  91. return trimmed
  92. }
  93. for (const prefix of LEGACY_MINIO_BASE_PREFIXES) {
  94. if (trimmed.toLowerCase().startsWith(prefix.toLowerCase())) {
  95. const objectPath = trimmed.slice(prefix.length).replace(/^\//, '')
  96. return base ? `${base}/${objectPath}` : trimmed
  97. }
  98. }
  99. return trimmed
  100. }
  101. let rel = trimmed.replace(/^\//, '')
  102. if (base.endsWith('/ruoyi') && (rel === 'ruoyi' || rel.startsWith('ruoyi/'))) {
  103. rel = rel.replace(/^ruoyi\/?/, '')
  104. }
  105. if (base.endsWith('/zkxw') && (rel === 'zkxw' || rel.startsWith('zkxw/'))) {
  106. rel = rel.replace(/^zkxw\/?/, '')
  107. }
  108. return base ? `${base}/${rel}` : trimmed
  109. }
  110. /** 每次调用按当前 APP_ENV 取值,避免热更新后 api.js 里拷贝的常量仍指向旧环境 */
  111. export function resolveApiBase() {
  112. return (ENV_PRESETS[APP_ENV] || ENV_PRESETS.local).apiBaseUrl
  113. }
  114. export function resolveWebOrigin() {
  115. return (ENV_PRESETS[APP_ENV] || ENV_PRESETS.local).webOrigin
  116. }