user.js 4.2 KB

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