user.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. lastUpdateTime: 0, // 上次更新时间
  34. accountInfo: cloneDeep(defaultAccountInfo), // 账号信息
  35. }),
  36. actions: {
  37. setLogin (val) {
  38. this.isLogin = val
  39. },
  40. // 登录
  41. async handleSmsLogin (query, type, route) {
  42. const api = type ? smsLogin : passwordLogin
  43. const { data, code } = await api(query)
  44. if (code === 0) {
  45. uni.showToast({
  46. title: '登录成功'
  47. })
  48. }
  49. this.accountInfo = data
  50. this.getInfo()
  51. this.getUserInfo()
  52. closeAuthModal()
  53. // 登录成功后的跳转地址
  54. // if (tabUrl.includes(route)) {
  55. // uni.switchTab({
  56. // url: '/' + route
  57. // })
  58. // } else {
  59. // uni.navigateTo({
  60. // url: '/' + route
  61. // })
  62. // }
  63. // uni.switchTab({
  64. // url: '/pages/index/position'
  65. // })
  66. },
  67. // 获取人才信息
  68. async getInfo() {
  69. const { code, data } = await getBaseInfo({ userId: this.accountInfo.userId });
  70. if (code !== 0) {
  71. return;
  72. }
  73. this.baseInfo = data;
  74. return Promise.resolve(data);
  75. },
  76. // 获取用户信息
  77. async getUserInfo() {
  78. const { code, data } = await getUserInfo({ id: this.accountInfo.userId });
  79. if (code !== 0) {
  80. return;
  81. }
  82. this.userInfo = data;
  83. return Promise.resolve(data);
  84. },
  85. // 设置 token
  86. setToken(token = '', refreshToken = '') {
  87. if (token === '') {
  88. this.isLogin = false;
  89. uni.removeStorageSync('token');
  90. uni.removeStorageSync('refresh-token');
  91. } else {
  92. this.isLogin = true;
  93. uni.setStorageSync('token', token);
  94. uni.setStorageSync('refresh-token', refreshToken);
  95. this.loginAfter();
  96. }
  97. return this.isLogin;
  98. },
  99. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  100. async updateUserData() {
  101. if (!this.isLogin) {
  102. this.resetUserData();
  103. return;
  104. }
  105. // 防抖,5 秒之内不刷新
  106. const nowTime = new Date().getTime();
  107. if (this.lastUpdateTime + 5000 > nowTime) {
  108. return;
  109. }
  110. this.lastUpdateTime = nowTime;
  111. // 获取最新信息
  112. return this.baseInfo;
  113. },
  114. // 重置用户默认数据
  115. resetUserData() {
  116. // 清空 token
  117. this.setToken();
  118. // 清空用户相关的缓存
  119. this.baseInfo = clone(defaultBaseInfo);
  120. this.userInfo = {}
  121. this.accountInfo = cloneDeep(defaultAccountInfo);
  122. },
  123. // 登录后,加载各种信息
  124. async loginAfter() {
  125. await this.updateUserData();
  126. },
  127. // 登出系统
  128. async handleLogout() {
  129. await logout()
  130. this.resetUserData();
  131. return !this.isLogin;
  132. },
  133. },
  134. persist: {
  135. // enabled: true,
  136. // strategies: [
  137. // {
  138. // key: 'user-store'
  139. // }
  140. // ]
  141. storage: {
  142. setItem(key, value) {
  143. uni.setStorageSync(key, value)
  144. },
  145. getItem(key) {
  146. return uni.getStorageSync(key)
  147. },
  148. },
  149. }
  150. })