| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 | 
							- import {
 
- 	defineStore
 
- } from 'pinia';
 
- import userApi from '@/sheep/api/user';
 
- import commissionApi from '@/sheep/api/commission';
 
- import $share from '@/sheep/platform/share';
 
- import {
 
- 	isEmpty,
 
- 	cloneDeep,
 
- 	clone
 
- } from 'lodash';
 
- import cart from './cart';
 
- import app from './app';
 
- import {
 
- 	showAuthModal
 
- } from '@/sheep/hooks/useModal';
 
- import AuthUtil from '@/sheep/api/member/auth';
 
- // 默认用户信息
 
- const defaultUserInfo = {
 
- 	avatar: '', // 头像
 
- 	nickname: '', // 昵称
 
- 	gender: 0, // 性别
 
- 	mobile: '', // 手机号
 
- 	money: '--', // 余额
 
- 	commission: '--', // 佣金
 
- 	score: '--', // 积分
 
- 	verification: {}, // 认证字段
 
- };
 
- // 默认订单、优惠券等其他资产信息
 
- const defaultNumData = {
 
- 	coupons_num: '--',
 
- 	order_num: {
 
- 		aftersale: 0,
 
- 		nocomment: 0,
 
- 		noget: 0,
 
- 		nosend: 0,
 
- 		unpaid: 0,
 
- 	},
 
- };
 
- const user = defineStore({
 
- 	id: 'user',
 
- 	state: () => ({
 
- 		userInfo: clone(defaultUserInfo), // 用户信息
 
- 		isLogin: !!uni.getStorageSync('token'), // 登录状态
 
- 		numData: cloneDeep(defaultNumData), // 用户其他数据
 
- 		agentInfo: {}, // 分销商信息
 
- 		lastUpdateTime: 0, // 上次更新时间
 
- 	}),
 
- 	actions: {
 
- 		// 获取个人信息
 
-     // TODO 芋艿:整理下;
 
- 		async getInfo() {
 
- 			const {
 
- 				code,
 
- 				data
 
- 			} = await userApi.profile();
 
- 			// 为了兼容 获取用户余额 可能还会用到其他参数
 
- 			// 优惠券数量,积分数量 应该在这里
 
- 			const {
 
- 				code: code2,
 
- 				data: data2
 
- 			} = await userApi.balance();
 
- 			if (code !== 0 || code2 != 0) return;
 
- 			data.money = data2.balance;
 
- 			this.userInfo = data;
 
- 			console.log(data2, '信息')
 
- 			return Promise.resolve(data);
 
- 		},
 
- 		// 获取分销商信息
 
-     // TODO 芋艿:整理下;
 
-     async getAgentInfo() {
 
- 			const res = await commissionApi.agent();
 
- 			if (res.error === 0) {
 
- 				this.agentInfo = res.data;
 
- 			}
 
- 			return Promise.resolve(res);
 
- 		},
 
- 		// 获取订单、优惠券等其他资产信息
 
-     // TODO 芋艿:整理下;
 
-     async getNumData() {
 
- 			const {
 
- 				code,
 
- 				data
 
- 			} = await userApi.data();
 
- 			const data2 = await userApi.data2();
 
- 			let data3 = await userApi.getUnused();
 
- 			console.log(data3.data, '优惠券')
 
- 			if (code === 0 && data2.code === 0) {
 
- 				console.log('订单数据', data);
 
- 				this.numData = {
 
- 					coupons_num: data3.data,
 
- 					order_num: {
 
- 						noget: data.deliveredCount,
 
- 						unpaid: data.unpaidCount,
 
- 						nocomment: data.uncommentedCount,
 
- 						aftersale: data2.data
 
- 					}
 
- 				};
 
- 			}
 
- 		},
 
- 		// 添加分享记录
 
-     // TODO 芋艿:整理下;
 
-     async addShareLog(params) {
 
- 			const {
 
- 				error
 
- 			} = await userApi.addShareLog(params);
 
- 			if (error === 0) uni.removeStorageSync('shareLog');
 
- 		},
 
- 		// 设置token
 
-     // TODO 芋艿:整理下;
 
-     setToken(token = '') {
 
- 			if (token === '') {
 
- 				this.isLogin = false;
 
- 				uni.removeStorageSync('token');
 
- 			} else {
 
- 				this.isLogin = true;
 
- 				uni.setStorageSync('token', token);
 
- 				this.loginAfter();
 
- 			}
 
- 			return this.isLogin;
 
- 		},
 
- 		// 更新用户相关信息 (手动限流 5秒之内不刷新)
 
-     // TODO 芋艿:整理下;
 
-     async updateUserData() {
 
- 			if (!this.isLogin) {
 
- 				this.resetUserData();
 
- 				return;
 
- 			}
 
- 			const nowTime = new Date().getTime();
 
- 			if (this.lastUpdateTime + 5000 > nowTime) return;
 
- 			await this.getInfo();
 
- 			this.getNumData();
 
- 			this.lastUpdateTime = nowTime;
 
- 			return this.userInfo;
 
- 		},
 
- 		// 重置用户默认数据
 
-     // TODO 芋艿:整理下;
 
-     resetUserData() {
 
- 			this.setToken();
 
- 			this.userInfo = clone(defaultUserInfo);
 
- 			this.numData = cloneDeep(defaultNumData);
 
- 			this.agentInfo = {};
 
- 			cart().emptyList();
 
- 		},
 
- 		// 登录后
 
-     // TODO 芋艿:整理下;
 
-     async loginAfter() {
 
- 			await this.updateUserData();
 
- 			cart().getList();
 
- 			// 登录后设置全局分享参数
 
- 			$share.getShareInfo();
 
- 			// 提醒绑定手机号
 
- 			// if (app().platform.bind_mobile && !this.userInfo.verification?.mobile) {
 
- 			// 	showAuthModal('changeMobile');
 
- 			// }
 
- 			// 添加分享记录
 
-       // TODO 芋艿:整理下;
 
-       const shareLog = uni.getStorageSync('shareLog');
 
- 			if (!isEmpty(shareLog)) {
 
- 				this.addShareLog({
 
- 					...shareLog,
 
- 				});
 
- 			}
 
- 		},
 
- 		// 登出
 
-     // TODO 芋艿:整理下;
 
-     async logout(force = false) {
 
- 			if (!force) {
 
- 				const { code } = AuthUtil.logout();
 
- 				if (code === 0) {
 
- 					this.resetUserData();
 
- 				}
 
- 			}
 
- 			if (force) {
 
- 				this.resetUserData();
 
- 			}
 
- 			return !this.isLogin;
 
- 		},
 
- 	},
 
- 	persist: {
 
- 		enabled: true,
 
- 		strategies: [{
 
- 			key: 'user-store',
 
- 		}, ],
 
- 	},
 
- });
 
- export default user;
 
 
  |