罗广辉
2025-10-21 3bdd4cddba3837c20a4209a27564e73cabaa0392
feat: 打包环境调整
8 files modified
1 files deleted
1 files added
72 ■■■■■ changed files
env/.env 3 ●●●●● patch | view | raw | blame | history
env/.env.development 5 ●●●●● patch | view | raw | blame | history
env/.env.production 3 ●●●●● patch | view | raw | blame | history
env/.env.test 8 ●●●●● patch | view | raw | blame | history
package.json 7 ●●●●● patch | view | raw | blame | history
src/config/env.js 24 ●●●●● patch | view | raw | blame | history
src/main.js 3 ●●●●● patch | view | raw | blame | history
src/utils/common/index.js 6 ●●●● patch | view | raw | blame | history
src/utils/request/index.js 5 ●●●●● patch | view | raw | blame | history
vite.config.js 8 ●●●● patch | view | raw | blame | history
env/.env
@@ -4,9 +4,6 @@
# 开发环境配置
VITE_APP_ENV=development
# 接口地址
VITE_API_BASE_URL=https://wrj.shuixiongit.com/api
# 端口号
VITE_APP_PORT=9527
env/.env.development
@@ -1,10 +1,5 @@
# 开发环境配置
VITE_APP_ENV=development
# 接口地址
VITE_API_BASE_URL=https://wrj.shuixiongit.com/api
# 删除console
VITE_DROP_CONSOLE=false
VITE_APP_WEBVIEW_URL = 'https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper'
env/.env.production
@@ -1,8 +1,5 @@
# 生产环境配置
VITE_APP_ENV=production
# 接口地址
VITE_API_BASE_URL=https://wrj.shuixiongit.com/api
# 删除console
VITE_DROP_CONSOLE=true
env/.env.test
File was deleted
package.json
@@ -142,22 +142,21 @@
        "title": "H5 开发环境",
        "env": {
          "UNI_PLATFORM": "h5",
          "NAME_ER": "development",
          "VITE_APP_WEBVIEW_URL": "https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper"
          "ENV_NAME": "development"
        }
      },
      "h5-test": {
        "title": "H5 测试环境",
        "env": {
          "UNI_PLATFORM": "h5",
          "NAME_ER": "test"
          "ENV_NAME": "test"
        }
      },
      "h5-prod": {
        "title": "H5 生产环境",
        "env": {
          "UNI_PLATFORM": "h5",
          "NAME_ER": "production"
          "ENV_NAME": "production"
        }
      }
    }
src/config/env.js
New file
@@ -0,0 +1,24 @@
// 不同的环境变量配置
const development = {
  VITE_APP_ENV:'development',
  // 开发环境这里改为自己的
  VITE_APP_WEBVIEW_URL: 'http://192.168.1.15:5173/drone-app-web-view/#/webViewWrapper',
  VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
}
const test = {
  VITE_APP_ENV:'development',
  VITE_APP_WEBVIEW_URL: 'https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper',
  VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
}
const production = {
  VITE_APP_ENV:'production',
}
export default {
  development,
  test,
  production
}
src/main.js
@@ -15,6 +15,9 @@
import "virtual:uno.css"
import 'leaflet/dist/leaflet.css'
import configEnv from '@/config/env.js';
console.log('process.env.ENV_NAME',)
console.log('当前环境变量',configEnv?.[process?.env?.ENV_NAME])
export function createApp () {
  const app = createSSRApp(App)
  app.use(setupPlugins)
src/utils/common/index.js
@@ -10,6 +10,7 @@
 */
// 小程序更新检测
import { useUserStore } from "@/store/index.js"
import configEnv from "@/config/env.js";
export function mpUpdate () {
  const updateManager = uni.getUpdateManager()
@@ -39,9 +40,12 @@
  })
}
export function getEnvObj() {
  return configEnv?.[process?.env?.ENV_NAME] || {}
}
export function getWebViewUrl (targetUrl, otherParams) {
  const userStore = useUserStore()
  const url = "https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper"
  const url = getEnvObj().VITE_APP_WEBVIEW_URL
  // 1. 处理用户参数
  const userParams = userStore?.userInfo ? JSON.stringify(userStore.userInfo) : '{}'
  // 2. 构建查询参数字符串
src/utils/request/index.js
@@ -1,5 +1,7 @@
import Request from "uview-plus/libs/luch-request/index"
import { requestInterceptors, responseInterceptors } from "./interceptors"
import configEnv from "@/config/env.js";
import {getEnvObj} from "@/utils/index.js";
const http = new Request()
@@ -16,12 +18,11 @@
  })
}
// 引入拦截器配置
export function setupRequest () {
  http.setConfig(defaultConfig => {
    /* defaultConfig 为默认全局配置 */
    defaultConfig.baseURL = import.meta.env.VITE_API_BASE_URL
    defaultConfig.baseURL = getEnvObj().VITE_API_BASE_URL
    // #ifdef H5
    if (import.meta.env.VITE_APP_PROXY === "true") {
      defaultConfig.baseURL = import.meta.env.VITE_API_PREFIX
vite.config.js
@@ -9,12 +9,12 @@
  // mode: 区分生产环境还是开发环境
  console.log("command, mode -> ", command, mode)
  const { UNI_PLATFORM } = process.env
  console.log("UNI_PLATFORM -> ", UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
  const { UNI_PLATFORM,UNI_CUSTOM_DEFINE } = process.env
  console.log("平台 -> ", UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
  console.log("环境 -> ", UNI_CUSTOM_DEFINE && JSON.parse(UNI_CUSTOM_DEFINE)?.ENV_NAME) // 得到 development,test 等
  const env = loadEnv(mode, fileURLToPath(new URL("./env", import.meta.url)))
  console.log("环境变量 env -> ", env)
  // console.log("环境变量 env -> ", env)
  const isBuild = process.env.NODE_ENV === "production"
  return {
    // 自定义env目录