user.js 4.1 KB

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