user.js 3.7 KB

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