auth-web/src/config/index.ts

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-09-29 10:47:37 +08:00
import axios from 'axios';
import type { App } from 'vue';
2024-09-26 09:38:02 +08:00
let config: object = {};
2024-10-24 10:42:44 +08:00
const { VITE_BASE_API } = import.meta.env;
2024-09-26 09:38:02 +08:00
const setConfig = (cfg?: unknown) => {
2025-04-24 13:43:37 +08:00
config = Object.assign(config, cfg);
2024-09-26 09:38:02 +08:00
};
const getConfig = (key?: string): PlatformConfigs => {
2025-04-24 13:43:37 +08:00
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;
2024-09-26 09:38:02 +08:00
};
/** 获取项目动态全局配置 */
export const getPlatformConfig = async (app: App): Promise<undefined> => {
2025-04-24 13:43:37 +08:00
app.config.globalProperties.$config = getConfig();
return axios({
method: 'get',
2025-04-27 22:16:06 +08:00
url: `${VITE_BASE_API}/config/public/webConfig`,
2025-04-24 13:43:37 +08:00
})
.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 '请在后端文件夹下添加platform-config.json配置文件';
});
2024-09-26 09:38:02 +08:00
};
/** 本地响应式存储的命名空间 */
const responsiveStorageNameSpace = () => getConfig().ResponsiveStorageNameSpace;
export { getConfig, setConfig, responsiveStorageNameSpace };