storage.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { queryAllEnums, queryTableDefine } from '@/services/api/common'
  2. import { AppTheme } from '@/constants/theme'
  3. import { Language } from '@/constants/language'
  4. import WebStorage from '@/utils/storage'
  5. import plus from '@/utils/h5plus'
  6. /**
  7. * 初始数据
  8. */
  9. const initData: Store.GlobalStorage = {
  10. appTheme: AppTheme.Default,
  11. lang: Language.ZhCN,
  12. loginInfo: {
  13. AccountIDs: [],
  14. LoginCode: '',
  15. LoginID: 0,
  16. LoginUserType: 0,
  17. AccountStatus: 0,
  18. UserID: 0,
  19. ClientID: 0,
  20. MemberUserID: 0,
  21. Token: '',
  22. },
  23. userMenus: [],
  24. allEnums: [],
  25. tableColumns: [],
  26. }
  27. /**
  28. * 本地存储实例
  29. */
  30. export const localData = new (class extends WebStorage<Store.GlobalStorage>{
  31. constructor() {
  32. super(localStorage, initData)
  33. document.addEventListener('DOMContentLoaded', this.loadTheme, false)
  34. }
  35. /**
  36. * 加载主题
  37. */
  38. private loadTheme = () => {
  39. const theme = this.getValue('appTheme')
  40. this.setStatusBarTheme(theme)
  41. document.documentElement.setAttribute('theme', theme)
  42. document.removeEventListener('DOMContentLoaded', this.loadTheme)
  43. }
  44. /**
  45. * 设置状态栏主题色
  46. * @param theme
  47. */
  48. private setStatusBarTheme = (theme: AppTheme) => {
  49. switch (theme) {
  50. case AppTheme.Default:
  51. case AppTheme.Dark: {
  52. plus.setStatusBarStyle('light')
  53. break
  54. }
  55. default: {
  56. plus.setStatusBarStyle('dark')
  57. }
  58. }
  59. }
  60. /**
  61. * 设置主题
  62. * @param key
  63. */
  64. setTheme = (key: keyof typeof AppTheme) => {
  65. const theme = AppTheme[key]
  66. this.setStatusBarTheme(theme)
  67. document.documentElement.setAttribute('theme', theme)
  68. this.setValue('appTheme', theme)
  69. }
  70. /**
  71. * 重置数据
  72. */
  73. reset = () => {
  74. this.clear('loginInfo', 'userMenus')
  75. }
  76. })
  77. /**
  78. * 会话存储实例
  79. */
  80. export const sessionData = new (class extends WebStorage<Store.GlobalStorage>{
  81. constructor() {
  82. super(sessionStorage, initData)
  83. }
  84. /**
  85. * 获取登录信息
  86. * @param key
  87. * @returns
  88. */
  89. getLoginInfo = <K extends keyof Proto.LoginRsp>(key: K) => {
  90. return this.getValue('loginInfo')[key]
  91. }
  92. /**
  93. * 获取所有枚举列表
  94. * @returns
  95. */
  96. getAllEnumList = () => {
  97. if (this.getValue('allEnums').length) {
  98. return Promise.resolve()
  99. }
  100. return queryAllEnums({
  101. success: (res) => {
  102. this.setValue('allEnums', res.data)
  103. }
  104. })
  105. }
  106. /**
  107. * 获取表格列列表
  108. * @returns
  109. */
  110. getTableColumnList = () => {
  111. if (this.getValue('tableColumns').length) {
  112. return Promise.resolve()
  113. }
  114. return queryTableDefine({
  115. data: {
  116. tableType: 2
  117. },
  118. success: (res) => {
  119. this.setValue('tableColumns', res.data)
  120. }
  121. })
  122. }
  123. /**
  124. * 重置数据
  125. */
  126. reset = () => {
  127. this.clear('loginInfo', 'userMenus')
  128. }
  129. })