user.js 3.7 KB

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