user.ts 1010 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { defineStore } from 'pinia'
  2. import { StaticUrl } from '@/config'
  3. interface userStroe {
  4. token: string
  5. /**
  6. * 重定向路由名称
  7. */
  8. redirectName: string
  9. /**
  10. * 用户登录信息
  11. */
  12. userInfo: Api.userInfo
  13. }
  14. export const useUserStore = defineStore('user', {
  15. state: (): userStroe => ({
  16. token: '',
  17. redirectName: '',
  18. userInfo: {
  19. id: 0,
  20. },
  21. }),
  22. getters: {
  23. getUserAvatar(): string {
  24. if (this.userInfo.avatarUrl) {
  25. return this.userInfo.avatarUrl
  26. }
  27. return `${StaticUrl}/avator.png`
  28. },
  29. },
  30. actions: {
  31. async getUserInfo() {
  32. if (this.token) {
  33. const res = await api.sys.userInfo({})
  34. this.userInfo = res
  35. }
  36. },
  37. async updataUserInfo(data: Api.userInfo) {
  38. uni.showLoading({ mask: true })
  39. await Apis.sys.updateUserInfo({ pathParams: { memberId: data.id }, data })
  40. uni.hideLoading()
  41. useGlobalToast().show({ msg: '修改成功' })
  42. this.getUserInfo()
  43. },
  44. },
  45. })