user.ts 833 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * @Author: wjc
  3. * @Date: 2023-11-27 15:35:24
  4. * @LastEditors: LiZhiWei
  5. * @LastEditTime: 2026-01-09 16:45:59
  6. * @Description:
  7. */
  8. import { getToken } from '@/utils/auth'
  9. interface IUser {
  10. id?: string
  11. }
  12. export const useUserStore = defineStore('user', {
  13. state: () => {
  14. return {
  15. userInfo: {} as IUser,
  16. navMenu: [],
  17. }
  18. },
  19. getters: {
  20. getUser(): IUser {
  21. if (process.browser) {
  22. const token = getToken()
  23. const userInfo = localStorage.getItem('userInfo')
  24. if (userInfo && token) {
  25. return JSON.parse(userInfo)
  26. }
  27. return this.userInfo
  28. } else {
  29. return this.userInfo
  30. }
  31. },
  32. getEnterpriseUserId(): string {
  33. return this.userInfo.id || ''
  34. },
  35. },
  36. actions: {
  37. clearUserInfo() {
  38. this.userInfo = {}
  39. },
  40. },
  41. })