user.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import {
  2. defineStore
  3. } from 'pinia';
  4. import {
  5. computed,
  6. ref,
  7. watch,
  8. } from 'vue';
  9. import sheep from '@/sheep';
  10. import $share from '@/sheep/platform/share';
  11. import {
  12. isEmpty,
  13. cloneDeep,
  14. clone
  15. } from 'lodash';
  16. import cart from './cart';
  17. import app from './app';
  18. import {
  19. showAuthModal,
  20. showSubscribeModal,
  21. cancelAutoSign,
  22. autoSign
  23. } from '@/sheep/hooks/useModal';
  24. import UserApi from '@/sheep/api/member/user';
  25. import PayWalletApi from '@/sheep/api/pay/wallet';
  26. import OrderApi from '@/sheep/api/trade/order';
  27. import CouponApi from '@/sheep/api/promotion/coupon';
  28. import { isAndroid,getAndroidJiGuangId } from '@/sheep/hooks/useApp'
  29. const android = computed(() => isAndroid());
  30. const androidJiGuangId = computed(() => getAndroidJiGuangId());
  31. // 默认用户信息
  32. const defaultUserInfo = {
  33. avatar: '', // 头像
  34. nickname: '', // 昵称
  35. gender: 0, // 性别
  36. mobile: '', // 手机号
  37. point: 0, // 佣金
  38. username:'', //用户名
  39. socialStatusLevel: "",
  40. socialStatusLevelName: "",
  41. socialStatusPoint: 0,
  42. socialUpNeed: 0,
  43. };
  44. // 默认钱包信息
  45. const defaultUserWallet = {
  46. integralDO: {
  47. currentQuota: 0, // 当前佣金
  48. highQuota: 0, //最高佣金
  49. freezeQuota: 0, //冻结佣金
  50. consumptionPoints:0//消费分
  51. },
  52. descNo: 0, //用户直推人人数
  53. descPrice: 0, //团队昨日贡献值
  54. descTotalPrice: 0, // 团队历史总贡献值
  55. }
  56. // 默认订单、优惠券等其他资产信息
  57. const defaultNumData = {
  58. unusedCouponCount: 0,
  59. orderCount: {
  60. allCount: 0,
  61. unpaidCount: 0,
  62. undeliveredCount: 0,
  63. deliveredCount: 0,
  64. uncommentedCount: 0,
  65. afterSaleCount: 0,
  66. },
  67. };
  68. const user = defineStore({
  69. id: 'user',
  70. state: () => ({
  71. userInfo: clone(defaultUserInfo), // 用户信息
  72. userWallet: clone(defaultUserWallet), // 用户钱包信息
  73. isLogin: !!uni.getStorageSync('token'), // 登录状态
  74. numData: cloneDeep(defaultNumData), // 用户其他数据
  75. lastUpdateTime: 0, // 上次更新时间
  76. isBinding: false,
  77. }),
  78. actions: {
  79. // 获取用户信息
  80. async getInfo() {
  81. const {
  82. code,
  83. data
  84. } = await UserApi.getUserInfo();
  85. if (code !== 0) {
  86. return;
  87. }
  88. // console.log("user.js data",data)
  89. this.userInfo = data;
  90. return Promise.resolve(data);
  91. },
  92. // 获得用户钱包
  93. async getWallet() {
  94. const {
  95. code,
  96. data
  97. } = await PayWalletApi.getDuserInfo();
  98. if (code !== 0) {
  99. return;
  100. }
  101. // 如果是安卓设备,判断是否注册极光推送
  102. if(android.value){
  103. // 如果后台没返回或者返回来的跟从webview中传来的不一致,拿最新的webview中的拿
  104. if(!data.androidRegisterId || data.androidRegisterId !== androidJiGuangId.value){
  105. await UserApi.updateAndroidJiGuangId(androidJiGuangId.value)
  106. }
  107. }
  108. this.userWallet = data;
  109. // 为什么要加1 因为这里返回来的人员不包括自身
  110. this.userWallet.descNo = data.descNo + 1
  111. uni.setStorageSync('isSign', data.isSign);
  112. },
  113. // 获取订单、优惠券等其他资产信息
  114. getNumData() {
  115. OrderApi.getOrderCount().then(res => {
  116. if (res.code === 0) {
  117. this.numData.orderCount = res.data;
  118. }
  119. });
  120. CouponApi.getUnusedCouponCount().then(res => {
  121. if (res.code === 0) {
  122. this.numData.unusedCouponCount = res.data;
  123. }
  124. });
  125. },
  126. // 添加分享记录
  127. // TODO 非繁人:整理下;
  128. async addShareLog(params) {
  129. const {
  130. error
  131. } = await userApi.addShareLog(params);
  132. if (error === 0) uni.removeStorageSync('shareLog');
  133. },
  134. // 设置 token
  135. setToken(token = '', refreshToken = '') {
  136. if (token === '') {
  137. this.isLogin = false;
  138. uni.removeStorageSync('token');
  139. uni.removeStorageSync('refresh-token')
  140. } else {
  141. this.isLogin = true;
  142. uni.setStorageSync('token', token);
  143. uni.setStorageSync('refresh-token', refreshToken);
  144. this.loginAfter();
  145. }
  146. return this.isLogin;
  147. },
  148. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  149. async updateUserData() {
  150. if (!this.isLogin) {
  151. this.resetUserData();
  152. return;
  153. }
  154. // 防抖,5 秒之内不刷新
  155. const nowTime = new Date().getTime();
  156. if (this.lastUpdateTime + 5000 > nowTime) {
  157. return;
  158. }
  159. this.lastUpdateTime = nowTime;
  160. // 获取最新信息
  161. await this.getInfo();
  162. this.getWallet();
  163. this.getNumData();
  164. return this.userInfo;
  165. },
  166. // 重置用户默认数据
  167. resetUserData() {
  168. // 清空 token
  169. this.setToken();
  170. // 清空用户相关的缓存
  171. this.userInfo = clone(defaultUserInfo);
  172. this.userWallet = clone(defaultUserWallet);
  173. this.numData = cloneDeep(defaultNumData);
  174. // 清空购物车的缓存
  175. cart().emptyList();
  176. // console.log("重制用户数据 取消自动签到")
  177. // 取消自动签到
  178. cancelAutoSign()
  179. // 删掉缓存中的isSign
  180. uni.removeStorageSync('isSign');
  181. uni.removeStorageSync('openid');
  182. },
  183. // 登录后,加载各种信息
  184. // TODO 非繁人:整理下;
  185. async loginAfter() {
  186. // console.log("登录后,加载各种信息")
  187. // 加载用户信息
  188. await this.updateUserData();
  189. // 加载购物车
  190. cart().getList();
  191. // 登录后设置全局分享参数
  192. $share.getShareInfo();
  193. // 自动签到
  194. autoSign();
  195. // 用户未绑定公众号,则弹出绑定公众号的弹窗
  196. const openid = await sheep.$platform.useProvider('wechat').getOpenid(true);
  197. if(!openid && !this.isBinding) {
  198. this.isBinding = true; // 设置正在绑定的标志
  199. uni.showModal({
  200. title: '常来此购',
  201. content: '您未绑定微信,请先绑定',
  202. confirmText: '绑定',
  203. success: function(res) {
  204. if (res.confirm) {
  205. // 跳转到绑定公众号页面
  206. sheep.$platform.useProvider('wechat').bind()
  207. this.isBinding = false; // 绑定完成后清除标志
  208. }
  209. },
  210. });
  211. }else if(!this.userInfo.subscribe) {
  212. // 用户未关注,则弹出关注公众号的弹窗
  213. console.log("user.js 当前用户未关注,弹出关注公众号的弹窗123123")
  214. showSubscribeModal();
  215. }
  216. // 提醒绑定手机号
  217. // if (app().platform.bind_mobile && !this.userInfo.mobile) {
  218. // showAuthModal('changeMobile');
  219. // }
  220. // 添加分享记录
  221. // TODO 非繁人:整理下;
  222. const shareLog = uni.getStorageSync('shareLog');
  223. if (!isEmpty(shareLog)) {
  224. this.addShareLog({
  225. ...shareLog,
  226. });
  227. }
  228. },
  229. // 登出系统
  230. async logout() {
  231. this.resetUserData();
  232. return !this.isLogin;
  233. }
  234. },
  235. persist: {
  236. enabled: true,
  237. strategies: [{
  238. key: 'user-store',
  239. }, ],
  240. },
  241. });
  242. export default user;