user.js 4.5 KB

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