罗广辉
2025-11-15 fac27acb9b453cfe9f66fdc4ef510d0c615d1e8f
feat: 配置环境变量
3 files modified
23 ■■■■ changed files
src/config/env.js 8 ●●●●● patch | view | raw | blame | history
src/utils/common/index.js 3 ●●●● patch | view | raw | blame | history
vite.config.js 12 ●●●●● patch | view | raw | blame | history
src/config/env.js
@@ -1,5 +1,4 @@
// 不同的环境变量配置
// 如果是打包app,需要改这里,h5不需要改这里
const development = {
  VITE_APP_ENV:'development',
  // 开发环境这里改为自己的
@@ -15,11 +14,10 @@
  VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
}
// app打包要改这里
const production = {
  VITE_APP_ENV:'production',
  VITE_APP_WEBVIEW_URL: 'https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper',
  VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
  VITE_APP_WEBVIEW_URL: 'https://aisky.org.cn/drone-app-web-view/#/webViewWrapper',
  VITE_API_BASE_URL: 'https://aisky.org.cn/api',
}
export default {
src/utils/common/index.js
@@ -49,8 +49,7 @@
  }
}
export function getEnvObj() {
  const envName = JSON.parse(__APP_ENV__?.UNI_CUSTOM_DEFINE||null)?.ENV_NAME
  return configEnv[envName || import.meta.env.VITE_APP_ENV] || {}
  return configEnv[__APP_ENV__?.ENV_NAME] || {}
}
export function getWebViewUrl (targetUrl, otherParams) {
  const userStore = useUserStore()
vite.config.js
@@ -12,15 +12,19 @@
  const { UNI_PLATFORM,UNI_CUSTOM_DEFINE } = process.env
  const ENV_NAME = UNI_CUSTOM_DEFINE && JSON.parse(UNI_CUSTOM_DEFINE)?.ENV_NAME
  console.log("平台 -> ", UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
  console.log("环境 -> ", ENV_NAME || 'development') // 得到 development,test 等
  const __APP_ENV__ = {
    UNI_PLATFORM: UNI_PLATFORM,
    ENV_NAME: ENV_NAME || 'development'
  }
  console.log("平台 -> ", __APP_ENV__.UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
  console.log("环境 -> ", __APP_ENV__.ENV_NAME) // 得到 development,test 等
  const env = loadEnv(mode, fileURLToPath(new URL("./env", import.meta.url)))
  // console.log("环境变量 env -> ", env)
  const isBuild = process.env.NODE_ENV === "production"
  return {
    define: {
      __APP_ENV__: process.env,
      __APP_ENV__,
    },
    // 自定义env目录
    envDir: "./env",
@@ -37,7 +41,7 @@
      hmr: true,
      host: true,
      open: true,
      proxy: createViteProxy(env,ENV_NAME)
      proxy: createViteProxy(env,__APP_ENV__.ENV_NAME)
    },
    // 设置scss的api类型为modern-compiler
    css: {