storage.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { AppTheme } from '@/constants/enum/theme'
  2. import { Language } from '@/constants/enum/language'
  3. import WebStorage from '@/utils/storage'
  4. import plus from '@/utils/h5plus'
  5. /**
  6. * 初始数据
  7. */
  8. const initData: Store.GlobalStorage = {
  9. lang: Language.ZhCN,
  10. loginInfo: {
  11. AccountIDs: [],
  12. LoginCode: '',
  13. LoginID: 0,
  14. LoginUserType: 0,
  15. AccountStatus: 0,
  16. UserID: 0,
  17. ClientID: 0,
  18. MemberUserID: 0,
  19. Token: '',
  20. },
  21. bankSignDetail: '',
  22. cusBank: '',
  23. errorInfos: '',
  24. allEnums: '',
  25. errorCodes: [],
  26. appTheme: AppTheme.Default,
  27. rowNumber: '',
  28. menus: [],
  29. }
  30. export default new (class {
  31. private localData = new WebStorage(localStorage, initData) // 本地存储实例
  32. private sessionData = new WebStorage(sessionStorage, initData) // 会话存储实例
  33. constructor() {
  34. document.addEventListener('DOMContentLoaded', this.loadTheme, false)
  35. }
  36. /**
  37. * 加载主题
  38. */
  39. private loadTheme = () => {
  40. const theme = this.localData.getValue('appTheme')
  41. this.setStatusBarTheme(theme)
  42. document.documentElement.setAttribute('theme', theme)
  43. document.removeEventListener('DOMContentLoaded', this.loadTheme)
  44. }
  45. /**
  46. * 设置状态栏主题色
  47. * @param theme
  48. */
  49. private setStatusBarTheme = (theme: AppTheme) => {
  50. switch (theme) {
  51. case AppTheme.Default:
  52. case AppTheme.Dark: {
  53. plus.setStatusBarStyle('light')
  54. break
  55. }
  56. default: {
  57. plus.setStatusBarStyle('dark')
  58. }
  59. }
  60. }
  61. /**
  62. * 获取登录信息
  63. * @param key
  64. * @returns
  65. */
  66. getLoginInfo = <K extends keyof Proto.LoginRsp>(key: K) => {
  67. return this.localData.getValue('loginInfo')[key] || this.sessionData.getValue('loginInfo')[key]
  68. }
  69. setLoginInfo = (value: Proto.LoginRsp) => {
  70. this.sessionData.setValue('loginInfo', value)
  71. }
  72. getAccountMenus = () => {
  73. return this.sessionData.getValue('menus')
  74. }
  75. setAccountMenus = (value: Ermcp.AccountMenu[]) => {
  76. return this.sessionData.setValue('menus', value)
  77. }
  78. /**
  79. * 获取当前主题
  80. * @returns
  81. */
  82. getTheme = () => {
  83. return this.localData.getRef('appTheme')
  84. }
  85. /**
  86. * 设置主题
  87. * @param key
  88. */
  89. setTheme = (key: keyof typeof AppTheme) => {
  90. const theme = AppTheme[key]
  91. this.setStatusBarTheme(theme)
  92. document.documentElement.setAttribute('theme', theme)
  93. this.localData.setValue('appTheme', theme)
  94. }
  95. getLanguage = () => {
  96. return this.localData.getValue('lang')
  97. }
  98. setLanguage = (lang: Language) => {
  99. this.localData.setValue('lang', lang)
  100. }
  101. /**
  102. * 重置数据
  103. */
  104. reset = () => {
  105. this.localData.clear()
  106. this.sessionData.clear()
  107. }
  108. })