user.js 3.3 KB

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