user.js 6.9 KB

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