From e1a3ec5e44b95dd9917c098e7c7836ee2b2ac388 Mon Sep 17 00:00:00 2001
From: 张含笑 <zhx18749296735@163.com>
Date: Tue, 21 Oct 2025 17:32:57 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'
---
/dev/null | 8 ----
src/config/env.js | 24 ++++++++++++
env/.env.development | 5 --
src/main.js | 3 +
src/utils/request/index.js | 5 +-
package.json | 7 +--
env/.env.production | 3 -
vite.config.js | 8 ++--
env/.env | 3 -
src/utils/common/index.js | 7 +++
10 files changed, 43 insertions(+), 30 deletions(-)
diff --git a/env/.env b/env/.env
index 4d07c50..695ff20 100644
--- a/env/.env
+++ b/env/.env
@@ -4,9 +4,6 @@
# 开发环境配置
VITE_APP_ENV=development
-# 接口地址
-VITE_API_BASE_URL=https://wrj.shuixiongit.com/api
-
# 端口号
VITE_APP_PORT=9527
diff --git a/env/.env.development b/env/.env.development
index 96b932b..80a51c6 100644
--- a/env/.env.development
+++ b/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'
diff --git a/env/.env.production b/env/.env.production
index 37d85fa..9d816d2 100644
--- a/env/.env.production
+++ b/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
diff --git a/env/.env.test b/env/.env.test
deleted file mode 100644
index c4499c7..0000000
--- a/env/.env.test
+++ /dev/null
@@ -1,8 +0,0 @@
-# 预发布环境配置
-VITE_APP_ENV=staging
-
-# 接口地址
-VITE_API_BASE_URL=https://wrj.shuixiongit.com/api
-
-# 删除console
-VITE_DROP_CONSOLE=true
diff --git a/package.json b/package.json
index 8b2d519..118abfc 100644
--- a/package.json
+++ b/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"
}
}
}
diff --git a/src/config/env.js b/src/config/env.js
new file mode 100644
index 0000000..b8e8d33
--- /dev/null
+++ b/src/config/env.js
@@ -0,0 +1,24 @@
+
+// 不同的环境变量配置
+const development = {
+ 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 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
+}
diff --git a/src/main.js b/src/main.js
index 3d16126..02e79c3 100644
--- a/src/main.js
+++ b/src/main.js
@@ -15,6 +15,9 @@
import "virtual:uno.css"
import 'leaflet/dist/leaflet.css'
+import {getEnvObj} from "@/utils/index.js";
+console.log('process.env.ENV_NAME',)
+console.log('当前环境变量',getEnvObj())
export function createApp () {
const app = createSSRApp(App)
app.use(setupPlugins)
diff --git a/src/utils/common/index.js b/src/utils/common/index.js
index 8d332d7..1c2bd62 100644
--- a/src/utils/common/index.js
+++ b/src/utils/common/index.js
@@ -10,6 +10,8 @@
*/
// 小程序更新检测
import { useUserStore } from "@/store/index.js"
+import configEnv from "@/config/env.js";
+import process from "node:process"
export function mpUpdate () {
const updateManager = uni.getUpdateManager()
@@ -39,9 +41,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. 构建查询参数字符串
diff --git a/src/utils/request/index.js b/src/utils/request/index.js
index 53652c7..bfbfefc 100644
--- a/src/utils/request/index.js
+++ b/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
diff --git a/vite.config.js b/vite.config.js
index f77f5cb..084e427 100644
--- a/vite.config.js
+++ b/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目录
--
Gitblit v1.9.3