From 36143438f677ef96c94d5e6b82cd800f7c9ca229 Mon Sep 17 00:00:00 2001 From: levius Date: Wed, 19 Jun 2024 13:09:48 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=A2=9E=E5=8A=A0=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=9C=AC=E5=9C=B0=E5=8A=A0=E8=BD=BD=E7=9A=84?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/utils.ts | 1 + src/config/index.ts | 45 +++++++++++++++++++++++++++------------------ tsconfig.json | 3 +++ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/build/utils.ts b/build/utils.ts index 3d778fe6d..ccc082b01 100644 --- a/build/utils.ts +++ b/build/utils.ts @@ -38,6 +38,7 @@ const pathResolve = (dir = ".", metaUrl = import.meta.url) => { /** 设置别名 */ const alias: Record = { "@": pathResolve("../src"), + "@public": pathResolve("../public"), "@build": pathResolve() }; diff --git a/src/config/index.ts b/src/config/index.ts index c81d1c4d7..1f31bc46a 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,5 +1,6 @@ import axios from "axios"; import type { App } from "vue"; +import platformConfig from "@public/platform-config.json"; let config: object = {}; const { VITE_PUBLIC_PATH } = import.meta.env; @@ -27,26 +28,34 @@ const getConfig = (key?: string): PlatformConfigs => { }; /** 获取项目动态全局配置 */ -export const getPlatformConfig = async (app: App): Promise => { +export const getPlatformConfig = async ( + app: App, + local = false +): Promise => { app.config.globalProperties.$config = getConfig(); - return axios({ - method: "get", - url: `${VITE_PUBLIC_PATH}platform-config.json` - }) - .then(({ data: config }) => { - let $config = app.config.globalProperties.$config; - // 自动注入系统配置 - if (app && $config && typeof config === "object") { - $config = Object.assign($config, config); - app.config.globalProperties.$config = $config; - // 设置全局配置 - setConfig($config); - } - return $config; + const fn = ({ data: config }) => { + let $config = app.config.globalProperties.$config; + // 自动注入系统配置 + if (app && $config && typeof config === "object") { + $config = Object.assign($config, config); + app.config.globalProperties.$config = $config; + // 设置全局配置 + setConfig($config); + } + return $config; + }; + if (local) { + return Promise.resolve({ data: platformConfig }).then(fn); + } else { + return axios({ + method: "get", + url: `${VITE_PUBLIC_PATH}platform-config.json` }) - .catch(() => { - throw "请在public文件夹下添加platform-config.json配置文件"; - }); + .then(fn) + .catch(() => { + throw "请在public文件夹下添加platform-config.json配置文件"; + }); + } }; /** 本地响应式存储的命名空间 */ diff --git a/tsconfig.json b/tsconfig.json index 5dd960a4e..2b8307ac3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,6 +25,9 @@ "@/*": [ "src/*" ], + "@public/*": [ + "public/*" + ], "@build/*": [ "build/*" ]