user.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // console.log("user.js data",data)
  78. this.userInfo = data;
  79. // console.log("user.js this.userInfo",this.userInfo)
  80. // console.log("获取到用户信息 开始自动签到")
  81. autoSign();
  82. return Promise.resolve(data);
  83. },
  84. // 获得用户钱包
  85. async getWallet() {
  86. const {
  87. code,
  88. data
  89. } = await PayWalletApi.getDuserInfo();
  90. if (code !== 0) {
  91. return;
  92. }
  93. this.userWallet = data;
  94. // 为什么要加1 因为这里返回来的人员不包括自身
  95. this.userWallet.descNo = data.descNo + 1
  96. uni.setStorageSync('isSign', data.isSign);
  97. },
  98. // 获取订单、优惠券等其他资产信息
  99. getNumData() {
  100. OrderApi.getOrderCount().then(res => {
  101. if (res.code === 0) {
  102. this.numData.orderCount = res.data;
  103. }
  104. });
  105. CouponApi.getUnusedCouponCount().then(res => {
  106. if (res.code === 0) {
  107. this.numData.unusedCouponCount = res.data;
  108. }
  109. });
  110. },
  111. // 添加分享记录
  112. // TODO 非繁人:整理下;
  113. async addShareLog(params) {
  114. const {
  115. error
  116. } = await userApi.addShareLog(params);
  117. if (error === 0) uni.removeStorageSync('shareLog');
  118. },
  119. // 设置 token
  120. setToken(token = '', refreshToken = '') {
  121. if (token === '') {
  122. this.isLogin = false;
  123. uni.removeStorageSync('token');
  124. uni.removeStorageSync('refresh-token')
  125. } else {
  126. this.isLogin = true;
  127. uni.setStorageSync('token', token);
  128. uni.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. // 防抖,5 秒之内不刷新
  140. const nowTime = new Date().getTime();
  141. if (this.lastUpdateTime + 5000 > nowTime) {
  142. return;
  143. }
  144. this.lastUpdateTime = nowTime;
  145. // 获取最新信息
  146. await this.getInfo();
  147. this.getWallet();
  148. this.getNumData();
  149. return this.userInfo;
  150. },
  151. // 重置用户默认数据
  152. resetUserData() {
  153. // 清空 token
  154. this.setToken();
  155. // 清空用户相关的缓存
  156. this.userInfo = clone(defaultUserInfo);
  157. this.userWallet = clone(defaultUserWallet);
  158. this.numData = cloneDeep(defaultNumData);
  159. // 清空购物车的缓存
  160. cart().emptyList();
  161. // console.log("重制用户数据 取消自动签到")
  162. // 取消自动签到
  163. cancelAutoSign()
  164. // 删掉缓存中的isSign
  165. uni.removeStorageSync('isSign');
  166. },
  167. // 登录后,加载各种信息
  168. // TODO 非繁人:整理下;
  169. async loginAfter() {
  170. await this.updateUserData();
  171. // 加载购物车
  172. cart().getList();
  173. // 登录后设置全局分享参数
  174. $share.getShareInfo();
  175. // 提醒绑定手机号
  176. // if (app().platform.bind_mobile && !this.userInfo.mobile) {
  177. // showAuthModal('changeMobile');
  178. // }
  179. // 添加分享记录
  180. // TODO 非繁人:整理下;
  181. const shareLog = uni.getStorageSync('shareLog');
  182. if (!isEmpty(shareLog)) {
  183. this.addShareLog({
  184. ...shareLog,
  185. });
  186. }
  187. },
  188. // 登出系统
  189. async logout() {
  190. this.resetUserData();
  191. return !this.isLogin;
  192. }
  193. },
  194. persist: {
  195. enabled: true,
  196. strategies: [{
  197. key: 'user-store',
  198. }, ],
  199. },
  200. });
  201. export default user;