perf: 增加全局配置本地加载的控制

This commit is contained in:
levius 2024-06-19 13:09:48 +08:00
parent 13e7a13e9d
commit 36143438f6
3 changed files with 31 additions and 18 deletions

View File

@ -38,6 +38,7 @@ const pathResolve = (dir = ".", metaUrl = import.meta.url) => {
/** 设置别名 */ /** 设置别名 */
const alias: Record<string, string> = { const alias: Record<string, string> = {
"@": pathResolve("../src"), "@": pathResolve("../src"),
"@public": pathResolve("../public"),
"@build": pathResolve() "@build": pathResolve()
}; };

View File

@ -1,5 +1,6 @@
import axios from "axios"; import axios from "axios";
import type { App } from "vue"; import type { App } from "vue";
import platformConfig from "@public/platform-config.json";
let config: object = {}; let config: object = {};
const { VITE_PUBLIC_PATH } = import.meta.env; const { VITE_PUBLIC_PATH } = import.meta.env;
@ -27,26 +28,34 @@ const getConfig = (key?: string): PlatformConfigs => {
}; };
/** 获取项目动态全局配置 */ /** 获取项目动态全局配置 */
export const getPlatformConfig = async (app: App): Promise<undefined> => { export const getPlatformConfig = async (
app: App,
local = false
): Promise<undefined> => {
app.config.globalProperties.$config = getConfig(); app.config.globalProperties.$config = getConfig();
return axios({ const fn = ({ data: config }) => {
method: "get", let $config = app.config.globalProperties.$config;
url: `${VITE_PUBLIC_PATH}platform-config.json` // 自动注入系统配置
}) if (app && $config && typeof config === "object") {
.then(({ data: config }) => { $config = Object.assign($config, config);
let $config = app.config.globalProperties.$config; app.config.globalProperties.$config = $config;
// 自动注入系统配置 // 设置全局配置
if (app && $config && typeof config === "object") { setConfig($config);
$config = Object.assign($config, config); }
app.config.globalProperties.$config = $config; return $config;
// 设置全局配置 };
setConfig($config); if (local) {
} return Promise.resolve({ data: platformConfig }).then(fn);
return $config; } else {
return axios({
method: "get",
url: `${VITE_PUBLIC_PATH}platform-config.json`
}) })
.catch(() => { .then(fn)
throw "请在public文件夹下添加platform-config.json配置文件"; .catch(() => {
}); throw "请在public文件夹下添加platform-config.json配置文件";
});
}
}; };
/** 本地响应式存储的命名空间 */ /** 本地响应式存储的命名空间 */

View File

@ -25,6 +25,9 @@
"@/*": [ "@/*": [
"src/*" "src/*"
], ],
"@public/*": [
"public/*"
],
"@build/*": [ "@build/*": [
"build/*" "build/*"
] ]