user.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const sheep_platform_share = require("../platform/share.js");
  4. const sheep_store_cart = require("./cart.js");
  5. require("./app.js");
  6. const sheep_hooks_useModal = require("../hooks/useModal.js");
  7. const sheep_api_member_user = require("../api/member/user.js");
  8. const sheep_api_pay_wallet = require("../api/pay/wallet.js");
  9. const sheep_api_trade_order = require("../api/trade/order.js");
  10. const sheep_api_promotion_coupon = require("../api/promotion/coupon.js");
  11. const defaultUserInfo = {
  12. avatar: "",
  13. // 头像
  14. nickname: "",
  15. // 昵称
  16. gender: 0,
  17. // 性别
  18. mobile: "",
  19. // 手机号
  20. point: 0,
  21. // 积分
  22. username: "",
  23. //用户名
  24. socialStatusLevel: "",
  25. socialStatusLevelName: "",
  26. socialStatusPoint: 0,
  27. socialUpNeed: 0
  28. };
  29. const defaultUserWallet = {
  30. integralDO: {
  31. currentQuota: 0,
  32. // 当前积分
  33. highQuota: 0,
  34. //最高积分
  35. freezeQuota: 0
  36. //冻结积分
  37. },
  38. descNo: 0,
  39. //用户直推人人数
  40. descPrice: 0,
  41. //团队昨日贡献值
  42. descTotalPrice: 0
  43. // 团队历史总贡献值
  44. };
  45. const defaultNumData = {
  46. unusedCouponCount: 0,
  47. orderCount: {
  48. allCount: 0,
  49. unpaidCount: 0,
  50. undeliveredCount: 0,
  51. deliveredCount: 0,
  52. uncommentedCount: 0,
  53. afterSaleCount: 0
  54. }
  55. };
  56. const user = common_vendor.defineStore({
  57. id: "user",
  58. state: () => ({
  59. userInfo: common_vendor.lodashExports.clone(defaultUserInfo),
  60. // 用户信息
  61. userWallet: common_vendor.lodashExports.clone(defaultUserWallet),
  62. // 用户钱包信息
  63. isLogin: !!common_vendor.index.getStorageSync("token"),
  64. // 登录状态
  65. numData: common_vendor.lodashExports.cloneDeep(defaultNumData),
  66. // 用户其他数据
  67. lastUpdateTime: 0
  68. // 上次更新时间
  69. }),
  70. actions: {
  71. // 获取用户信息
  72. async getInfo() {
  73. const {
  74. code,
  75. data
  76. } = await sheep_api_member_user.UserApi.getUserInfo();
  77. if (code !== 0) {
  78. return;
  79. }
  80. this.userInfo = data;
  81. sheep_hooks_useModal.autoSign();
  82. return Promise.resolve(data);
  83. },
  84. // 获得用户钱包
  85. async getWallet() {
  86. const {
  87. code,
  88. data
  89. } = await sheep_api_pay_wallet.PayWalletApi.getDuserInfo();
  90. if (code !== 0) {
  91. return;
  92. }
  93. this.userWallet = data;
  94. this.userWallet.descNo = data.descNo + 1;
  95. common_vendor.index.setStorageSync("isSign", data.isSign);
  96. },
  97. // 获取订单、优惠券等其他资产信息
  98. getNumData() {
  99. sheep_api_trade_order.OrderApi.getOrderCount().then((res) => {
  100. if (res.code === 0) {
  101. this.numData.orderCount = res.data;
  102. }
  103. });
  104. sheep_api_promotion_coupon.CouponApi.getUnusedCouponCount().then((res) => {
  105. if (res.code === 0) {
  106. this.numData.unusedCouponCount = res.data;
  107. }
  108. });
  109. },
  110. // 添加分享记录
  111. // TODO 非繁人:整理下;
  112. async addShareLog(params) {
  113. const {
  114. error
  115. } = await userApi.addShareLog(params);
  116. if (error === 0)
  117. common_vendor.index.removeStorageSync("shareLog");
  118. },
  119. // 设置 token
  120. setToken(token = "", refreshToken = "") {
  121. if (token === "") {
  122. this.isLogin = false;
  123. common_vendor.index.removeStorageSync("token");
  124. common_vendor.index.removeStorageSync("refresh-token");
  125. } else {
  126. this.isLogin = true;
  127. common_vendor.index.setStorageSync("token", token);
  128. common_vendor.index.setStorageSync("refresh-token", refreshToken);
  129. this.loginAfter();
  130. }
  131. return this.isLogin;
  132. },
  133. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  134. async updateUserData() {
  135. if (!this.isLogin) {
  136. this.resetUserData();
  137. return;
  138. }
  139. const nowTime = (/* @__PURE__ */ new Date()).getTime();
  140. if (this.lastUpdateTime + 5e3 > nowTime) {
  141. return;
  142. }
  143. this.lastUpdateTime = nowTime;
  144. await this.getInfo();
  145. this.getWallet();
  146. this.getNumData();
  147. return this.userInfo;
  148. },
  149. // 重置用户默认数据
  150. resetUserData() {
  151. this.setToken();
  152. this.userInfo = common_vendor.lodashExports.clone(defaultUserInfo);
  153. this.userWallet = common_vendor.lodashExports.clone(defaultUserWallet);
  154. this.numData = common_vendor.lodashExports.cloneDeep(defaultNumData);
  155. sheep_store_cart.cart().emptyList();
  156. sheep_hooks_useModal.cancelAutoSign();
  157. common_vendor.index.removeStorageSync("isSign");
  158. },
  159. // 登录后,加载各种信息
  160. // TODO 非繁人:整理下;
  161. async loginAfter() {
  162. await this.updateUserData();
  163. sheep_store_cart.cart().getList();
  164. sheep_platform_share.$share.getShareInfo();
  165. const shareLog = common_vendor.index.getStorageSync("shareLog");
  166. if (!common_vendor.lodashExports.isEmpty(shareLog)) {
  167. this.addShareLog({
  168. ...shareLog
  169. });
  170. }
  171. },
  172. // 登出系统
  173. async logout() {
  174. this.resetUserData();
  175. return !this.isLogin;
  176. }
  177. },
  178. persist: {
  179. enabled: true,
  180. strategies: [{
  181. key: "user-store"
  182. }]
  183. }
  184. });
  185. const __vite_glob_0_4 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  186. __proto__: null,
  187. default: user
  188. }, Symbol.toStringTag, { value: "Module" }));
  189. exports.__vite_glob_0_4 = __vite_glob_0_4;
  190. exports.user = user;