From f9e5a33590ffc5a93e29f031a389f434813f589b Mon Sep 17 00:00:00 2001 From: old burden Date: Fri, 17 Apr 2026 14:20:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8B=86=E5=88=86=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- portal-ui/.env | 12 +++--- portal-ui/.env.development | 7 ++-- portal-ui/.env.development.local | 3 -- portal-ui/src/utils/request.js | 7 +++- portal-ui/vite.config.js | 3 +- portal-ui/vite.dev.config.js | 65 +++++++++++++++++++++++--------- 6 files changed, 66 insertions(+), 31 deletions(-) delete mode 100644 portal-ui/.env.development.local diff --git a/portal-ui/.env b/portal-ui/.env index 8fa58e4..e280e04 100644 --- a/portal-ui/.env +++ b/portal-ui/.env @@ -1,7 +1,7 @@ # 接口地址 -VITE_AMAP_KEY = 6888d721d30981b479ecdabd85f286fe -VITE_AMAP_SAFE_KEY = 23d3fdbc6b4eaf65d4a82a16510938c9 -VITE_REGISTER_SOURCE = ALL -#VITE_REGISTER_SOURCE = XM001 -VITE_SHOW_PAY = SUCCESS -VITE_API_URL = http://47.86.170.114:8011/api \ No newline at end of file +VITE_AMAP_KEY=6888d721d30981b479ecdabd85f286fe +VITE_AMAP_SAFE_KEY=23d3fdbc6b4eaf65d4a82a16510938c9 +VITE_REGISTER_SOURCE=ALL +#VITE_REGISTER_SOURCE=XM001 +VITE_SHOW_PAY=SUCCESS +VITE_API_URL=http://111.230.37.169:10004/api \ No newline at end of file diff --git a/portal-ui/.env.development b/portal-ui/.env.development index a082422..1422a80 100644 --- a/portal-ui/.env.development +++ b/portal-ui/.env.development @@ -1,3 +1,4 @@ -# 端口 -VITE_PORT = 8887 -VITE_API_URL = http://47.86.170.114:8011/api \ No newline at end of file +# 端口(等号两侧不要空格,避免解析出带空格的 target) +VITE_PORT=8887 +# 仅 dev 时给 vite 代理用;若与 .env.development.local 冲突,以 .local 为准 +VITE_API_URL=http://47.86.170.114:8011/api \ No newline at end of file diff --git a/portal-ui/.env.development.local b/portal-ui/.env.development.local deleted file mode 100644 index 1cb79ed..0000000 --- a/portal-ui/.env.development.local +++ /dev/null @@ -1,3 +0,0 @@ -# 是否生成打包分析图表 -VITE_VISUALIZER = false -VITE_API_URL = https://api.undressing.name/api \ No newline at end of file diff --git a/portal-ui/src/utils/request.js b/portal-ui/src/utils/request.js index cb64eaa..68de8da 100644 --- a/portal-ui/src/utils/request.js +++ b/portal-ui/src/utils/request.js @@ -20,8 +20,13 @@ const messages = i18n.global.messages[lang]; * @constant * @desc 创建axios实例 */ +// Vite 仅注入 import.meta.env(VITE_*);勿用 process.env.VUE_APP_BASE_API(Vue CLI) +// 开发:走相对路径 + vite 代理 /dev-api;生产:直连 .env 中的 VITE_API_URL(与后端同域时可留空并在构建里写死路径) const service = axios.create({ - baseURL: process.env.VUE_APP_BASE_API, + baseURL: + import.meta.env.MODE === 'development' + ? '' + : (import.meta.env.VITE_API_URL || ''), withCredentials: import.meta.env.MODE === 'development', timeout: 600 * 1000 }) diff --git a/portal-ui/vite.config.js b/portal-ui/vite.config.js index 3bbb5f0..056d988 100644 --- a/portal-ui/vite.config.js +++ b/portal-ui/vite.config.js @@ -26,7 +26,8 @@ export default defineConfig({ less: { modifyVars: { '@size-9': '40px', - 'arcoblue-6': '#e6217a' + // 主色:青霓虹(替代原品红),与暗色壳层搭配 + 'arcoblue-6': '#22d3ee' } } } diff --git a/portal-ui/vite.dev.config.js b/portal-ui/vite.dev.config.js index 50797cc..deff39f 100644 --- a/portal-ui/vite.dev.config.js +++ b/portal-ui/vite.dev.config.js @@ -1,24 +1,55 @@ import { defineConfig, mergeConfig, loadEnv } from 'vite' +import { dirname, resolve } from 'path' +import { fileURLToPath } from 'url' import baseViteConfig from './vite.config' -const env = loadEnv('development', process.cwd()) +const __dirname = dirname(fileURLToPath(import.meta.url)) + +/** 代理只能用「协议+主机+端口」,不能带 path;否则转发路径容易错 */ +function devProxyTarget(apiBaseUrl) { + if (!apiBaseUrl || typeof apiBaseUrl !== 'string') { + return '' + } + try { + const u = new URL(apiBaseUrl) + return `${u.protocol}//${u.host}` + } catch { + return '' + } +} + +// 与 portal-ui 根目录一致加载 .env*(ESM 下必须用 import.meta.url,不能依赖未定义的 __dirname) +const env = loadEnv('development', resolve(__dirname)) + +const apiBase = env.VITE_API_URL?.trim() || '' +const proxyTarget = devProxyTarget(apiBase) + +if (!proxyTarget) { + console.warn( + '[vite] VITE_API_URL 未配置或无效,/dev-api 代理未启用。请在 .env.development 中设置,例如:VITE_API_URL=http://47.86.170.114:8011/api' + ) +} const devConfig = defineConfig({ - server: { - host: env.VITE_HOST || 'localhost', - port: env.VITE_PORT || 8081, - open: true, - watch: { - ignored: [/node_modules/, /dist/, /deploy/] - }, - proxy: { - '/dev-api': { - target: env.VITE_API_URL, - ws: false, - changeOrigin: true, - rewrite: (path) => path.replace(/^\/dev-api/, '') - } - } - } + server: { + host: env.VITE_HOST || 'localhost', + port: Number(env.VITE_PORT) || 8081, + open: true, + watch: { + ignored: [/node_modules/, /dist/, /deploy/] + }, + ...(proxyTarget + ? { + proxy: { + '/dev-api': { + target: proxyTarget, + ws: false, + changeOrigin: true, + rewrite: (path) => path.replace(/^\/dev-api/, '') + } + } + } + : {}) + } }) export default mergeConfig(baseViteConfig, devConfig)