user.js 3.4 KB

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