userStore.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * @Author: wjc
  3. * @Date: 2024-06-05 17:13:30
  4. * @LastEditors: wjc
  5. * @LastEditTime: 2024-07-12 17:18:01
  6. * @Description:
  7. */
  8. import { defineStore } from 'pinia'
  9. import { useAppStore } from './appStore'
  10. import { UserState, ILogin, LoginRes, Employees } from '@/models/userTypes'
  11. import { login, logout, getUserInfo } from '@/api/userApi'
  12. export const useUserStore = defineStore('user', {
  13. state: (): UserState => {
  14. return {
  15. isPrivacyShowed: false,
  16. isInstall: false,
  17. token: '',
  18. userInfo: new Employees({}),
  19. storageLoginInfo: undefined,
  20. storageUserId: '',
  21. }
  22. },
  23. persist: true,
  24. // persist: {
  25. // paths: ['isPrivacyShowed', 'isInstall', 'storageUserId', 'storageLoginInfo'],
  26. // },
  27. getters: {},
  28. actions: {
  29. loginAction(data: ILogin): Promise<LoginRes> {
  30. return new Promise((resolve, reject) => {
  31. login(data)
  32. .then((res) => {
  33. if (res && res.data) {
  34. this.isPrivacyShowed = true
  35. this.isInstall = true
  36. this.storageLoginInfo = data
  37. this.storageUserId = res.data.id
  38. resolve(res.data)
  39. }
  40. })
  41. .catch((error) => {
  42. reject(error)
  43. })
  44. })
  45. },
  46. logoutAction(): Promise<string> {
  47. const appStore = useAppStore()
  48. return new Promise((resolve, reject) => {
  49. logout()
  50. .then((res) => {
  51. if (res && res.data) {
  52. appStore.setTabbar(0)
  53. this.$reset()
  54. resolve(res.data)
  55. }
  56. })
  57. .catch((error) => {
  58. reject(error)
  59. })
  60. })
  61. },
  62. getUserInfoAction(): Promise<Employees> {
  63. return new Promise((resolve, reject) => {
  64. if (this.storageUserId) {
  65. getUserInfo(this.storageUserId)
  66. .then((res) => {
  67. if (res && res.data) {
  68. this.userInfo = res.data
  69. resolve(res.data)
  70. }
  71. })
  72. .catch((error) => {
  73. reject(error)
  74. })
  75. }
  76. })
  77. },
  78. },
  79. })