user.js 4.2 KB

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