2024-05-27 07:56:50 +00:00
|
|
|
|
import { defineStore } from 'pinia';
|
2022-11-22 07:45:36 +00:00
|
|
|
|
import $share from '@/sheep/platform/share';
|
2024-08-04 09:11:12 +00:00
|
|
|
|
import { clone, cloneDeep } from 'lodash-es';
|
2022-11-22 07:45:36 +00:00
|
|
|
|
import cart from './cart';
|
|
|
|
|
import app from './app';
|
2024-05-27 07:56:50 +00:00
|
|
|
|
import { showAuthModal } from '@/sheep/hooks/useModal';
|
2023-12-28 14:00:07 +00:00
|
|
|
|
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';
|
2022-11-22 07:45:36 +00:00
|
|
|
|
|
|
|
|
|
// 默认用户信息
|
|
|
|
|
const defaultUserInfo = {
|
2024-05-27 07:56:50 +00:00
|
|
|
|
avatar: '', // 头像
|
|
|
|
|
nickname: '', // 昵称
|
|
|
|
|
gender: 0, // 性别
|
|
|
|
|
mobile: '', // 手机号
|
2023-12-28 14:00:07 +00:00
|
|
|
|
point: 0, // 积分
|
2022-11-22 07:45:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2023-12-28 14:00:07 +00:00
|
|
|
|
// 默认钱包信息
|
|
|
|
|
const defaultUserWallet = {
|
|
|
|
|
balance: 0, // 余额
|
2024-05-27 07:56:50 +00:00
|
|
|
|
};
|
2023-12-28 14:00:07 +00:00
|
|
|
|
|
2022-11-22 07:45:36 +00:00
|
|
|
|
// 默认订单、优惠券等其他资产信息
|
|
|
|
|
const defaultNumData = {
|
2023-12-28 14:00:07 +00:00
|
|
|
|
unusedCouponCount: 0,
|
|
|
|
|
orderCount: {
|
|
|
|
|
allCount: 0,
|
|
|
|
|
unpaidCount: 0,
|
|
|
|
|
undeliveredCount: 0,
|
|
|
|
|
deliveredCount: 0,
|
|
|
|
|
uncommentedCount: 0,
|
|
|
|
|
afterSaleCount: 0,
|
2024-05-27 07:56:50 +00:00
|
|
|
|
},
|
2022-11-22 07:45:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const user = defineStore({
|
2024-05-27 07:56:50 +00:00
|
|
|
|
id: 'user',
|
|
|
|
|
state: () => ({
|
|
|
|
|
userInfo: clone(defaultUserInfo), // 用户信息
|
2023-12-28 14:00:07 +00:00
|
|
|
|
userWallet: clone(defaultUserWallet), // 用户钱包信息
|
2024-05-27 07:56:50 +00:00
|
|
|
|
isLogin: !!uni.getStorageSync('token'), // 登录状态
|
|
|
|
|
numData: cloneDeep(defaultNumData), // 用户其他数据
|
|
|
|
|
lastUpdateTime: 0, // 上次更新时间
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
|
// 获取用户信息
|
|
|
|
|
async getInfo() {
|
|
|
|
|
const { code, data } = await UserApi.getUserInfo();
|
2023-12-28 14:00:07 +00:00
|
|
|
|
if (code !== 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.userInfo = data;
|
2024-05-27 07:56:50 +00:00
|
|
|
|
return Promise.resolve(data);
|
|
|
|
|
},
|
2023-12-06 09:56:46 +00:00
|
|
|
|
|
2023-12-28 14:00:07 +00:00
|
|
|
|
// 获得用户钱包
|
|
|
|
|
async getWallet() {
|
|
|
|
|
const { code, data } = await PayWalletApi.getPayWallet();
|
|
|
|
|
if (code !== 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.userWallet = data;
|
|
|
|
|
},
|
|
|
|
|
|
2024-05-27 07:56:50 +00:00
|
|
|
|
// 获取订单、优惠券等其他资产信息
|
2023-12-28 14:00:07 +00:00
|
|
|
|
getNumData() {
|
2024-08-04 09:11:12 +00:00
|
|
|
|
OrderApi.getOrderCount().then((res) => {
|
2023-12-28 14:00:07 +00:00
|
|
|
|
if (res.code === 0) {
|
|
|
|
|
this.numData.orderCount = res.data;
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-08-04 09:11:12 +00:00
|
|
|
|
CouponApi.getUnusedCouponCount().then((res) => {
|
2023-12-28 14:00:07 +00:00
|
|
|
|
if (res.code === 0) {
|
|
|
|
|
this.numData.unusedCouponCount = res.data;
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-05-27 07:56:50 +00:00
|
|
|
|
},
|
2023-12-06 09:56:46 +00:00
|
|
|
|
|
2024-05-27 07:56:50 +00:00
|
|
|
|
// 设置 token
|
2024-01-20 04:46:19 +00:00
|
|
|
|
setToken(token = '', refreshToken = '') {
|
2024-05-27 07:56:50 +00:00
|
|
|
|
if (token === '') {
|
|
|
|
|
this.isLogin = false;
|
|
|
|
|
uni.removeStorageSync('token');
|
|
|
|
|
uni.removeStorageSync('refresh-token');
|
|
|
|
|
} else {
|
|
|
|
|
this.isLogin = true;
|
|
|
|
|
uni.setStorageSync('token', token);
|
2024-01-20 04:46:19 +00:00
|
|
|
|
uni.setStorageSync('refresh-token', refreshToken);
|
2024-05-27 07:56:50 +00:00
|
|
|
|
this.loginAfter();
|
|
|
|
|
}
|
|
|
|
|
return this.isLogin;
|
|
|
|
|
},
|
2023-12-06 09:56:46 +00:00
|
|
|
|
|
2024-05-27 07:56:50 +00:00
|
|
|
|
// 更新用户相关信息 (手动限流,5 秒之内不刷新)
|
2023-12-17 15:47:16 +00:00
|
|
|
|
async updateUserData() {
|
2024-05-27 07:56:50 +00:00
|
|
|
|
if (!this.isLogin) {
|
|
|
|
|
this.resetUserData();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-28 14:00:07 +00:00
|
|
|
|
// 防抖,5 秒之内不刷新
|
2024-05-27 07:56:50 +00:00
|
|
|
|
const nowTime = new Date().getTime();
|
2023-12-28 14:00:07 +00:00
|
|
|
|
if (this.lastUpdateTime + 5000 > nowTime) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.lastUpdateTime = nowTime;
|
|
|
|
|
|
|
|
|
|
// 获取最新信息
|
2024-05-27 07:56:50 +00:00
|
|
|
|
await this.getInfo();
|
2023-12-28 14:00:07 +00:00
|
|
|
|
this.getWallet();
|
2024-05-27 07:56:50 +00:00
|
|
|
|
this.getNumData();
|
|
|
|
|
return this.userInfo;
|
|
|
|
|
},
|
2023-12-06 09:56:46 +00:00
|
|
|
|
|
2024-05-27 07:56:50 +00:00
|
|
|
|
// 重置用户默认数据
|
2023-12-17 15:47:16 +00:00
|
|
|
|
resetUserData() {
|
2023-12-28 14:59:14 +00:00
|
|
|
|
// 清空 token
|
2024-05-27 07:56:50 +00:00
|
|
|
|
this.setToken();
|
2023-12-28 14:59:14 +00:00
|
|
|
|
// 清空用户相关的缓存
|
2024-05-27 07:56:50 +00:00
|
|
|
|
this.userInfo = clone(defaultUserInfo);
|
2023-12-28 14:00:07 +00:00
|
|
|
|
this.userWallet = clone(defaultUserWallet);
|
2024-05-27 07:56:50 +00:00
|
|
|
|
this.numData = cloneDeep(defaultNumData);
|
2023-12-28 14:59:14 +00:00
|
|
|
|
// 清空购物车的缓存
|
2024-05-27 07:56:50 +00:00
|
|
|
|
cart().emptyList();
|
|
|
|
|
},
|
2023-12-06 09:56:46 +00:00
|
|
|
|
|
2024-05-27 07:56:50 +00:00
|
|
|
|
// 登录后,加载各种信息
|
2023-12-17 15:47:16 +00:00
|
|
|
|
// TODO 芋艿:整理下;
|
|
|
|
|
async loginAfter() {
|
2024-05-27 07:56:50 +00:00
|
|
|
|
await this.updateUserData();
|
2023-12-28 14:59:14 +00:00
|
|
|
|
|
|
|
|
|
// 加载购物车
|
2024-05-27 07:56:50 +00:00
|
|
|
|
cart().getList();
|
|
|
|
|
// 登录后设置全局分享参数
|
|
|
|
|
$share.getShareInfo();
|
2023-12-28 14:59:14 +00:00
|
|
|
|
|
2024-05-27 07:56:50 +00:00
|
|
|
|
// 提醒绑定手机号
|
|
|
|
|
if (app().platform.bind_mobile && !this.userInfo.mobile) {
|
|
|
|
|
showAuthModal('changeMobile');
|
|
|
|
|
}
|
2023-12-06 09:56:46 +00:00
|
|
|
|
|
2024-05-27 07:56:50 +00:00
|
|
|
|
// 绑定推广员
|
2024-08-04 09:11:12 +00:00
|
|
|
|
$share.bindBrokerageUser();
|
2024-05-27 07:56:50 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 登出系统
|
2023-12-28 14:59:14 +00:00
|
|
|
|
async logout() {
|
|
|
|
|
this.resetUserData();
|
2024-05-27 07:56:50 +00:00
|
|
|
|
return !this.isLogin;
|
2024-08-04 09:11:12 +00:00
|
|
|
|
},
|
2024-05-27 07:56:50 +00:00
|
|
|
|
},
|
|
|
|
|
persist: {
|
|
|
|
|
enabled: true,
|
2024-08-04 09:11:12 +00:00
|
|
|
|
strategies: [
|
|
|
|
|
{
|
|
|
|
|
key: 'user-store',
|
|
|
|
|
},
|
|
|
|
|
],
|
2024-05-27 07:56:50 +00:00
|
|
|
|
},
|
2022-11-22 07:45:36 +00:00
|
|
|
|
});
|
|
|
|
|
|
2024-05-14 15:23:31 +00:00
|
|
|
|
export default user;
|