user.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. if (code === 0 && data2.code === 0) {
  83. console.log('订单数据', data);
  84. this.numData = {
  85. order_num: {
  86. noget: data.deliveredCount,
  87. unpaid: data.unpaidCount,
  88. nocomment: data.uncommentedCount,
  89. aftersale: data2.data
  90. }
  91. };
  92. }
  93. },
  94. // 添加分享记录
  95. async addShareLog(params) {
  96. const {
  97. error
  98. } = await userApi.addShareLog(params);
  99. if (error === 0) uni.removeStorageSync('shareLog');
  100. },
  101. // 设置token
  102. setToken(token = '') {
  103. if (token === '') {
  104. this.isLogin = false;
  105. uni.removeStorageSync('token');
  106. } else {
  107. this.isLogin = true;
  108. uni.setStorageSync('token', token);
  109. this.loginAfter();
  110. }
  111. return this.isLogin;
  112. },
  113. // 更新用户相关信息 (手动限流 5秒之内不刷新)
  114. async updateUserData() {
  115. if (!this.isLogin) {
  116. this.resetUserData();
  117. return;
  118. }
  119. const nowTime = new Date().getTime();
  120. if (this.lastUpdateTime + 5000 > nowTime) return;
  121. await this.getInfo();
  122. this.getNumData();
  123. this.lastUpdateTime = nowTime;
  124. return this.userInfo;
  125. },
  126. // 重置用户默认数据
  127. resetUserData() {
  128. this.setToken();
  129. this.userInfo = clone(defaultUserInfo);
  130. this.numData = cloneDeep(defaultNumData);
  131. this.agentInfo = {};
  132. cart().emptyList();
  133. },
  134. // 登录后
  135. async loginAfter() {
  136. await this.updateUserData();
  137. cart().getList();
  138. // 登录后设置全局分享参数
  139. $share.getShareInfo();
  140. // 提醒绑定手机号
  141. if (app().platform.bind_mobile && !this.userInfo.verification?.mobile) {
  142. showAuthModal('changeMobile');
  143. }
  144. // 添加分享记录
  145. const shareLog = uni.getStorageSync('shareLog');
  146. if (!isEmpty(shareLog)) {
  147. this.addShareLog({
  148. ...shareLog,
  149. });
  150. }
  151. },
  152. // 登出
  153. async logout(force = false) {
  154. if (!force) {
  155. const {
  156. error
  157. } = await userApi.logout();
  158. if (error === 0) {
  159. this.resetUserData();
  160. }
  161. }
  162. if (force) {
  163. this.resetUserData();
  164. }
  165. return !this.isLogin;
  166. },
  167. },
  168. persist: {
  169. enabled: true,
  170. strategies: [{
  171. key: 'user-store',
  172. }, ],
  173. },
  174. });
  175. export default user;