8
0

index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { defineStore } from 'pinia'
  2. import pinia from '~src/store/init'
  3. import website from '~src/config/index'
  4. import logoIcon from '~src/assets/logo/icon.png'
  5. import { clearStoreAll } from 'hc-vue3-ui'
  6. import { getStoreValue, setStoreValue } from '~src/utils/storage'
  7. import {
  8. removeRefreshToken,
  9. removeToken,
  10. setRefreshToken,
  11. setToken,
  12. } from '~src/api/auth'
  13. export const useAppStore = defineStore('main', {
  14. state: () => ({
  15. //系统信息
  16. title: getStoreValue('title') || website.title,
  17. logoIcon: getStoreValue('logoIcon') || logoIcon,
  18. logoName: getStoreValue('logoName') || website.name,
  19. //主题信息
  20. theme: getStoreValue('theme') || website.theme,
  21. color: getStoreValue('color') || website.color,
  22. //用户信息
  23. token: getStoreValue('token') || '',
  24. refreshToken: getStoreValue('refreshToken') || '',
  25. tenantId: getStoreValue('tenantId') || '',
  26. userInfo: getStoreValue('userInfo') || {},
  27. //菜单信息
  28. menus: getStoreValue('menus') || [],
  29. buttons: getStoreValue('buttons') || {},
  30. //其他配置信息
  31. isCollapse: getStoreValue('isCollapse') || false, //菜单折叠
  32. //表单显示配置
  33. isShowName: getStoreValue('isShowName') || true, //表单显示名称
  34. //表单改变值Id
  35. tableId: getStoreValue('tableId') || true,
  36. }),
  37. getters: {
  38. //系统信息
  39. getTitle: (state) => state.title,
  40. getLogoIcon: (state) => state.logoIcon,
  41. getLogoName: (state) => state.logoName,
  42. //主题信息
  43. getTheme: (state) => state.theme,
  44. getColor: (state) => state.color,
  45. //用户信息
  46. getToken: (state) => state.token,
  47. getRefreshToken: (state) => state.refreshToken,
  48. getTenantId: (state) => state.tenantId,
  49. getUserInfo: (state) => state.userInfo,
  50. //菜单信息
  51. getMenus: (state) => state.menus,
  52. getButtons: (state) => state.buttons,
  53. //其他配置信息
  54. getCollapse: (state) => state.isCollapse,
  55. //表单显示配置
  56. getIsShowName: (state) => state.isShowName,
  57. getTableId: (state) => state.tableId,
  58. },
  59. actions: {
  60. //系统信息
  61. setTitle(value) {
  62. this.title = value
  63. setStoreValue('title', value)
  64. },
  65. setLogoIcon(value) {
  66. this.logoIcon = value
  67. setStoreValue('logoIcon', value)
  68. },
  69. setLogoName(value) {
  70. this.logoName = value
  71. setStoreValue('logoName', value)
  72. },
  73. //主题信息
  74. setTheme(value) {
  75. this.theme = value
  76. setStoreValue('theme', value)
  77. },
  78. setColor(value) {
  79. this.color = value
  80. setStoreValue('color', value)
  81. },
  82. //用户信息
  83. setTokenVal(value) {
  84. this.token = value
  85. setToken(value)
  86. setStoreValue('token', value)
  87. },
  88. setRefreshTokenVal(value) {
  89. this.refreshToken = value
  90. setRefreshToken(value)
  91. setStoreValue('refreshToken', value)
  92. },
  93. setTenantId(value) {
  94. this.tenantId = value
  95. setStoreValue('tenantId', value)
  96. },
  97. setUserInfo(value) {
  98. this.userInfo = value
  99. setStoreValue('userInfo', value)
  100. },
  101. //菜单信息
  102. setMenus(value) {
  103. this.menus = value
  104. setStoreValue('menus', value)
  105. },
  106. setButtons(value) {
  107. this.buttons = value
  108. setStoreValue('buttons', value)
  109. },
  110. getButtonsVal(value) {
  111. return this.buttons[value] || false
  112. },
  113. //其他配置信息
  114. setCollapse(value) {
  115. //菜单折叠
  116. this.isCollapse = value
  117. setStoreValue('isCollapse', value)
  118. },
  119. //表单显示
  120. setIsShowName(value) {
  121. this.isShowName = value
  122. setStoreValue('isShowName', value)
  123. },
  124. setTableId(value) {
  125. this.tableId = value
  126. setStoreValue('tableId', value)
  127. },
  128. //清除缓存和token
  129. clearStoreData() {
  130. this.title = null
  131. this.logoIcon = null
  132. this.logoName = null
  133. this.theme = website.theme
  134. this.color = website.color
  135. this.token = null
  136. this.refreshToken = null
  137. this.tenantId = null
  138. this.userInfo = null
  139. this.menus = null
  140. this.buttons = null
  141. this.isCollapse = false
  142. this.isShowName = true
  143. this.tableId = ''
  144. //清除缓存
  145. clearStoreAll()
  146. removeToken()
  147. removeRefreshToken()
  148. },
  149. },
  150. })
  151. export default function useUserStoreWidthOut() {
  152. return useAppStore(pinia)
  153. }