user.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. cancelAutoSign,
  16. autoSign
  17. } from '@/sheep/hooks/useModal';
  18. import UserApi from '@/sheep/api/member/user';
  19. import PayWalletApi from '@/sheep/api/pay/wallet';
  20. import OrderApi from '@/sheep/api/trade/order';
  21. import CouponApi from '@/sheep/api/promotion/coupon';
  22. // 默认用户信息
  23. const defaultUserInfo = {
  24. avatar: '', // 头像
  25. nickname: '', // 昵称
  26. gender: 0, // 性别
  27. mobile: '', // 手机号
  28. point: 0, // 积分
  29. socialStatusLevel: "",
  30. socialStatusLevelName: "",
  31. socialStatusPoint: 0,
  32. socialUpNeed: 0,
  33. };
  34. // 默认钱包信息
  35. const defaultUserWallet = {
  36. integralDO: {
  37. currentQuota: 0, // 当前积分
  38. highQuota: 0, //最高积分
  39. freezeQuota: 0 //冻结积分
  40. },
  41. descNo: 0, //用户直推人人数
  42. descPrice: 0, //团队昨日贡献值
  43. descTotalPrice: 0, // 团队历史总贡献值
  44. }
  45. // 默认订单、优惠券等其他资产信息
  46. const defaultNumData = {
  47. unusedCouponCount: 0,
  48. orderCount: {
  49. allCount: 0,
  50. unpaidCount: 0,
  51. undeliveredCount: 0,
  52. deliveredCount: 0,
  53. uncommentedCount: 0,
  54. afterSaleCount: 0,
  55. },
  56. };
  57. const user = defineStore({
  58. id: 'user',
  59. state: () => ({
  60. userInfo: clone(defaultUserInfo), // 用户信息
  61. userWallet: clone(defaultUserWallet), // 用户钱包信息
  62. isLogin: !!uni.getStorageSync('token'), // 登录状态
  63. numData: cloneDeep(defaultNumData), // 用户其他数据
  64. lastUpdateTime: 0, // 上次更新时间
  65. }),
  66. actions: {
  67. // 获取用户信息
  68. async getInfo() {
  69. const {
  70. code,
  71. data
  72. } = await UserApi.getUserInfo();
  73. if (code !== 0) {
  74. return;
  75. }
  76. this.userInfo = data;
  77. // console.log("获取到用户信息 开始自动签到")
  78. autoSign();
  79. return Promise.resolve(data);
  80. },
  81. // 获得用户钱包
  82. async getWallet() {
  83. const {
  84. code,
  85. data
  86. } = await PayWalletApi.getDuserInfo();
  87. if (code !== 0) {
  88. return;
  89. }
  90. this.userWallet = data;
  91. // 为什么要加1 因为这里返回来的人员不包括自身
  92. this.userWallet.descNo = data.descNo + 1
  93. uni.setStorageSync('isSign', data.isSign);
  94. },
  95. // 获取订单、优惠券等其他资产信息
  96. getNumData() {
  97. OrderApi.getOrderCount().then(res => {
  98. if (res.code === 0) {
  99. this.numData.orderCount = res.data;
  100. }
  101. });
  102. CouponApi.getUnusedCouponCount().then(res => {
  103. if (res.code === 0) {
  104. this.numData.unusedCouponCount = res.data;
  105. }
  106. });
  107. },
  108. // 添加分享记录
  109. // TODO 非繁人:整理下;
  110. async addShareLog(params) {
  111. const {
  112. error
  113. } = await userApi.addShareLog(params);
  114. if (error === 0) uni.removeStorageSync('shareLog');
  115. },
  116. // 设置 token
  117. setToken(token = '', refreshToken = '') {
  118. if (token === '') {
  119. this.isLogin = false;
  120. uni.removeStorageSync('token');
  121. uni.removeStorageSync('refresh-token')
  122. } else {
  123. this.isLogin = true;
  124. uni.setStorageSync('token', token);
  125. uni.setStorageSync('refresh-token', refreshToken);
  126. this.loginAfter();
  127. }
  128. return this.isLogin;
  129. },
  130. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  131. async updateUserData() {
  132. if (!this.isLogin) {
  133. this.resetUserData();
  134. return;
  135. }
  136. // 防抖,5 秒之内不刷新
  137. const nowTime = new Date().getTime();
  138. if (this.lastUpdateTime + 5000 > nowTime) {
  139. return;
  140. }
  141. this.lastUpdateTime = nowTime;
  142. // 获取最新信息
  143. await this.getInfo();
  144. this.getWallet();
  145. this.getNumData();
  146. return this.userInfo;
  147. },
  148. // 重置用户默认数据
  149. resetUserData() {
  150. // 清空 token
  151. this.setToken();
  152. // 清空用户相关的缓存
  153. this.userInfo = clone(defaultUserInfo);
  154. this.userWallet = clone(defaultUserWallet);
  155. this.numData = cloneDeep(defaultNumData);
  156. // 清空购物车的缓存
  157. cart().emptyList();
  158. // console.log("重制用户数据 取消自动签到")
  159. // 取消自动签到
  160. cancelAutoSign()
  161. // 删掉缓存中的isSign
  162. uni.removeStorageSync('isSign');
  163. },
  164. // 登录后,加载各种信息
  165. // TODO 非繁人:整理下;
  166. async loginAfter() {
  167. await this.updateUserData();
  168. // 加载购物车
  169. cart().getList();
  170. // 登录后设置全局分享参数
  171. $share.getShareInfo();
  172. // 提醒绑定手机号
  173. // if (app().platform.bind_mobile && !this.userInfo.mobile) {
  174. // showAuthModal('changeMobile');
  175. // }
  176. // 添加分享记录
  177. // TODO 非繁人:整理下;
  178. const shareLog = uni.getStorageSync('shareLog');
  179. if (!isEmpty(shareLog)) {
  180. this.addShareLog({
  181. ...shareLog,
  182. });
  183. }
  184. },
  185. // 登出系统
  186. async logout() {
  187. this.resetUserData();
  188. return !this.isLogin;
  189. }
  190. },
  191. persist: {
  192. enabled: true,
  193. strategies: [{
  194. key: 'user-store',
  195. }, ],
  196. },
  197. });
  198. export default user;