西藏巴青项目

index.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * 接口根地址(与 ruoyi-ui 的 VUE_APP_BASE_API 对齐)
  3. *
  4. * - 开发:.env.development → /dev-api(H5 代理)或 VITE_APP_API_HOST 直连
  5. * - 生产:.env.production → /prod-api(与 ruoyi-ui/.env.production 一致,由 Nginx 转发)
  6. *
  7. * 打包 H5:uni build -p h5
  8. */
  9. import { isExternal } from '@/utils/validate'
  10. const apiHost = String(import.meta.env.VITE_APP_API_HOST || 'http://192.168.1.6:8010').replace(/\/$/, '')
  11. /** 与 ruoyi-ui 的 VUE_APP_BASE_API 同源配置(dev/prod 由 .env 注入) */
  12. const envBaseApi = String(import.meta.env.VITE_APP_BASE_API || '').trim()
  13. /** H5 打包后取浏览器当前 host(IP/域名),与 ruoyi-ui 生产 /prod-api 同域反代一致 */
  14. function getBrowserOrigin() {
  15. if (typeof window === 'undefined' || !window.location) {
  16. return ''
  17. }
  18. return String(window.location.origin || '').replace(/\/$/, '')
  19. }
  20. /**
  21. * H5 开发是否走 /dev-api 代理(需 vite.config.js rewrite)
  22. * 默认 false:直连 VITE_APP_API_HOST
  23. */
  24. export const H5_USE_PROXY = false
  25. function resolveBaseApi() {
  26. if (import.meta.env.PROD) {
  27. const apiPath = envBaseApi || '/prod-api'
  28. // #ifdef H5
  29. const origin = getBrowserOrigin()
  30. if (origin) {
  31. const normalized = apiPath.startsWith('/') ? apiPath : `/${apiPath}`
  32. return `${origin}${normalized}`.replace(/\/$/, '')
  33. }
  34. // #endif
  35. // #ifndef H5
  36. if (/^https?:\/\//i.test(apiHost)) {
  37. return apiHost
  38. }
  39. // #endif
  40. return apiPath
  41. }
  42. const devBase = envBaseApi || '/dev-api'
  43. // #ifdef H5
  44. return H5_USE_PROXY ? devBase : apiHost
  45. // #endif
  46. // #ifndef H5
  47. return apiHost
  48. // #endif
  49. }
  50. export const BASE_API = resolveBaseApi()
  51. /**
  52. * 拼接接口或静态资源地址(/login、/profile/...)
  53. * @param {string} path
  54. */
  55. export function joinApiUrl(path) {
  56. if (!path) {
  57. return BASE_API
  58. }
  59. if (isExternal(path)) {
  60. return path
  61. }
  62. const p = path.startsWith('/') ? path : '/' + path
  63. const base = String(BASE_API).replace(/\/$/, '')
  64. return base + p
  65. }
  66. /** 交易市场项目专用,与 ruoyi-ui-app 的 Admin-Token 隔离,避免同端登录态串用 */
  67. export const TOKEN_KEY = 'Jiaoyi-Admin-Token'
  68. export const REMEMBER_USERNAME_KEY = 'jiaoyi_login_username'