From 1a5b583c2f2f6e5d9ac1483419f34a41eeffa002 Mon Sep 17 00:00:00 2001 From: zhangjf Date: Mon, 23 Feb 2026 10:28:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20fund-mobile=E6=94=AF=E6=8C=81Nginx?= =?UTF-8?q?=E5=AD=90=E8=B7=AF=E5=BE=84/fmobile=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增.env.development/.env.production环境配置 - vite.config.ts支持VITE_BASE动态base路径 - router使用import.meta.env.BASE_URL - API baseURL使用环境变量 - 新增vite-env.d.ts类型声明 --- fund-mobile/.env.development | 6 +++ fund-mobile/.env.production | 6 +++ fund-mobile/src/api/request.ts | 2 +- fund-mobile/src/router/index.ts | 2 +- fund-mobile/src/vite-env.d.ts | 10 ++++ fund-mobile/vite.config.ts | 86 ++++++++++++++++++--------------- 6 files changed, 71 insertions(+), 41 deletions(-) create mode 100644 fund-mobile/.env.development create mode 100644 fund-mobile/.env.production create mode 100644 fund-mobile/src/vite-env.d.ts diff --git a/fund-mobile/.env.development b/fund-mobile/.env.development new file mode 100644 index 0000000..68e9a47 --- /dev/null +++ b/fund-mobile/.env.development @@ -0,0 +1,6 @@ +# 开发环境配置 +# 开发模式无部署前缀 +VITE_BASE=/ + +# API基础路径(开发模式使用代理) +VITE_API_BASE_URL= diff --git a/fund-mobile/.env.production b/fund-mobile/.env.production new file mode 100644 index 0000000..c530c4a --- /dev/null +++ b/fund-mobile/.env.production @@ -0,0 +1,6 @@ +# 生产环境配置 +# 部署路径前缀(Nginx路由使用) +VITE_BASE=/fmobile/ + +# API基础路径 +VITE_API_BASE_URL=/fund diff --git a/fund-mobile/src/api/request.ts b/fund-mobile/src/api/request.ts index 7b9b96c..29a4181 100644 --- a/fund-mobile/src/api/request.ts +++ b/fund-mobile/src/api/request.ts @@ -1,7 +1,7 @@ import axios from 'axios' const request = axios.create({ - baseURL: '/fund', + baseURL: import.meta.env.VITE_API_BASE_URL || '/fund', timeout: 15000, headers: { 'Content-Type': 'application/json' diff --git a/fund-mobile/src/router/index.ts b/fund-mobile/src/router/index.ts index 8a8ecc1..a3a3958 100644 --- a/fund-mobile/src/router/index.ts +++ b/fund-mobile/src/router/index.ts @@ -2,7 +2,7 @@ import { createRouter, createWebHistory } from 'vue-router' import { useTenantStore } from '@/stores/tenant' const router = createRouter({ - history: createWebHistory(), + history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', diff --git a/fund-mobile/src/vite-env.d.ts b/fund-mobile/src/vite-env.d.ts new file mode 100644 index 0000000..04f1a77 --- /dev/null +++ b/fund-mobile/src/vite-env.d.ts @@ -0,0 +1,10 @@ +/// + +interface ImportMetaEnv { + readonly VITE_BASE: string + readonly VITE_API_BASE_URL: string +} + +interface ImportMeta { + readonly env: ImportMetaEnv +} diff --git a/fund-mobile/vite.config.ts b/fund-mobile/vite.config.ts index 00576e6..557c401 100644 --- a/fund-mobile/vite.config.ts +++ b/fund-mobile/vite.config.ts @@ -1,47 +1,55 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' import Components from 'unplugin-vue-components/vite' import { VantResolver } from '@vant/auto-import-resolver' import { fileURLToPath, URL } from 'node:url' -export default defineConfig({ - plugins: [ - vue(), - Components({ - resolvers: [VantResolver()] - }) - ], - resolve: { - alias: { - '@': fileURLToPath(new URL('./src', import.meta.url)) - } - }, - server: { - port: 8080, - proxy: { - '/sys': { - target: 'http://localhost:8000', - changeOrigin: true - }, - '/cust': { - target: 'http://localhost:8000', - changeOrigin: true - }, - '/proj': { - target: 'http://localhost:8000', - changeOrigin: true - }, - '/exp': { - target: 'http://localhost:8000', - changeOrigin: true - }, - '/receipt': { - target: 'http://localhost:8000', - changeOrigin: true - }, - '/file': { - target: 'http://localhost:8000', - changeOrigin: true +export default defineConfig(({ mode }) => { + // 加载环境变量 + const env = loadEnv(mode, process.cwd()) + const base = env.VITE_BASE || '/' + + return { + // 部署路径前缀 + base, + plugins: [ + vue(), + Components({ + resolvers: [VantResolver()] + }) + ], + resolve: { + alias: { + '@': fileURLToPath(new URL('./src', import.meta.url)) + } + }, + server: { + port: 8080, + proxy: { + '/sys': { + target: 'http://localhost:8000', + changeOrigin: true + }, + '/cust': { + target: 'http://localhost:8000', + changeOrigin: true + }, + '/proj': { + target: 'http://localhost:8000', + changeOrigin: true + }, + '/exp': { + target: 'http://localhost:8000', + changeOrigin: true + }, + '/receipt': { + target: 'http://localhost:8000', + changeOrigin: true + }, + '/file': { + target: 'http://localhost:8000', + changeOrigin: true + } } } }