import { AppTheme } from '@/constants/enum/theme' import { Language } from '@/constants/enum/language' import WebStorage from '@/utils/storage' import plus from '@/utils/h5plus' /** * 初始数据 */ const initData: Store.GlobalStorage = { lang: Language.ZhCN, loginInfo: { AccountIDs: [], LoginCode: '', LoginID: 0, LoginUserType: 0, AccountStatus: 0, UserID: 0, ClientID: 0, MemberUserID: 0, Token: '', }, bankSignDetail: '', cusBank: '', errorInfos: '', allEnums: '', errorCodes: [], appTheme: AppTheme.Default, rowNumber: '', menus: [], } export default new (class { private localData = new WebStorage(localStorage, initData) // 本地存储实例 private sessionData = new WebStorage(sessionStorage, initData) // 会话存储实例 constructor() { document.addEventListener('DOMContentLoaded', this.loadTheme, false) } /** * 加载主题 */ private loadTheme = () => { const theme = this.localData.getValue('appTheme') this.setStatusBarTheme(theme) document.documentElement.setAttribute('theme', theme) document.removeEventListener('DOMContentLoaded', this.loadTheme) } /** * 设置状态栏主题色 * @param theme */ private setStatusBarTheme = (theme: AppTheme) => { switch (theme) { case AppTheme.Default: case AppTheme.Dark: { plus.setStatusBarStyle('light') break } default: { plus.setStatusBarStyle('dark') } } } /** * 获取登录信息 * @param key * @returns */ getLoginInfo = (key: K) => { return this.localData.getValue('loginInfo')[key] || this.sessionData.getValue('loginInfo')[key] } setLoginInfo = (value: Proto.LoginRsp) => { this.sessionData.setValue('loginInfo', value) } getAccountMenus = () => { return this.sessionData.getValue('menus') } setAccountMenus = (value: Ermcp.AccountMenu[]) => { return this.sessionData.setValue('menus', value) } /** * 获取当前主题 * @returns */ getTheme = () => { return this.localData.getRef('appTheme') } /** * 设置主题 * @param key */ setTheme = (key: keyof typeof AppTheme) => { const theme = AppTheme[key] this.setStatusBarTheme(theme) document.documentElement.setAttribute('theme', theme) this.localData.setValue('appTheme', theme) } getLanguage = () => { return this.localData.getValue('lang') } setLanguage = (lang: Language) => { this.localData.setValue('lang', lang) } /** * 重置数据 */ reset = () => { this.localData.clear() this.sessionData.clear() } })