sysConfig.ts
1.49 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { getAppKey } from '@/api/modules/queryService'
import { ElMessage } from 'element-plus';
const sysConfigStore = defineStore(
// 唯一ID
'sysConfig',
() => {
let configMap: any = {};
/** 动态appkey值,从接口获取 */
let appKey = '';
const loadingAppKey = () => {
getAppKey().then((res: any) => {
if (res?.code == '00000') {
appKey = res.data;
} else {
ElMessage.error('appKey获取失败');
}
})
}
// 封装请求配置文件的函数
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';
}
const getAppKeyValue = () => {
return appKey;
}
return {
loadingAppKey,
getAppKeyValue,
configMap,
loadConfig,
setConfig,
getConfig
}
},
)
export default sysConfigStore