From 6de35daab177009ae903a42e0c66b8cdca85ae52 Mon Sep 17 00:00:00 2001 From: luoer Date: Wed, 8 Nov 2023 10:03:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=85=8D=E7=BD=AEnginx=E6=94=AF?= =?UTF-8?q?=E6=8C=81web=E8=B7=AF=E7=94=B1=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 ++ src/api/instance/api.ts | 7 +++---- src/config/env.ts | 25 +++++++++++++++++++++++++ src/router/guards/auth.ts | 3 ++- src/router/router/util.ts | 3 ++- src/store/app/index.ts | 6 +++--- vite.config.ts | 4 ++-- 7 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 src/config/env.ts diff --git a/.env b/.env index 5c319d4..55eeb10 100644 --- a/.env +++ b/.env @@ -19,6 +19,8 @@ VITE_HISTORY = web VITE_HOST = 0.0.0.0 # 端口 VITE_PORT = 3020 +# 代理前缀 +VITE_PROXY_PREFIX = /api,/upload # 代理地址 VITE_PROXY = http://127.0.0.1:3030/ # API文档 说明:需返回符合 OPENAPI 规范的json内容 diff --git a/src/api/instance/api.ts b/src/api/instance/api.ts index 35105bb..aa4d078 100644 --- a/src/api/instance/api.ts +++ b/src/api/instance/api.ts @@ -2,13 +2,14 @@ import { Service } from "./service"; import { addToastInterceptor } from "../interceptors/toast"; import { addAuthInterceptor } from "../interceptors/auth"; import { addExceptionInterceptor } from "../interceptors/exception"; +import { env } from "@/config/env"; /** * API 接口实例 * @see src/api/instance/instance.ts */ -const api = new Service({ - baseURL: import.meta.env.VITE_API, +export const api = new Service({ + baseURL: env.apiPrefix, }); /** @@ -25,5 +26,3 @@ addAuthInterceptor(api.instance); * 添加异常处理拦截器 */ addExceptionInterceptor(api.instance, () => api.expireHandler?.()); - -export { api }; diff --git a/src/config/env.ts b/src/config/env.ts new file mode 100644 index 0000000..85840b9 --- /dev/null +++ b/src/config/env.ts @@ -0,0 +1,25 @@ +/** + * 环境变量 + */ +export const env = { + /** + * 站点标题 + */ + title: import.meta.env.VITE_TITLE, + /** + * 站点副标题 + */ + subtitle: import.meta.env.VITE_SUBTITLE, + /** + * 接口前缀 + */ + apiPrefix: import.meta.env.VITE_API, + /** + * 路由模式 + */ + historyMode: import.meta.env.VITE_HISTORY, + /** + * 首页路径 + */ + homePath: import.meta.env.VITE_HOME_PATH, +}; diff --git a/src/router/guards/auth.ts b/src/router/guards/auth.ts index 0fdd9cb..4356024 100644 --- a/src/router/guards/auth.ts +++ b/src/router/guards/auth.ts @@ -7,6 +7,7 @@ import { Router } from "vue-router"; import { menus } from "../menus"; import { APP_HOME_NAME } from "../routes/base"; import { APP_ROUTE_NAME, routes } from "../routes/page"; +import { env } from "@/config/env"; const WHITE_LIST = ["/:all(.*)*"]; const UNSIGNIN_LIST = ["/login"]; @@ -49,7 +50,7 @@ export function useAuthGuard(router: Router) { } if (!menuStore.menus.length) { menuStore.setMenus(menus); - menuStore.setHome(import.meta.env.VITE_HOME_PATH); + menuStore.setHome(env.homePath); for (const route of routes) { router.addRoute(route); } diff --git a/src/router/router/util.ts b/src/router/router/util.ts index 2977d24..3c61d32 100644 --- a/src/router/router/util.ts +++ b/src/router/router/util.ts @@ -1,3 +1,4 @@ +import { env } from "@/config/env"; import { createWebHashHistory, createWebHistory } from "vue-router"; /** @@ -12,6 +13,6 @@ const HistoryMap = { * 路由模式 */ export function historyMode() { - const mode = HistoryMap[import.meta.env.VITE_HISTORY]; + const mode = HistoryMap[env.historyMode]; return mode(); } diff --git a/src/store/app/index.ts b/src/store/app/index.ts index 787585b..ac4cb0e 100644 --- a/src/store/app/index.ts +++ b/src/store/app/index.ts @@ -1,11 +1,12 @@ +import { env } from "@/config/env"; import { defineStore } from "pinia"; export const useAppStore = defineStore({ id: "app", state: (): AppStore => ({ isDarkMode: false, - title: import.meta.env.VITE_TITLE, - subtitle: import.meta.env.VITE_SUBTITLE, + title: env.title, + subtitle: env.subtitle, pageLoding: false, pageTags: [], }), @@ -70,7 +71,6 @@ export const useAppStore = defineStore({ } }, }, - persist: !import.meta.env.DEV, }); interface AppStore { diff --git a/vite.config.ts b/vite.config.ts index 8861bd5..d75c35d 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -99,11 +99,11 @@ export default defineConfig(({ mode }) => { }), /** - * 产物分析 + * 提供产物分析报告 * @see https://github.com/btd/rollup-plugin-visualizer */ visualizer({ - title: "产物分析 | 自动生成", + title: `构建统计 | ${env.VITE_SUBTITLE}`, filename: ".gitea/stat.html", }),