user.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. export const userStore = defineStore({
  22. id: 'user',
  23. state: () => ({
  24. userInfo: {}, // 用户信息
  25. isLogin: !!uni.getStorageSync('token'), // 登录状态
  26. lastUpdateTime: 0, // 上次更新时间
  27. accountInfo: cloneDeep(defaultAccountInfo), // 账号信息
  28. }),
  29. actions: {
  30. // 登录
  31. async handleSmsLogin (query, type) {
  32. const api = type ? smsLogin : passwordLogin
  33. const { data, code } = await api(query)
  34. if (code === 0) {
  35. uni.showToast({
  36. title: '登录成功'
  37. })
  38. }
  39. this.accountInfo = data
  40. this.getInfo()
  41. uni.switchTab({
  42. url: '/pages/index/my'
  43. })
  44. },
  45. // 获取用户信息
  46. async getInfo() {
  47. const { code, data } = await getUserInfo({ userId: this.accountInfo.userId });
  48. if (code !== 0) {
  49. return;
  50. }
  51. this.userInfo = data;
  52. return Promise.resolve(data);
  53. },
  54. // 设置 token
  55. setToken(token = '', refreshToken = '') {
  56. if (token === '') {
  57. this.isLogin = false;
  58. uni.removeStorageSync('token');
  59. uni.removeStorageSync('refresh-token');
  60. } else {
  61. this.isLogin = true;
  62. uni.setStorageSync('token', token);
  63. uni.setStorageSync('refresh-token', refreshToken);
  64. this.loginAfter();
  65. }
  66. return this.isLogin;
  67. },
  68. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  69. async updateUserData() {
  70. if (!this.isLogin) {
  71. this.resetUserData();
  72. return;
  73. }
  74. // 防抖,5 秒之内不刷新
  75. const nowTime = new Date().getTime();
  76. if (this.lastUpdateTime + 5000 > nowTime) {
  77. return;
  78. }
  79. this.lastUpdateTime = nowTime;
  80. // 获取最新信息
  81. // await this.getInfo();
  82. // this.getWallet();
  83. return this.userInfo;
  84. },
  85. // 重置用户默认数据
  86. resetUserData() {
  87. // 清空 token
  88. this.setToken();
  89. // 清空用户相关的缓存
  90. this.userInfo = clone(defaultUserInfo);
  91. this.accountInfo = cloneDeep(defaultAccountInfo);
  92. },
  93. // 登录后,加载各种信息
  94. async loginAfter() {
  95. await this.updateUserData();
  96. },
  97. // 登出系统
  98. async handleLogout() {
  99. await logout()
  100. this.resetUserData();
  101. return !this.isLogin;
  102. },
  103. },
  104. persist: {
  105. enabled: true,
  106. strategies: [
  107. {
  108. key: 'user-store',
  109. },
  110. ],
  111. },
  112. });