user.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 { error, data } = await userApi.profile();
  44. if (error !== 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 { error, data } = await userApi.data();
  59. if (error === 0) {
  60. this.numData = data;
  61. }
  62. },
  63. // 添加分享记录
  64. async addShareLog(params) {
  65. const { error } = await userApi.addShareLog(params);
  66. if (error === 0) uni.removeStorageSync('shareLog');
  67. },
  68. // 设置token
  69. setToken(token = '') {
  70. if (token === '') {
  71. this.isLogin = false;
  72. uni.removeStorageSync('token');
  73. } else {
  74. this.isLogin = true;
  75. uni.setStorageSync('token', token);
  76. this.loginAfter();
  77. }
  78. return this.isLogin;
  79. },
  80. // 更新用户相关信息 (手动限流 5秒之内不刷新)
  81. async updateUserData() {
  82. if (!this.isLogin) {
  83. this.resetUserData();
  84. return;
  85. }
  86. const nowTime = new Date().getTime();
  87. if (this.lastUpdateTime + 5000 > nowTime) return;
  88. await this.getInfo();
  89. this.getNumData();
  90. this.lastUpdateTime = nowTime;
  91. return this.userInfo;
  92. },
  93. // 重置用户默认数据
  94. resetUserData() {
  95. this.setToken();
  96. this.userInfo = clone(defaultUserInfo);
  97. this.numData = cloneDeep(defaultNumData);
  98. this.agentInfo = {};
  99. cart().emptyList();
  100. },
  101. // 登录后
  102. async loginAfter() {
  103. await this.updateUserData();
  104. cart().getList();
  105. // 登录后设置全局分享参数
  106. $share.getShareInfo();
  107. // 提醒绑定手机号
  108. if (app().platform.bind_mobile && !this.userInfo.verification?.mobile) {
  109. showAuthModal('changeMobile');
  110. }
  111. // 添加分享记录
  112. const shareLog = uni.getStorageSync('shareLog');
  113. if (!isEmpty(shareLog)) {
  114. this.addShareLog({
  115. ...shareLog,
  116. });
  117. }
  118. },
  119. // 登出
  120. async logout(force = false) {
  121. if (!force) {
  122. const { error } = await userApi.logout();
  123. if (error === 0) {
  124. this.resetUserData();
  125. }
  126. }
  127. if (force) {
  128. this.resetUserData();
  129. }
  130. return !this.isLogin;
  131. },
  132. },
  133. persist: {
  134. enabled: true,
  135. strategies: [
  136. {
  137. key: 'user-store',
  138. },
  139. ],
  140. },
  141. });
  142. export default user;