个人中心:优化登录后的数据读取

This commit is contained in:
YunaiV
2023-12-28 22:00:07 +08:00
parent 1a07dc4a40
commit c3cc5d653d
12 changed files with 125 additions and 171 deletions
+56 -52
View File
@@ -15,6 +15,10 @@ import {
} from '@/sheep/hooks/useModal';
import AuthUtil from '@/sheep/api/member/auth';
import BrokerageApi from '@/sheep/api/trade/brokerage';
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';
// 默认用户信息
const defaultUserInfo = {
@@ -22,20 +26,26 @@ const defaultUserInfo = {
nickname: '', // 昵称
gender: 0, // 性别
mobile: '', // 手机号
money: '--', // 余额
score: '--', // 积分 TODO 芋艿:改成 point
point: 0, // 积分
money: 0, // 余额
verification: {}, // 认证字段
};
// 默认钱包信息
const defaultUserWallet = {
balance: 0, // 余额
}
// 默认订单、优惠券等其他资产信息
const defaultNumData = {
coupons_num: '--',
order_num: {
aftersale: 0,
nocomment: 0,
noget: 0,
nosend: 0,
unpaid: 0,
unusedCouponCount: 0,
orderCount: {
allCount: 0,
unpaidCount: 0,
undeliveredCount: 0,
deliveredCount: 0,
uncommentedCount: 0,
afterSaleCount: 0,
},
};
@@ -43,55 +53,44 @@ const user = defineStore({
id: 'user',
state: () => ({
userInfo: clone(defaultUserInfo), // 用户信息
userWallet: clone(defaultUserWallet), // 用户钱包信息
isLogin: !!uni.getStorageSync('token'), // 登录状态
numData: cloneDeep(defaultNumData), // 用户其他数据
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, '信息')
const { code, data } = await UserApi.getUserInfo();
if (code !== 0) {
return;
}
this.userInfo = data;
return Promise.resolve(data);
},
// 获得用户钱包
async getWallet() {
const { code, data } = await PayWalletApi.getPayWallet();
if (code !== 0) {
return;
}
this.userWallet = data;
},
// 获取订单、优惠券等其他资产信息
// 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
}
};
}
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;
}
});
},
// 添加分享记录
@@ -117,18 +116,23 @@ const user = defineStore({
return this.isLogin;
},
// 更新用户相关信息 (手动限流 5秒之内不刷新)
// TODO 芋艿:整理下;
// 更新用户相关信息 (手动限流5 秒之内不刷新)
async updateUserData() {
if (!this.isLogin) {
this.resetUserData();
return;
}
// 防抖,5 秒之内不刷新
const nowTime = new Date().getTime();
if (this.lastUpdateTime + 5000 > nowTime) return;
if (this.lastUpdateTime + 5000 > nowTime) {
return;
}
this.lastUpdateTime = nowTime;
// 获取最新信息
await this.getInfo();
this.getWallet();
this.getNumData();
this.lastUpdateTime = nowTime;
return this.userInfo;
},
@@ -137,8 +141,8 @@ const user = defineStore({
resetUserData() {
this.setToken();
this.userInfo = clone(defaultUserInfo);
this.userWallet = clone(defaultUserWallet);
this.numData = cloneDeep(defaultNumData);
this.agentInfo = {};
cart().emptyList();
},