mirror of
https://github.com/pure-admin/pure-admin-thin.git
synced 2025-04-24 23:47:17 +08:00
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { App } from "vue";
|
|
import axios from "axios";
|
|
import { configConver } from "@/utils/rootConver";
|
|
|
|
let config: object = {};
|
|
|
|
const setConfig = (cfg?: unknown) => {
|
|
config = Object.assign(config, cfg);
|
|
};
|
|
|
|
const getConfig = (key?: string): ServerConfigs => {
|
|
if (typeof key === "string") {
|
|
const arr = key.split(".");
|
|
if (arr && arr.length) {
|
|
let data = config;
|
|
arr.forEach(v => {
|
|
if (data && typeof data[v] !== "undefined") {
|
|
data = data[v];
|
|
} else {
|
|
data = null;
|
|
}
|
|
});
|
|
return data;
|
|
}
|
|
}
|
|
return config;
|
|
};
|
|
|
|
/** 获取项目动态全局配置 */
|
|
export const getServerConfig = async (app: App): Promise<undefined> => {
|
|
app.config.globalProperties.$config = getConfig();
|
|
return axios({
|
|
method: "get",
|
|
url: `${configConver()}serverConfig.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;
|
|
})
|
|
.catch(() => {
|
|
throw "请在public文件夹下添加serverConfig.json配置文件";
|
|
});
|
|
};
|
|
|
|
/** 本地响应式存储的命名空间 */
|
|
const responsiveStorageNameSpace = () => getConfig().ResponsiveStorageNameSpace;
|
|
|
|
export { getConfig, setConfig, responsiveStorageNameSpace };
|