user.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 {
  57. code,
  58. data
  59. } = await UserApi.getUserInfo();
  60. if (code !== 0) {
  61. return;
  62. }
  63. this.userInfo = data;
  64. return Promise.resolve(data);
  65. },
  66. // 获得用户钱包
  67. async getWallet() {
  68. const {
  69. code,
  70. data
  71. } = await PayWalletApi.getPayWallet();
  72. if (code !== 0) {
  73. return;
  74. }
  75. this.userWallet = data;
  76. },
  77. // 获取订单、优惠券等其他资产信息
  78. getNumData() {
  79. OrderApi.getOrderCount().then(res => {
  80. if (res.code === 0) {
  81. this.numData.orderCount = res.data;
  82. }
  83. });
  84. CouponApi.getUnusedCouponCount().then(res => {
  85. if (res.code === 0) {
  86. this.numData.unusedCouponCount = res.data;
  87. }
  88. });
  89. },
  90. // 添加分享记录
  91. // TODO 非繁人:整理下;
  92. async addShareLog(params) {
  93. const {
  94. error
  95. } = await userApi.addShareLog(params);
  96. if (error === 0) uni.removeStorageSync('shareLog');
  97. },
  98. // 设置 token
  99. setToken(token = '', refreshToken = '') {
  100. if (token === '') {
  101. this.isLogin = false;
  102. uni.removeStorageSync('token');
  103. uni.removeStorageSync('refresh-token')
  104. } else {
  105. this.isLogin = true;
  106. uni.setStorageSync('token', token);
  107. uni.setStorageSync('refresh-token', refreshToken);
  108. this.loginAfter();
  109. }
  110. return this.isLogin;
  111. },
  112. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  113. async updateUserData() {
  114. if (!this.isLogin) {
  115. this.resetUserData();
  116. return;
  117. }
  118. // 防抖,5 秒之内不刷新
  119. const nowTime = new Date().getTime();
  120. if (this.lastUpdateTime + 5000 > nowTime) {
  121. return;
  122. }
  123. this.lastUpdateTime = nowTime;
  124. // 获取最新信息
  125. await this.getInfo();
  126. this.getWallet();
  127. this.getNumData();
  128. return this.userInfo;
  129. },
  130. // 重置用户默认数据
  131. resetUserData() {
  132. // 清空 token
  133. this.setToken();
  134. // 清空用户相关的缓存
  135. this.userInfo = clone(defaultUserInfo);
  136. this.userWallet = clone(defaultUserWallet);
  137. this.numData = cloneDeep(defaultNumData);
  138. // 清空购物车的缓存
  139. cart().emptyList();
  140. // 清空linkId
  141. uni.removeStorageSync('linkId');
  142. },
  143. // 登录后,加载各种信息
  144. // TODO 非繁人:整理下;
  145. async loginAfter() {
  146. await this.updateUserData();
  147. // 加载购物车
  148. cart().getList();
  149. // 登录后设置全局分享参数
  150. $share.getShareInfo();
  151. // 提醒绑定手机号
  152. if (app().platform.bind_mobile && !this.userInfo.mobile) {
  153. showAuthModal('changeMobile');
  154. }
  155. // 添加分享记录
  156. // TODO 非繁人:整理下;
  157. const shareLog = uni.getStorageSync('shareLog');
  158. if (!isEmpty(shareLog)) {
  159. this.addShareLog({
  160. ...shareLog,
  161. });
  162. }
  163. },
  164. // 登出系统
  165. async logout() {
  166. this.resetUserData();
  167. return !this.isLogin;
  168. }
  169. },
  170. persist: {
  171. enabled: true,
  172. strategies: [{
  173. key: 'user-store',
  174. }, ],
  175. },
  176. });
  177. export default user;