123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- import {
- defineStore
- } from 'pinia';
- import {
- computed,
- ref,
- watch,
- } from 'vue';
- import sheep from '@/sheep';
- import $share from '@/sheep/platform/share';
- import {
- isEmpty,
- cloneDeep,
- clone
- } from 'lodash';
- import cart from './cart';
- import app from './app';
- import {
- showAuthModal,
- showSubscribeModal,
- cancelAutoSign,
- autoSign
- } from '@/sheep/hooks/useModal';
- import UserApi from '@/sheep/api/member/user';
- import PayWalletApi from '@/sheep/api/pay/wallet';
- import OrderApi from '@/sheep/api/trade/order';
- import CouponApi from '@/sheep/api/promotion/coupon';
- import { isAndroid,getAndroidJiGuangId } from '@/sheep/hooks/useApp'
- const android = computed(() => isAndroid());
- const androidJiGuangId = computed(() => getAndroidJiGuangId());
- const defaultUserInfo = {
- avatar: '',
- nickname: '',
- gender: 0,
- mobile: '',
- point: 0,
- username:'',
- socialStatusLevel: "",
- socialStatusLevelName: "",
- socialStatusPoint: 0,
- socialUpNeed: 0,
- };
- const defaultUserWallet = {
- integralDO: {
- currentQuota: 0,
- highQuota: 0,
- freezeQuota: 0,
- consumptionPoints:0
- },
-
- descNo: 0,
- descPrice: 0,
- descTotalPrice: 0,
- }
- const defaultNumData = {
- unusedCouponCount: 0,
- orderCount: {
- allCount: 0,
- unpaidCount: 0,
- undeliveredCount: 0,
- deliveredCount: 0,
- uncommentedCount: 0,
- afterSaleCount: 0,
- },
- };
- const user = defineStore({
- id: 'user',
- state: () => ({
- userInfo: clone(defaultUserInfo),
- userWallet: clone(defaultUserWallet),
- isLogin: !!uni.getStorageSync('token'),
- numData: cloneDeep(defaultNumData),
- lastUpdateTime: 0,
- }),
- actions: {
-
- async getInfo() {
- const {
- code,
- data
- } = await UserApi.getUserInfo();
- if (code !== 0) {
- return;
- }
-
-
-
- this.userInfo = data;
-
- return Promise.resolve(data);
-
-
-
- },
-
- async getWallet() {
-
- const {
- code,
- data
- } = await PayWalletApi.getDuserInfo();
- if (code !== 0) {
- return;
- }
-
-
- if(android.value){
-
- if(!data.androidRegisterId || data.androidRegisterId !== androidJiGuangId.value){
- await UserApi.updateAndroidJiGuangId(androidJiGuangId.value)
- }
- }
- this.userWallet = data;
-
- this.userWallet.descNo = data.descNo + 1
- uni.setStorageSync('isSign', data.isSign);
- },
-
- getNumData() {
- OrderApi.getOrderCount().then(res => {
- if (res.code === 0) {
- this.numData.orderCount = res.data;
- }
- });
- CouponApi.getUnusedCouponCount().then(res => {
- if (res.code === 0) {
- this.numData.unusedCouponCount = res.data;
- }
- });
- },
-
-
-
- async addShareLog(params) {
- const {
- error
- } = await userApi.addShareLog(params);
- if (error === 0) uni.removeStorageSync('shareLog');
- },
-
- async setToken(token = '', refreshToken = '') {
- if (token === '') {
- this.isLogin = false;
- uni.removeStorageSync('token');
- uni.removeStorageSync('refresh-token')
- } else {
- this.isLogin = true;
- uni.setStorageSync('token', token);
- uni.setStorageSync('refresh-token', refreshToken);
- this.loginAfter();
-
-
- const openid = await sheep.$platform.useProvider('wechat').getOpenid(true);
- if(!openid) {
- uni.showModal({
- title: '常来此购',
- content: '您未绑定微信,请先绑定',
- confirmText: '绑定',
- success: function(res) {
- if (res.confirm) {
- sheep.$platform.useProvider('wechat').bind()
- }
- },
- });
- }
- }
- return this.isLogin;
- },
-
-
- async updateUserData() {
- if (!this.isLogin) {
- this.resetUserData();
- return;
- }
-
- const nowTime = new Date().getTime();
- if (this.lastUpdateTime + 5000 > nowTime) {
- return;
- }
- this.lastUpdateTime = nowTime;
-
- await this.getInfo();
- this.getWallet();
- this.getNumData();
- return this.userInfo;
- },
-
- resetUserData() {
-
- this.setToken();
-
- this.userInfo = clone(defaultUserInfo);
- this.userWallet = clone(defaultUserWallet);
- this.numData = cloneDeep(defaultNumData);
-
- cart().emptyList();
-
-
- cancelAutoSign()
-
- uni.removeStorageSync('isSign');
- uni.removeStorageSync('openid');
- },
-
-
- async loginAfter() {
-
- await this.updateUserData();
-
- cart().getList();
-
- $share.getShareInfo();
-
- autoSign();
-
-
-
-
-
-
-
-
- const shareLog = uni.getStorageSync('shareLog');
- if (!isEmpty(shareLog)) {
- this.addShareLog({
- ...shareLog,
- });
- }
-
- setTimeout(async() => {
- const openid = await sheep.$platform.useProvider('wechat').getOpenid(true);
- if(!this.userInfo.subscribe && openid) {
-
-
- showSubscribeModal();
- }
- }, 800);
-
- },
-
-
- async logout() {
- this.resetUserData();
- return !this.isLogin;
-
- }
- },
- persist: {
- enabled: true,
- strategies: [{
- key: 'user-store',
- }, ],
- },
- });
- export default user;
|