user.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { defineStore } from 'pinia';
  2. import { clone, cloneDeep } from 'lodash-es';
  3. import { getBaseInfo, getUserInfo } from '@/api/user';
  4. import { smsLogin, passwordLogin, logout } from '@/api/common'
  5. import { closeAuthModal } from '@/hooks/useModal'
  6. // 默认用户信息
  7. const defaultBaseInfo = {
  8. avatar: '', // 头像
  9. nickname: '', // 昵称
  10. gender: 0, // 性别
  11. mobile: '', // 手机号
  12. point: 0, // 积分
  13. };
  14. // 默认账户信息
  15. const defaultAccountInfo = {
  16. accessToken: '',
  17. expiresTime: '',
  18. openid: '',
  19. refreshToken: '',
  20. userId: ''
  21. }
  22. const tabUrl = [
  23. 'pages/index/position',
  24. 'pages/index/communicate',
  25. 'pages/index/my'
  26. ]
  27. export const userStore = defineStore({
  28. id: 'user',
  29. state: () => ({
  30. baseInfo: {}, // 用户信息
  31. userInfo: {},
  32. isLogin: !!uni.getStorageSync('token'), // 登录状态
  33. refreshToken: uni.getStorageSync('refresh-token'), // 用户切换
  34. lastUpdateTime: 0, // 上次更新时间
  35. accountInfo: cloneDeep(defaultAccountInfo), // 账号信息
  36. }),
  37. actions: {
  38. setLogin (val) {
  39. this.isLogin = val
  40. },
  41. // 登录
  42. async handleSmsLogin (query, type, route) {
  43. const api = type ? smsLogin : passwordLogin
  44. const { data, code } = await api(query)
  45. if (code === 0) {
  46. uni.showToast({
  47. title: '登录成功'
  48. })
  49. }
  50. this.accountInfo = data
  51. this.getInfo()
  52. this.getUserInfo()
  53. closeAuthModal()
  54. // 登录成功后的跳转地址
  55. // if (tabUrl.includes(route)) {
  56. // uni.switchTab({
  57. // url: '/' + route
  58. // })
  59. // } else {
  60. // uni.navigateTo({
  61. // url: '/' + route
  62. // })
  63. // }
  64. // uni.switchTab({
  65. // url: '/pages/index/position'
  66. // })
  67. },
  68. // 获取人才信息
  69. async getInfo() {
  70. const { code, data } = await getBaseInfo({ userId: this.accountInfo.userId });
  71. if (code !== 0) {
  72. return;
  73. }
  74. this.baseInfo = data;
  75. return Promise.resolve(data);
  76. },
  77. // 获取用户信息
  78. async getUserInfo() {
  79. const { code, data } = await getUserInfo({ id: this.accountInfo.userId });
  80. if (code !== 0) {
  81. return;
  82. }
  83. this.userInfo = data;
  84. return Promise.resolve(data);
  85. },
  86. // 设置 token
  87. setToken(token = '', refreshToken = '') {
  88. if (token === '') {
  89. this.isLogin = false;
  90. this.refreshToken = ''
  91. uni.removeStorageSync('token');
  92. uni.removeStorageSync('refresh-token');
  93. } else {
  94. this.isLogin = true;
  95. uni.setStorageSync('token', token);
  96. this.refreshToken = refreshToken
  97. uni.setStorageSync('refresh-token', refreshToken);
  98. this.loginAfter();
  99. }
  100. return this.isLogin;
  101. },
  102. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  103. async updateUserData() {
  104. if (!this.isLogin) {
  105. this.resetUserData();
  106. return;
  107. }
  108. // 防抖,5 秒之内不刷新
  109. const nowTime = new Date().getTime();
  110. if (this.lastUpdateTime + 5000 > nowTime) {
  111. return;
  112. }
  113. this.lastUpdateTime = nowTime;
  114. // 获取最新信息
  115. return this.baseInfo;
  116. },
  117. // 重置用户默认数据
  118. resetUserData() {
  119. // 清空 token
  120. this.setToken();
  121. // 清空用户相关的缓存
  122. this.baseInfo = clone(defaultBaseInfo);
  123. this.userInfo = {}
  124. this.accountInfo = cloneDeep(defaultAccountInfo);
  125. },
  126. // 登录后,加载各种信息
  127. async loginAfter() {
  128. await this.updateUserData();
  129. },
  130. // 登出系统
  131. async handleLogout() {
  132. await logout()
  133. this.resetUserData();
  134. return !this.isLogin;
  135. },
  136. },
  137. persist: {
  138. // enabled: true,
  139. // strategies: [
  140. // {
  141. // key: 'user-store'
  142. // }
  143. // ]
  144. storage: {
  145. setItem(key, value) {
  146. uni.setStorageSync(key, value)
  147. },
  148. getItem(key) {
  149. return uni.getStorageSync(key)
  150. },
  151. },
  152. }
  153. })