| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 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<Store.GlobalStorage>{
- 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<Store.GlobalStorage>{
- constructor() {
- super(sessionStorage, initData)
- }
- /**
- * 获取登录信息
- * @param key
- * @returns
- */
- getLoginInfo = <K extends keyof Proto.LoginRsp>(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')
- }
- })
|