user.js 4.1 KB

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