user.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { defineStore } from 'pinia';
  2. import userApi from '@/sheep/api/user';
  3. import commissionApi from '@/sheep/api/commission';
  4. import $share from '@/sheep/platform/share';
  5. import { isEmpty, cloneDeep, clone } from 'lodash';
  6. import cart from './cart';
  7. import app from './app';
  8. import { showAuthModal } from '@/sheep/hooks/useModal';
  9. // 默认用户信息
  10. const defaultUserInfo = {
  11. avatar: '', // 头像
  12. nickname: '', // 昵称
  13. gender: 0, // 性别
  14. mobile: '', // 手机号
  15. money: '--', // 余额
  16. commission: '--', // 佣金
  17. score: '--', // 积分
  18. verification: {}, // 认证字段
  19. };
  20. // 默认订单、优惠券等其他资产信息
  21. const defaultNumData = {
  22. coupons_num: '--',
  23. order_num: {
  24. aftersale: 0,
  25. nocomment: 0,
  26. noget: 0,
  27. nosend: 0,
  28. unpaid: 0,
  29. },
  30. };
  31. const user = defineStore({
  32. id: 'user',
  33. state: () => ({
  34. userInfo: clone(defaultUserInfo), // 用户信息
  35. isLogin: !!uni.getStorageSync('token'), // 登录状态
  36. numData: cloneDeep(defaultNumData), // 用户其他数据
  37. agentInfo: {}, // 分销商信息
  38. lastUpdateTime: 0, // 上次更新时间
  39. }),
  40. actions: {
  41. // 获取个人信息
  42. async getInfo() {
  43. const { code, data } = await userApi.profile();
  44. // 为了兼容 获取用户余额 可能还会用到其他参数
  45. const { code:code2, data:data2 } = await userApi.balance();
  46. if (code !== 0||code2!=0) return;
  47. data.money=data2.balance/100;
  48. this.userInfo = data;
  49. return Promise.resolve(data);
  50. },
  51. // 获取分销商信息
  52. async getAgentInfo() {
  53. const res = await commissionApi.agent();
  54. if (res.error === 0) {
  55. this.agentInfo = res.data;
  56. }
  57. return Promise.resolve(res);
  58. },
  59. // 获取订单、优惠券等其他资产信息
  60. async getNumData() {
  61. const { code, data } = await userApi.data();
  62. const data2 = await userApi.data2();
  63. if (code === 0&&data2.code===0) {
  64. console.log('订单数据',data);
  65. this.numData = {order_num:{
  66. noget:data.deliveredCount,
  67. unpaid:data.unpaidCount,
  68. nocomment:data.uncommentedCount,
  69. aftersale:data2.data
  70. }};
  71. }
  72. },
  73. // 添加分享记录
  74. async addShareLog(params) {
  75. const { error } = await userApi.addShareLog(params);
  76. if (error === 0) uni.removeStorageSync('shareLog');
  77. },
  78. // 设置token
  79. setToken(token = '') {
  80. if (token === '') {
  81. this.isLogin = false;
  82. uni.removeStorageSync('token');
  83. } else {
  84. this.isLogin = true;
  85. uni.setStorageSync('token', token);
  86. this.loginAfter();
  87. }
  88. return this.isLogin;
  89. },
  90. // 更新用户相关信息 (手动限流 5秒之内不刷新)
  91. async updateUserData() {
  92. if (!this.isLogin) {
  93. this.resetUserData();
  94. return;
  95. }
  96. const nowTime = new Date().getTime();
  97. if (this.lastUpdateTime + 5000 > nowTime) return;
  98. await this.getInfo();
  99. this.getNumData();
  100. this.lastUpdateTime = nowTime;
  101. return this.userInfo;
  102. },
  103. // 重置用户默认数据
  104. resetUserData() {
  105. this.setToken();
  106. this.userInfo = clone(defaultUserInfo);
  107. this.numData = cloneDeep(defaultNumData);
  108. this.agentInfo = {};
  109. cart().emptyList();
  110. },
  111. // 登录后
  112. async loginAfter() {
  113. await this.updateUserData();
  114. cart().getList();
  115. // 登录后设置全局分享参数
  116. $share.getShareInfo();
  117. // 提醒绑定手机号
  118. if (app().platform.bind_mobile && !this.userInfo.verification?.mobile) {
  119. showAuthModal('changeMobile');
  120. }
  121. // 添加分享记录
  122. const shareLog = uni.getStorageSync('shareLog');
  123. if (!isEmpty(shareLog)) {
  124. this.addShareLog({
  125. ...shareLog,
  126. });
  127. }
  128. },
  129. // 登出
  130. async logout(force = false) {
  131. if (!force) {
  132. const { error } = await userApi.logout();
  133. if (error === 0) {
  134. this.resetUserData();
  135. }
  136. }
  137. if (force) {
  138. this.resetUserData();
  139. }
  140. return !this.isLogin;
  141. },
  142. },
  143. persist: {
  144. enabled: true,
  145. strategies: [
  146. {
  147. key: 'user-store',
  148. },
  149. ],
  150. },
  151. });
  152. export default user;