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> = {
"@": pathResolve("../src"),
"@public": pathResolve("../public"),
"@build": pathResolve()
};

View File

@ -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<undefined> => {
export const getPlatformConfig = async (
app: App,
local = false
): Promise<undefined> => {
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配置文件";
});
}
};
/** 本地响应式存储的命名空间 */

View File

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