From 6f9be933391c2877bf41290635a07b9857e0e78c Mon Sep 17 00:00:00 2001
From: 罗广辉 <guanghui.luo@foxmail.com>
Date: Tue, 21 Oct 2025 17:45:54 +0800
Subject: [PATCH] feat: 打包环境调整

---
 src/config/env.js         |    2 +-
 src/main.js               |    1 -
 build/plugins/index.js    |    1 -
 build/config/proxy.js     |    8 +++++---
 env/.env.production       |    2 +-
 vite.config.js            |    9 +++++++--
 src/utils/common/index.js |    6 ++++--
 7 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/build/config/proxy.js b/build/config/proxy.js
index 9c31850..25fada2 100644
--- a/build/config/proxy.js
+++ b/build/config/proxy.js
@@ -8,13 +8,15 @@
  * Copyright 2025 OBKoro1, All Rights Reserved.
  * 2025-09-28 09:31:16
  */
-export const createViteProxy = (env) => {
-  const { VITE_APP_PROXY, VITE_API_PREFIX, VITE_API_BASE_URL } = env
+import configEnv from "../../src/config/env.js";
+
+export const createViteProxy = (env,envName) => {
+  const { VITE_APP_PROXY, VITE_API_PREFIX } = env
   // 不使用代理直接返回
   if (!JSON.parse(VITE_APP_PROXY)) return undefined
   const proxy = {
     [VITE_API_PREFIX]: {
-      target: VITE_API_BASE_URL,
+      target: configEnv[envName].VITE_API_BASE_URL,
       changeOrigin: true,
       rewrite: path => path.replace(new RegExp(`^${VITE_API_PREFIX}`), ''),
     },
diff --git a/build/plugins/index.js b/build/plugins/index.js
index 0cb9962..49a2364 100644
--- a/build/plugins/index.js
+++ b/build/plugins/index.js
@@ -6,7 +6,6 @@
 import { AutoRegistryComponents } from './component'
 import { MCPPlugin } from './mcp'
 import { ConfigUnoCSSPlugin } from './unocss'
-
 export default function createVitePlugins (isBuild) {
   const vitePlugins = [
     // UnoCSS配置
diff --git a/env/.env.production b/env/.env.production
index 9d816d2..15e9377 100644
--- a/env/.env.production
+++ b/env/.env.production
@@ -2,4 +2,4 @@
 VITE_APP_ENV=production
 
 # 删除console
-VITE_DROP_CONSOLE=true
+VITE_DROP_CONSOLE=false
diff --git a/src/config/env.js b/src/config/env.js
index b8e8d33..b17f88e 100644
--- a/src/config/env.js
+++ b/src/config/env.js
@@ -8,7 +8,7 @@
 }
 
 const test = {
-  VITE_APP_ENV:'development',
+  VITE_APP_ENV:'test',
   VITE_APP_WEBVIEW_URL: 'https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper',
   VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
 }
diff --git a/src/main.js b/src/main.js
index 02e79c3..3edc248 100644
--- a/src/main.js
+++ b/src/main.js
@@ -16,7 +16,6 @@
 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)
diff --git a/src/utils/common/index.js b/src/utils/common/index.js
index 1c2bd62..666398d 100644
--- a/src/utils/common/index.js
+++ b/src/utils/common/index.js
@@ -11,7 +11,7 @@
 // 小程序更新检测
 import { useUserStore } from "@/store/index.js"
 import configEnv from "@/config/env.js";
-import process from "node:process"
+import process from "node:process";
 
 export function mpUpdate () {
   const updateManager = uni.getUpdateManager()
@@ -42,11 +42,13 @@
 }
 
 export function getEnvObj() {
-  return configEnv?.[process?.env?.ENV_NAME] || {}
+  const envName = JSON.parse(__APP_ENV__.UNI_CUSTOM_DEFINE)?.ENV_NAME
+  return configEnv[envName] || {}
 }
 export function getWebViewUrl (targetUrl, otherParams) {
   const userStore = useUserStore()
   const url = getEnvObj().VITE_APP_WEBVIEW_URL
+  console.log(getEnvObj(),'getEnvObj()')
   // 1. 处理用户参数
   const userParams = userStore?.userInfo ? JSON.stringify(userStore.userInfo) : '{}'
   // 2. 构建查询参数字符串
diff --git a/vite.config.js b/vite.config.js
index 084e427..a9bdfac 100644
--- a/vite.config.js
+++ b/vite.config.js
@@ -3,6 +3,7 @@
 import { defineConfig, loadEnv } from "vite"
 import { createViteProxy } from "./build/config/index"
 import createVitePlugins from "./build/plugins/index"
+import configEnv from "./src/config/env.js";
 
 // https://vitejs.dev/config/
 export default defineConfig(({ command, mode }) => {
@@ -10,13 +11,17 @@
   console.log("command, mode -> ", command, mode)
 
   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("环境 -> ", UNI_CUSTOM_DEFINE && JSON.parse(UNI_CUSTOM_DEFINE)?.ENV_NAME) // 得到 development,test 等
+  console.log("环境 -> ", 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,
+    },
     // 自定义env目录
     envDir: "./env",
     resolve: {
@@ -32,7 +37,7 @@
       hmr: true,
       host: true,
       open: true,
-      proxy: createViteProxy(env)
+      proxy: createViteProxy(env,ENV_NAME)
     },
     // 设置scss的api类型为modern-compiler
     css: {

--
Gitblit v1.9.3