2026-02-04 17:47:51 +08:00
|
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
|
|
import $share from '@/sheep/platform/share';
|
|
|
|
|
|
import { clone, cloneDeep } from 'lodash-es';
|
|
|
|
|
|
import cart from './cart';
|
|
|
|
|
|
import app from './app';
|
|
|
|
|
|
import { showAuthModal } 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';
|
|
|
|
|
|
|
|
|
|
|
|
// 默认用户信息
|
|
|
|
|
|
const defaultUserInfo = {
|
|
|
|
|
|
avatar: '', // 头像
|
|
|
|
|
|
nickname: '', // 昵称
|
|
|
|
|
|
gender: 0, // 性别
|
|
|
|
|
|
mobile: '', // 手机号
|
|
|
|
|
|
point: 0, // 积分
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 默认钱包信息
|
|
|
|
|
|
const defaultUserWallet = {
|
|
|
|
|
|
balance: 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), // 用户钱包信息
|
2026-02-06 14:32:50 +08:00
|
|
|
|
isLogin: false, // 登录状态,初始值为 false,由持久化插件恢复
|
2026-02-04 17:47:51 +08:00
|
|
|
|
numData: cloneDeep(defaultNumData), // 用户其他数据
|
|
|
|
|
|
lastUpdateTime: 0, // 上次更新时间
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
|
|
actions: {
|
2026-02-06 14:32:50 +08:00
|
|
|
|
// 初始化用户状态
|
|
|
|
|
|
async initialize() {
|
|
|
|
|
|
// 检查 token 是否存在
|
|
|
|
|
|
const token = uni.getStorageSync('token');
|
|
|
|
|
|
// 检查 refresh-token 是否存在
|
|
|
|
|
|
const refreshToken = uni.getStorageSync('refresh-token');
|
|
|
|
|
|
// 检查持久化的用户数据是否存在
|
|
|
|
|
|
const userStoreData = uni.getStorageSync('user-store');
|
|
|
|
|
|
// 如果有用户数据但没有 token 和 refresh-token,说明登录状态已失效
|
|
|
|
|
|
if (userStoreData && !token && !refreshToken) {
|
|
|
|
|
|
this.resetUserData();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果有 token 或 refresh-token 但 isLogin 为 false,更新登录状态
|
|
|
|
|
|
else if ((token || refreshToken) && !this.isLogin) {
|
|
|
|
|
|
this.isLogin = true;
|
|
|
|
|
|
// 如果只有 refresh-token 没有 token,尝试刷新 token
|
|
|
|
|
|
if (!token && refreshToken) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const AuthUtil = await import('@/sheep/api/member/auth');
|
|
|
|
|
|
const refreshTokenResult = await AuthUtil.default.refreshToken(refreshToken);
|
|
|
|
|
|
if (refreshTokenResult.code === 0) {
|
|
|
|
|
|
this.setToken(refreshTokenResult.data.accessToken, refreshTokenResult.data.refreshToken);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 刷新失败,重置用户数据
|
|
|
|
|
|
this.resetUserData();
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
// 刷新失败,重置用户数据
|
|
|
|
|
|
this.resetUserData();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 有 token,直接更新用户信息
|
|
|
|
|
|
this.loginAfter(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2026-02-04 17:47:51 +08:00
|
|
|
|
// 获取用户信息
|
|
|
|
|
|
async getInfo() {
|
|
|
|
|
|
const { code, data } = await UserApi.getUserInfo();
|
|
|
|
|
|
if (code !== 0) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.userInfo = data;
|
2026-02-06 00:44:50 +08:00
|
|
|
|
// 存储租户ID
|
|
|
|
|
|
if (data.tenantId) {
|
|
|
|
|
|
uni.setStorageSync('tenant-id', data.tenantId);
|
|
|
|
|
|
}
|
2026-02-04 17:47:51 +08:00
|
|
|
|
return Promise.resolve(data);
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获得用户钱包
|
|
|
|
|
|
async getWallet() {
|
|
|
|
|
|
const { code, data } = await PayWalletApi.getPayWallet();
|
|
|
|
|
|
if (code !== 0) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.userWallet = 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 设置 token
|
|
|
|
|
|
setToken(token = '', refreshToken = '') {
|
|
|
|
|
|
if (token === '') {
|
|
|
|
|
|
this.isLogin = false;
|
|
|
|
|
|
uni.removeStorageSync('token');
|
|
|
|
|
|
uni.removeStorageSync('refresh-token');
|
2026-02-06 00:44:50 +08:00
|
|
|
|
uni.removeStorageSync('tenant-id');
|
2026-02-04 17:47:51 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
this.isLogin = true;
|
|
|
|
|
|
uni.setStorageSync('token', token);
|
|
|
|
|
|
uni.setStorageSync('refresh-token', refreshToken);
|
2026-02-06 14:32:50 +08:00
|
|
|
|
this.loginAfter(true);
|
2026-02-04 17:47:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
return this.isLogin;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 更新用户相关信息 (手动限流,5 秒之内不刷新)
|
|
|
|
|
|
async updateUserData() {
|
|
|
|
|
|
if (!this.isLogin) {
|
|
|
|
|
|
this.resetUserData();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 防抖,5 秒之内不刷新
|
|
|
|
|
|
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() {
|
|
|
|
|
|
// 清空 token
|
|
|
|
|
|
this.setToken();
|
|
|
|
|
|
// 清空用户相关的缓存
|
|
|
|
|
|
this.userInfo = clone(defaultUserInfo);
|
|
|
|
|
|
this.userWallet = clone(defaultUserWallet);
|
|
|
|
|
|
this.numData = cloneDeep(defaultNumData);
|
|
|
|
|
|
// 清空购物车的缓存
|
|
|
|
|
|
cart().emptyList();
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 登录后,加载各种信息
|
2026-02-06 14:32:50 +08:00
|
|
|
|
async loginAfter(isLogin=false) {
|
|
|
|
|
|
if(!isLogin) {
|
|
|
|
|
|
return;
|
2026-02-04 17:47:51 +08:00
|
|
|
|
}
|
2026-02-06 14:32:50 +08:00
|
|
|
|
// 先更新用户数据
|
|
|
|
|
|
await this.updateUserData();
|
2026-02-04 17:47:51 +08:00
|
|
|
|
|
2026-02-06 14:32:50 +08:00
|
|
|
|
// 登录成功后,延迟重新获取模板信息
|
|
|
|
|
|
// 确保租户ID已经被正确存储到本地存储中
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
app().init(null, true);
|
|
|
|
|
|
}, 100);
|
2026-02-04 17:47:51 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 登出系统
|
|
|
|
|
|
async logout() {
|
|
|
|
|
|
this.resetUserData();
|
|
|
|
|
|
return !this.isLogin;
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
persist: {
|
|
|
|
|
|
enabled: true,
|
|
|
|
|
|
strategies: [
|
|
|
|
|
|
{
|
|
|
|
|
|
key: 'user-store',
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export default user;
|