import { queryAllEnums, queryTableDefine } from '@/services/api/common' import { AppTheme } from '@/constants/theme' import { Language } from '@/constants/language' import WebStorage from '@/utils/storage' import plus from '@/utils/h5plus' /** * 初始数据 */ const initData: Store.GlobalStorage = { appTheme: AppTheme.Default, lang: Language.ZhCN, loginInfo: { AccountIDs: [], LoginCode: '', LoginID: 0, LoginUserType: 0, AccountStatus: 0, UserID: 0, ClientID: 0, MemberUserID: 0, Token: '', }, userMenus: [], allEnums: [], tableColumns: [], } /** * 本地存储实例 */ export const localData = new (class extends WebStorage{ constructor() { super(localStorage, initData) document.addEventListener('DOMContentLoaded', this.loadTheme, false) } /** * 加载主题 */ private loadTheme = () => { const theme = this.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 */ setTheme = (key: keyof typeof AppTheme) => { const theme = AppTheme[key] this.setStatusBarTheme(theme) document.documentElement.setAttribute('theme', theme) this.setValue('appTheme', theme) } /** * 重置数据 */ reset = () => { this.clear('loginInfo', 'userMenus') } }) /** * 会话存储实例 */ export const sessionData = new (class extends WebStorage{ constructor() { super(sessionStorage, initData) } /** * 获取登录信息 * @param key * @returns */ getLoginInfo = (key: K) => { return this.getValue('loginInfo')[key] } /** * 获取所有枚举列表 * @returns */ getAllEnumList = () => { if (this.getValue('allEnums').length) { return Promise.resolve() } return queryAllEnums({ success: (res) => { this.setValue('allEnums', res.data) } }) } /** * 获取表格列列表 * @returns */ getTableColumnList = () => { if (this.getValue('tableColumns').length) { return Promise.resolve() } return queryTableDefine({ data: { tableType: 2 }, success: (res) => { this.setValue('tableColumns', res.data) } }) } /** * 重置数据 */ reset = () => { this.clear('loginInfo', 'userMenus') } })