user.js 5.2 KB

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