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
12
import { updateHeaderBgColor, updateSidebarBgColor } from '/@/logics/theme/updateBackground';
import { updateColorWeak } from '/@/logics/theme/updateColorWeak';
import { updateGrayMode } from '/@/logics/theme/updateGrayMode';
import { changeTheme } from '/@/logics/theme';
13
14
import { appStore } from '/@/store/modules/app';
Vben
authored
4 years ago
15
16
17
18
import { localeStore } from '/@/store/modules/locale';
import { getCommonStoragePrefix, getStorageShortName } from '/@/utils/env';
vben
authored
4 years ago
19
import { primaryColor } from '../../build/config/themeConfig';
Vben
authored
4 years ago
20
21
import { Persistent } from '/@/utils/cache/persistent';
import { deepMerge } from '/@/utils';
22
vben
authored
5 years ago
23
// Initial project configuration
vben
authored
5 years ago
24
export function initAppConfigStore() {
Vben
authored
4 years ago
25
26
let projCfg: ProjectConfig = Persistent.getLocal(PROJ_CFG_KEY) as ProjectConfig;
projCfg = deepMerge(projectSetting, projCfg || {});
27
try {
vben
authored
5 years ago
28
29
30
const {
colorWeak,
grayMode,
vben
authored
4 years ago
31
themeColor,
vben
authored
5 years ago
32
33
headerSetting: { bgColor: headerBgColor } = {},
menuSetting: { bgColor } = {},
Vben
authored
4 years ago
34
} = projCfg;
vben
authored
4 years ago
35
36
37
if (themeColor && themeColor !== primaryColor) {
changeTheme(themeColor);
}
vben
authored
5 years ago
38
headerBgColor && updateHeaderBgColor(headerBgColor);
vben
authored
5 years ago
39
bgColor && updateSidebarBgColor(bgColor);
40
41
42
43
44
grayMode && updateGrayMode(grayMode);
colorWeak && updateColorWeak(colorWeak);
} catch (error) {
console.log(error);
}
Vben
authored
4 years ago
45
appStore.commitProjectConfigState(projCfg);
Vben
authored
4 years ago
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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);
}
});
});
68
}