user.js 4.5 KB

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