This commit is contained in:
2026-02-23 02:05:48 +08:00
parent c8598d3dea
commit 5b3047f675
8 changed files with 178 additions and 159 deletions

View File

@@ -3,22 +3,29 @@ import { defineStore } from 'pinia'
import { getJSON, setJSON, remove } from '@/utils/storage'
import tokenManager from '@gold/utils/token-manager'
import { getUserInfo, clearUserInfoCache } from '@/api/userinfo'
import { getUserProfile } from '@/api/auth'
const STORAGE_KEY = 'user_store_v1'
export const useUserStore = defineStore('user', () => {
const isLoggedIn = ref(false)
const isHydrated = ref(false)
// 基础信息
const userId = ref('')
const nickname = ref('')
const avatar = ref('')
const wechat = ref({ openId: '', unionId: '', nickname: '', avatar: '' })
const balance = ref(0)
const vipLevel = ref(0)
const credits = ref(0)
const isHydrated = ref(false)
const displayName = computed(() => nickname.value || wechat.value.nickname || '未命名用户')
const displayAvatar = computed(() => avatar.value || wechat.value.avatar || '')
// 档案数据(来自 MemberUserProfile
const profile = ref(null)
const remainingPoints = computed(() => profile.value?.remainingPoints ?? 0)
const remainingStorage = computed(() => profile.value?.remainingStorage ?? 10) // GB
const usedStorage = computed(() => profile.value?.usedStorage ?? 0) // GB
const totalStorage = computed(() => profile.value?.totalStorage ?? 10) // GB
const totalRecharge = computed(() => profile.value?.totalRecharge ?? 0) // 充值金额
const displayName = computed(() => nickname.value || '未命名用户')
const displayAvatar = computed(() => avatar.value || '')
// 持久化数据
const userData = computed(() => ({
@@ -26,10 +33,7 @@ export const useUserStore = defineStore('user', () => {
userId: userId.value,
nickname: nickname.value,
avatar: avatar.value,
wechat: wechat.value,
balance: balance.value,
vipLevel: vipLevel.value,
credits: credits.value,
profile: profile.value,
}))
// 自动持久化
@@ -43,10 +47,7 @@ export const useUserStore = defineStore('user', () => {
userId.value = saved.userId || ''
nickname.value = saved.nickname || ''
avatar.value = saved.avatar || ''
wechat.value = saved.wechat || { openId: '', unionId: '', nickname: '', avatar: '' }
balance.value = Number(saved.balance || 0)
vipLevel.value = Number(saved.vipLevel || 0)
credits.value = Number(saved.credits || 0)
profile.value = saved.profile || null
}
hydrateFromStorage().then(() => {
@@ -54,17 +55,11 @@ export const useUserStore = defineStore('user', () => {
})
// 登录
const login = (profile) => {
const login = (data) => {
isLoggedIn.value = true
userId.value = String(profile.userId || profile.id || '')
nickname.value = profile.nickname || ''
avatar.value = profile.avatar || ''
if (profile.wechat) {
wechat.value = profile.wechat
}
balance.value = Number(profile.balance || 0)
vipLevel.value = Number(profile.vipLevel || 0)
credits.value = Number(profile.credits || 0)
userId.value = String(data.userId || data.id || '')
nickname.value = data.nickname || ''
avatar.value = data.avatar || ''
}
// 获取用户信息
@@ -75,7 +70,6 @@ export const useUserStore = defineStore('user', () => {
userId.value = String(userInfo.id || userInfo.userId || '')
nickname.value = userInfo.nickname || ''
avatar.value = userInfo.avatar || ''
if (userInfo.point !== undefined) credits.value = Number(userInfo.point || 0)
isLoggedIn.value = true
}
} catch (error) {
@@ -83,6 +77,18 @@ export const useUserStore = defineStore('user', () => {
}
}
// 获取用户档案
const fetchUserProfile = async () => {
try {
const profileData = await getUserProfile()
if (profileData) {
profile.value = profileData
}
} catch (error) {
console.error('获取用户档案失败:', error)
}
}
// 登出
const logout = async () => {
try {
@@ -96,10 +102,7 @@ export const useUserStore = defineStore('user', () => {
userId.value = ''
nickname.value = ''
avatar.value = ''
wechat.value = { openId: '', unionId: '', nickname: '', avatar: '' }
balance.value = 0
vipLevel.value = 0
credits.value = 0
profile.value = null
await remove(STORAGE_KEY)
}
@@ -110,18 +113,19 @@ export const useUserStore = defineStore('user', () => {
userId,
nickname,
avatar,
wechat,
balance,
vipLevel,
credits,
profile,
remainingPoints,
remainingStorage,
usedStorage,
totalStorage,
totalRecharge,
displayName,
displayAvatar,
login,
fetchUserInfo,
fetchUserProfile,
logout,
}
})
export default useUserStore