user.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { defineStore } from 'pinia';
  2. import $share from '@/sheep/platform/share';
  3. import { clone, cloneDeep } from 'lodash-es';
  4. import cart from './cart';
  5. import app from './app';
  6. import { showAuthModal } from '@/sheep/hooks/useModal';
  7. import UserApi from '@/sheep/api/member/user';
  8. import PayWalletApi from '@/sheep/api/pay/wallet';
  9. import OrderApi from '@/sheep/api/trade/order';
  10. import CouponApi from '@/sheep/api/promotion/coupon';
  11. // 默认用户信息
  12. const defaultUserInfo = {
  13. avatar: '', // 头像
  14. nickname: '', // 昵称
  15. gender: 0, // 性别
  16. mobile: '', // 手机号
  17. point: 0, // 积分
  18. };
  19. // 默认账户信息
  20. const defaultAccountInfo = {
  21. accessToken: '',
  22. expiresTime: '',
  23. openid: '',
  24. refreshToken: '',
  25. userId: ''
  26. }
  27. // 默认钱包信息
  28. const defaultUserWallet = {
  29. balance: 0, // 余额
  30. };
  31. // 默认订单、优惠券等其他资产信息
  32. const defaultNumData = {
  33. unusedCouponCount: 0,
  34. orderCount: {
  35. allCount: 0,
  36. unpaidCount: 0,
  37. undeliveredCount: 0,
  38. deliveredCount: 0,
  39. uncommentedCount: 0,
  40. afterSaleCount: 0,
  41. },
  42. };
  43. const user = defineStore({
  44. id: 'user',
  45. state: () => ({
  46. userInfo: clone(defaultUserInfo), // 用户信息
  47. userWallet: clone(defaultUserWallet), // 用户钱包信息
  48. isLogin: !!uni.getStorageSync('token'), // 登录状态
  49. numData: cloneDeep(defaultNumData), // 用户其他数据
  50. lastUpdateTime: 0, // 上次更新时间
  51. accountInfo: cloneDeep(defaultAccountInfo), // 账号信息
  52. }),
  53. actions: {
  54. // 获取用户信息
  55. async getInfo() {
  56. const { code, data } = await UserApi.getUserInfo({ userId: this.accountInfo.userId });
  57. if (code !== 0) {
  58. return;
  59. }
  60. this.userInfo = data;
  61. return Promise.resolve(data);
  62. },
  63. // 获得用户钱包
  64. async getWallet() {
  65. const { code, data } = await PayWalletApi.getPayWallet();
  66. if (code !== 0) {
  67. return;
  68. }
  69. this.userWallet = data;
  70. },
  71. // 获取订单、优惠券等其他资产信息
  72. getNumData() {
  73. OrderApi.getOrderCount().then((res) => {
  74. if (res.code === 0) {
  75. this.numData.orderCount = res.data;
  76. }
  77. });
  78. CouponApi.getUnusedCouponCount().then((res) => {
  79. if (res.code === 0) {
  80. this.numData.unusedCouponCount = res.data;
  81. }
  82. });
  83. },
  84. // 设置 token
  85. setToken(token = '', refreshToken = '') {
  86. if (token === '') {
  87. this.isLogin = false;
  88. uni.removeStorageSync('token');
  89. uni.removeStorageSync('refresh-token');
  90. } else {
  91. this.isLogin = true;
  92. uni.setStorageSync('token', token);
  93. uni.setStorageSync('refresh-token', refreshToken);
  94. this.loginAfter();
  95. }
  96. return this.isLogin;
  97. },
  98. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  99. async updateUserData() {
  100. if (!this.isLogin) {
  101. this.resetUserData();
  102. return;
  103. }
  104. // 防抖,5 秒之内不刷新
  105. const nowTime = new Date().getTime();
  106. if (this.lastUpdateTime + 5000 > nowTime) {
  107. return;
  108. }
  109. this.lastUpdateTime = nowTime;
  110. // 获取最新信息
  111. // await this.getInfo();
  112. this.getWallet();
  113. this.getNumData();
  114. return this.userInfo;
  115. },
  116. // 重置用户默认数据
  117. resetUserData() {
  118. // 清空 token
  119. this.setToken();
  120. // 清空用户相关的缓存
  121. this.userInfo = clone(defaultUserInfo);
  122. this.userWallet = clone(defaultUserWallet);
  123. this.numData = cloneDeep(defaultNumData);
  124. this.accountInfo = cloneDeep(defaultAccountInfo);
  125. // 清空购物车的缓存
  126. cart().emptyList();
  127. },
  128. // 登录后,加载各种信息
  129. // TODO 芋艿:整理下;
  130. async loginAfter() {
  131. await this.updateUserData();
  132. // 加载购物车
  133. cart().getList();
  134. // 登录后设置全局分享参数
  135. $share.getShareInfo();
  136. // 提醒绑定手机号
  137. if (app().platform.bind_mobile && !this.userInfo.mobile) {
  138. showAuthModal('changeMobile');
  139. }
  140. // 绑定推广员
  141. $share.bindBrokerageUser();
  142. },
  143. // 登出系统
  144. async logout() {
  145. this.resetUserData();
  146. return !this.isLogin;
  147. },
  148. },
  149. persist: {
  150. enabled: true,
  151. strategies: [
  152. {
  153. key: 'user-store',
  154. },
  155. ],
  156. },
  157. });
  158. export default user;