sysConfig.ts
1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const sysConfigStore = defineStore(
// 唯一ID
'config',
() => {
let configMap: any = {};
// 封装请求配置文件的函数
const loadConfig = async () => {
try {
const response = await fetch('/config.json');
if (!response.ok) {
throw new Error(`请求配置失败,状态码: ${response.status}`);
}
const config = await response.json();
setConfig(config);
return config;
} catch (error) {
console.error('加载配置时出错:', error);
throw error;
}
};
const setConfig = (val) => {
configMap = val
}
const getConfig = (field) => {
// if (import.meta.env.MODE == 'nginx' || import.meta.env.MODE == 'development') {
// return import.meta.env.VITE_appKey
// }
return (field ? configMap[field] : configMap) || '69afd501e4b08251f6cf6419';
}
return {
configMap,
loadConfig,
setConfig,
getConfig
}
},
)
export default sysConfigStore