user.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { store } from '@/store'
  2. import { defineStore } from 'pinia'
  3. import { getAccessToken, removeToken } from '@/utils/auth'
  4. import { CACHE_KEY, useCache, deleteUserCache } from '@/hooks/web/useCache'
  5. import { getInfo, loginOut } from '@/api/login'
  6. import * as LoginApi from '@/api/login'
  7. const { wsCache } = useCache()
  8. import * as authUtil from '@/utils/auth'
  9. interface UserVO {
  10. id: string
  11. avatar: string
  12. nickname: string
  13. deptId: number | string
  14. appointmentDept: string
  15. employeeNo?: string
  16. mobile?: string
  17. }
  18. interface UserInfoVO {
  19. // USER 缓存
  20. permissions: string[]
  21. roles: string[]
  22. isSetUser: boolean
  23. user: UserVO
  24. }
  25. export const useUserStore = defineStore('admin-user', {
  26. state: (): UserInfoVO => ({
  27. permissions: [],
  28. roles: [],
  29. isSetUser: false,
  30. user: {
  31. id: '',
  32. avatar: '',
  33. appointmentDept: '',
  34. nickname: '',
  35. deptId: '',
  36. mobile: ''
  37. }
  38. }),
  39. getters: {
  40. getPermissions(): string[] {
  41. return this.permissions
  42. },
  43. getRoles(): string[] {
  44. return this.roles
  45. },
  46. getIsSetUser(): boolean {
  47. return this.isSetUser
  48. },
  49. getUser(): UserVO {
  50. return this.user
  51. }
  52. },
  53. actions: {
  54. async setUserInfoAction() {
  55. if (!getAccessToken()) {
  56. this.resetState()
  57. return null
  58. }
  59. let userInfo = wsCache.get(CACHE_KEY.USER)
  60. if (!userInfo) {
  61. userInfo = await getInfo()
  62. }
  63. this.permissions = userInfo.permissions
  64. this.roles = userInfo.roles
  65. this.user = userInfo.user
  66. this.isSetUser = true
  67. wsCache.set(CACHE_KEY.USER, userInfo)
  68. wsCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus)
  69. },
  70. async setUserAppointmentDept(appointmentDept: string) {
  71. this.user = { ...this.user, appointmentDept }
  72. const cachedUserInfo = wsCache.get(CACHE_KEY.USER)
  73. if (cachedUserInfo) {
  74. cachedUserInfo.user.appointmentDept = appointmentDept
  75. wsCache.set(CACHE_KEY.USER, cachedUserInfo) // 重新写入缓存
  76. }
  77. },
  78. async setUserAvatarAction(avatar: string) {
  79. const userInfo = wsCache.get(CACHE_KEY.USER)
  80. // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
  81. this.user.avatar = avatar
  82. userInfo.user.avatar = avatar
  83. wsCache.set(CACHE_KEY.USER, userInfo)
  84. },
  85. async setUserNicknameAction(nickname: string) {
  86. const userInfo = wsCache.get(CACHE_KEY.USER)
  87. // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
  88. this.user.nickname = nickname
  89. userInfo.user.nickname = nickname
  90. wsCache.set(CACHE_KEY.USER, userInfo)
  91. },
  92. async ssoLogin(code) {
  93. try {
  94. const res = await LoginApi.loginCasMineInfoFn({code})
  95. if (res && res.accessToken) {
  96. authUtil.setToken(res)
  97. return res
  98. }
  99. return null
  100. } catch (error) {
  101. console.error('SSO登录失败:', error)
  102. throw error
  103. }
  104. },
  105. async loginOut() {
  106. await loginOut()
  107. removeToken()
  108. deleteUserCache() // 删除用户缓存
  109. this.resetState()
  110. },
  111. resetState() {
  112. this.permissions = []
  113. this.roles = []
  114. this.isSetUser = false
  115. this.user = {
  116. id: '',
  117. appointmentDept: '',
  118. avatar: '',
  119. nickname: '',
  120. deptId: '',
  121. mobile: ''
  122. }
  123. }
  124. }
  125. })
  126. export const useUserStoreWithOut = () => {
  127. return useUserStore(store)
  128. }