user.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. } from '@/sheep/hooks/useModal';
  16. import AuthUtil from '@/sheep/api/member/auth';
  17. import BrokerageApi from '@/sheep/api/trade/brokerage';
  18. // 默认用户信息
  19. const defaultUserInfo = {
  20. avatar: '', // 头像
  21. nickname: '', // 昵称
  22. gender: 0, // 性别
  23. mobile: '', // 手机号
  24. money: '--', // 余额
  25. commission: '--', // 佣金 TODO 芋艿:干掉
  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. lastUpdateTime: 0, // 上次更新时间
  47. }),
  48. actions: {
  49. // 获取个人信息
  50. // TODO 芋艿:整理下;
  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;
  64. this.userInfo = data;
  65. // console.log(data2, '信息')
  66. return Promise.resolve(data);
  67. },
  68. // 获取订单、优惠券等其他资产信息
  69. // TODO 芋艿:整理下;
  70. async getNumData() {
  71. const {
  72. code,
  73. data
  74. } = await userApi.data();
  75. const data2 = await userApi.data2();
  76. let data3 = await userApi.getUnused();
  77. // console.log(data3.data, '优惠券')
  78. if (code === 0 && data2.code === 0) {
  79. // console.log('订单数据', data);
  80. this.numData = {
  81. coupons_num: data3.data,
  82. order_num: {
  83. noget: data.deliveredCount,
  84. unpaid: data.unpaidCount,
  85. nocomment: data.uncommentedCount,
  86. aftersale: data2.data
  87. }
  88. };
  89. }
  90. },
  91. // 添加分享记录
  92. // TODO 芋艿:整理下;
  93. async addShareLog(params) {
  94. const {
  95. error
  96. } = await userApi.addShareLog(params);
  97. if (error === 0) uni.removeStorageSync('shareLog');
  98. },
  99. // 设置token
  100. // TODO 芋艿:整理下;
  101. setToken(token = '') {
  102. if (token === '') {
  103. this.isLogin = false;
  104. uni.removeStorageSync('token');
  105. } else {
  106. this.isLogin = true;
  107. uni.setStorageSync('token', token);
  108. this.loginAfter();
  109. }
  110. return this.isLogin;
  111. },
  112. // 更新用户相关信息 (手动限流 5秒之内不刷新)
  113. // TODO 芋艿:整理下;
  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. // TODO 芋艿:整理下;
  128. resetUserData() {
  129. this.setToken();
  130. this.userInfo = clone(defaultUserInfo);
  131. this.numData = cloneDeep(defaultNumData);
  132. this.agentInfo = {};
  133. cart().emptyList();
  134. },
  135. // 登录后
  136. // TODO 芋艿:整理下;
  137. async loginAfter() {
  138. await this.updateUserData();
  139. cart().getList();
  140. // 登录后设置全局分享参数
  141. $share.getShareInfo();
  142. // 提醒绑定手机号
  143. // if (app().platform.bind_mobile && !this.userInfo.verification?.mobile) {
  144. // showAuthModal('changeMobile');
  145. // }
  146. // 添加分享记录
  147. // TODO 芋艿:整理下;
  148. const shareLog = uni.getStorageSync('shareLog');
  149. if (!isEmpty(shareLog)) {
  150. this.addShareLog({
  151. ...shareLog,
  152. });
  153. }
  154. },
  155. // 登出
  156. // TODO 芋艿:整理下;
  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;