userStore.ts 2.0 KB

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