user.js 3.7 KB

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