vben
authored
5 years ago
1
2
3
/**
* Application configuration
*/
Vben
authored
4 years ago
4
import type { ProjectConfig } from '/#/config';
vben
authored
5 years ago
5
Vben
authored
4 years ago
6
import { PROJ_CFG_KEY } from '/@/enums/cacheEnum';
7
import projectSetting from '/@/settings/projectSetting';
Vben
authored
4 years ago
8
vben
authored
4 years ago
9
10
11
import { updateHeaderBgColor, updateSidebarBgColor } from '/@/logics/theme/updateBackground';
import { updateColorWeak } from '/@/logics/theme/updateColorWeak';
import { updateGrayMode } from '/@/logics/theme/updateGrayMode';
Vben
authored
4 years ago
12
import { updateDarkTheme } from '/@/logics/theme/dark';
vben
authored
4 years ago
13
import { changeTheme } from '/@/logics/theme';
14
Vben
authored
4 years ago
15
16
import { useAppStore } from '/@/store/modules/app';
import { useLocaleStore } from '/@/store/modules/locale';
Vben
authored
4 years ago
17
18
19
import { getCommonStoragePrefix, getStorageShortName } from '/@/utils/env';
vben
authored
4 years ago
20
import { primaryColor } from '../../build/config/themeConfig';
Vben
authored
4 years ago
21
22
import { Persistent } from '/@/utils/cache/persistent';
import { deepMerge } from '/@/utils';
Vben
authored
4 years ago
23
import { ThemeEnum } from '/@/enums/appEnum';
24
vben
authored
5 years ago
25
// Initial project configuration
vben
authored
5 years ago
26
export function initAppConfigStore() {
Vben
authored
4 years ago
27
28
const localeStore = useLocaleStore();
const appStore = useAppStore();
Vben
authored
4 years ago
29
30
let projCfg: ProjectConfig = Persistent.getLocal(PROJ_CFG_KEY) as ProjectConfig;
projCfg = deepMerge(projectSetting, projCfg || {});
Vben
authored
4 years ago
31
32
33
34
35
36
37
38
39
const darkMode = appStore.getDarkMode;
const {
colorWeak,
grayMode,
themeColor,
headerSetting: { bgColor: headerBgColor } = {},
menuSetting: { bgColor } = {},
} = projCfg;
40
try {
vben
authored
4 years ago
41
42
43
if (themeColor && themeColor !== primaryColor) {
changeTheme(themeColor);
}
Vben
authored
4 years ago
44
45
46
47
48
49
grayMode && updateGrayMode(grayMode);
colorWeak && updateColorWeak(colorWeak);
} catch (error) {
console.log(error);
}
Vben
authored
4 years ago
50
appStore.setProjectConfig(projCfg);
Vben
authored
4 years ago
51
52
53
54
55
56
57
58
59
60
61
// init dark mode
updateDarkTheme(darkMode);
if (darkMode === ThemeEnum.DARK) {
updateHeaderBgColor();
updateSidebarBgColor();
} else {
headerBgColor && updateHeaderBgColor(headerBgColor);
bgColor && updateSidebarBgColor(bgColor);
}
// init store
Vben
authored
4 years ago
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
localeStore.initLocale();
setTimeout(() => {
clearObsoleteStorage();
}, 16);
}
/**
* As the version continues to iterate, there will be more and more cache keys stored in localStorage.
* This method is used to delete useless keys
*/
export function clearObsoleteStorage() {
const commonPrefix = getCommonStoragePrefix();
const shortPrefix = getStorageShortName();
[localStorage, sessionStorage].forEach((item: Storage) => {
Object.keys(item).forEach((key) => {
if (key && key.startsWith(commonPrefix) && !key.startsWith(shortPrefix)) {
item.removeItem(key);
}
});
});
84
}