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