user.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { defineStore } from 'pinia';
  2. import { smsLogin, passwordLogin, logout } from '@/api/common'
  3. // 默认账户信息
  4. const defaultAccountInfo = {
  5. accessToken: '',
  6. expiresTime: '',
  7. openid: '',
  8. refreshToken: '',
  9. userId: ''
  10. }
  11. export const userStore = defineStore('user', {
  12. state: () => {
  13. const userLocal = uni.getStorageSync('user')
  14. const userInfo = userLocal ? JSON.parse(userLocal) : {}
  15. return {
  16. phone: null,
  17. baseInfo: userInfo.baseInfo ?? {}, // 用户信息
  18. userInfo: userInfo.userInfo ?? {},
  19. isLogin: !!uni.getStorageSync('token'), // 登录状态
  20. refreshToken: uni.getStorageSync('refresh-token'), // 用户切换
  21. lastUpdateTime: 0, // 上次更新时间
  22. accountInfo: { ...defaultAccountInfo }, // 账号信息
  23. userAccountInfo: userInfo.userAccountInfo ?? {}, // 账户信息
  24. }
  25. },
  26. actions: {
  27. setLogin (val) {
  28. this.isLogin = val
  29. },
  30. // 登录
  31. async handleSmsLogin (query, index = 0, isGetData = true) {
  32. this.phone = query.phone
  33. const apiList = [smsLogin, passwordLogin]
  34. const { data, code } = await apiList[index](query)
  35. if (code === 0) {
  36. uni.showToast({
  37. title: '登录成功'
  38. })
  39. }
  40. this.accountInfo = data
  41. },
  42. // 设置 token
  43. setToken(token = '', refreshToken = '') {
  44. if (token === '') {
  45. this.isLogin = false;
  46. this.refreshToken = ''
  47. uni.removeStorageSync('token');
  48. uni.removeStorageSync('refresh-token');
  49. } else {
  50. this.isLogin = true;
  51. uni.setStorageSync('token', token);
  52. this.refreshToken = refreshToken
  53. uni.setStorageSync('refresh-token', refreshToken);
  54. this.loginAfter();
  55. }
  56. return this.isLogin;
  57. },
  58. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  59. async updateUserData() {
  60. if (!this.isLogin) {
  61. this.resetUserData();
  62. return;
  63. }
  64. // 防抖,5 秒之内不刷新
  65. const nowTime = new Date().getTime();
  66. if (this.lastUpdateTime + 5000 > nowTime) {
  67. return;
  68. }
  69. this.lastUpdateTime = nowTime;
  70. },
  71. // 重置用户默认数据
  72. resetUserData() {
  73. // 清空 token
  74. this.setToken();
  75. // 清空用户相关的缓存
  76. this.userInfo = {}
  77. this.phone = ''
  78. this.userAccountInfo = {}
  79. this.accountInfo = { ...defaultAccountInfo };
  80. },
  81. // 登录后,加载各种信息
  82. async loginAfter() {
  83. await this.updateUserData();
  84. },
  85. // 登出系统
  86. async handleLogout() {
  87. try {
  88. await logout(uni.getStorageSync('token'))
  89. this.resetUserData()
  90. } catch (error) {
  91. console.log('handleLogout:error', error)
  92. // if (error?.msg === '企业登录授权错误,请重新登录')
  93. this.resetUserData()
  94. }
  95. // return !this.isLogin;
  96. }
  97. },
  98. persist: {
  99. storage: {
  100. setItem(key, value) {
  101. uni.setStorageSync(key, value)
  102. },
  103. getItem(key) {
  104. return uni.getStorageSync(key)
  105. },
  106. },
  107. }
  108. })