user.js 3.9 KB

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