user.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. }),
  77. actions: {
  78. // 获取用户信息
  79. async getInfo() {
  80. const {
  81. code,
  82. data
  83. } = await UserApi.getUserInfo();
  84. if (code !== 0) {
  85. return;
  86. }
  87. // console.log("user.js data",data)
  88. this.userInfo = data;
  89. return Promise.resolve(data);
  90. },
  91. // 获得用户钱包
  92. async getWallet() {
  93. const {
  94. code,
  95. data
  96. } = await PayWalletApi.getDuserInfo();
  97. if (code !== 0) {
  98. return;
  99. }
  100. // 如果是安卓设备,判断是否注册极光推送
  101. if(android.value){
  102. // 如果后台没返回或者返回来的跟从webview中传来的不一致,拿最新的webview中的拿
  103. if(!data.androidRegisterId || data.androidRegisterId !== androidJiGuangId.value){
  104. await UserApi.updateAndroidJiGuangId(androidJiGuangId.value)
  105. }
  106. }
  107. this.userWallet = data;
  108. // 为什么要加1 因为这里返回来的人员不包括自身
  109. this.userWallet.descNo = data.descNo + 1
  110. uni.setStorageSync('isSign', data.isSign);
  111. },
  112. // 获取订单、优惠券等其他资产信息
  113. getNumData() {
  114. OrderApi.getOrderCount().then(res => {
  115. if (res.code === 0) {
  116. this.numData.orderCount = res.data;
  117. }
  118. });
  119. CouponApi.getUnusedCouponCount().then(res => {
  120. if (res.code === 0) {
  121. this.numData.unusedCouponCount = res.data;
  122. }
  123. });
  124. },
  125. // 添加分享记录
  126. // TODO 非繁人:整理下;
  127. async addShareLog(params) {
  128. const {
  129. error
  130. } = await userApi.addShareLog(params);
  131. if (error === 0) uni.removeStorageSync('shareLog');
  132. },
  133. // 设置 token
  134. async setToken(token = '', refreshToken = '') {
  135. if (token === '') {
  136. this.isLogin = false;
  137. uni.removeStorageSync('token');
  138. uni.removeStorageSync('refresh-token')
  139. } else {
  140. this.isLogin = true;
  141. uni.setStorageSync('token', token);
  142. uni.setStorageSync('refresh-token', refreshToken);
  143. this.loginAfter();
  144. // 用户未绑定公众号,则弹出绑定公众号的弹窗
  145. const openid = await sheep.$platform.useProvider('wechat').getOpenid(true);
  146. if(!openid) {
  147. uni.showModal({
  148. title: '常来此购',
  149. content: '您未绑定微信,请先绑定',
  150. confirmText: '绑定',
  151. success: function(res) {
  152. if (res.confirm) {
  153. sheep.$platform.useProvider('wechat').bind()
  154. }
  155. },
  156. });
  157. }
  158. }
  159. return this.isLogin;
  160. },
  161. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  162. async updateUserData() {
  163. if (!this.isLogin) {
  164. this.resetUserData();
  165. return;
  166. }
  167. // 防抖,5 秒之内不刷新
  168. const nowTime = new Date().getTime();
  169. if (this.lastUpdateTime + 5000 > nowTime) {
  170. return;
  171. }
  172. this.lastUpdateTime = nowTime;
  173. // 获取最新信息
  174. await this.getInfo();
  175. this.getWallet();
  176. this.getNumData();
  177. return this.userInfo;
  178. },
  179. // 重置用户默认数据
  180. resetUserData() {
  181. // 清空 token
  182. this.setToken();
  183. // 清空用户相关的缓存
  184. this.userInfo = clone(defaultUserInfo);
  185. this.userWallet = clone(defaultUserWallet);
  186. this.numData = cloneDeep(defaultNumData);
  187. // 清空购物车的缓存
  188. cart().emptyList();
  189. // console.log("重制用户数据 取消自动签到")
  190. // 取消自动签到
  191. cancelAutoSign()
  192. // 删掉缓存中的isSign
  193. uni.removeStorageSync('isSign');
  194. uni.removeStorageSync('openid');
  195. },
  196. // 登录后,加载各种信息
  197. // TODO 非繁人:整理下;
  198. async loginAfter() {
  199. // 加载用户信息
  200. await this.updateUserData();
  201. // 加载购物车
  202. cart().getList();
  203. // 登录后设置全局分享参数
  204. $share.getShareInfo();
  205. // 自动签到
  206. autoSign();
  207. // 提醒绑定手机号
  208. // if (app().platform.bind_mobile && !this.userInfo.mobile) {
  209. // showAuthModal('changeMobile');
  210. // }
  211. // 添加分享记录
  212. // TODO 非繁人:整理下;
  213. const shareLog = uni.getStorageSync('shareLog');
  214. if (!isEmpty(shareLog)) {
  215. this.addShareLog({
  216. ...shareLog,
  217. });
  218. }
  219. setTimeout(async() => {
  220. const openid = await sheep.$platform.useProvider('wechat').getOpenid(true);
  221. if(!this.userInfo.subscribe && openid) {
  222. // 用户未关注,则弹出关注公众号的弹窗
  223. // console.log("user.js loginAfter() 当前用户未关注,弹出关注公众号的弹窗")
  224. showSubscribeModal();
  225. }
  226. }, 800);
  227. },
  228. // 登出系统
  229. async logout() {
  230. this.resetUserData();
  231. return !this.isLogin;
  232. }
  233. },
  234. persist: {
  235. enabled: true,
  236. strategies: [{
  237. key: 'user-store',
  238. }, ],
  239. },
  240. });
  241. export default user;