user.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { store } from '../index'
  2. import { defineStore } from 'pinia'
  3. import { getAccessToken } from '@/utils/auth'
  4. import { useCache } from '@/hooks/web/useCache'
  5. const { wsCache } = useCache()
  6. interface UserInfoVO {
  7. permissions: []
  8. roles: []
  9. user: {
  10. avatar: string
  11. id: number
  12. nickname: string
  13. }
  14. }
  15. export const useUserStore = defineStore({
  16. id: 'admin-user',
  17. state: (): UserInfoVO => ({
  18. permissions: [],
  19. roles: [],
  20. user: {
  21. id: 0,
  22. avatar: '',
  23. nickname: ''
  24. }
  25. }),
  26. actions: {
  27. async getUserInfoAction(userInfo: UserInfoVO) {
  28. if (!getAccessToken()) {
  29. this.resetState()
  30. return null
  31. }
  32. this.permissions = userInfo.permissions
  33. this.roles = userInfo.roles
  34. this.user = userInfo.user
  35. wsCache.set('user', userInfo)
  36. },
  37. resetState() {
  38. this.permissions = []
  39. this.roles = []
  40. this.user = {
  41. id: 0,
  42. avatar: '',
  43. nickname: ''
  44. }
  45. }
  46. }
  47. })
  48. export const useUserStoreWithOut = () => {
  49. return useUserStore(store)
  50. }