Files
sionrui/frontend/app/web-gold/src/stores/user.js
2026-01-18 15:27:43 +08:00

128 lines
3.4 KiB
JavaScript

import { ref, computed, watch } from 'vue'
import { defineStore } from 'pinia'
import { getJSON, setJSON, remove } from '@/utils/storage'
import tokenManager from '@gold/utils/token-manager'
import { getUserInfo, clearUserInfoCache } from '@/api/userinfo'
const STORAGE_KEY = 'user_store_v1'
export const useUserStore = defineStore('user', () => {
const isLoggedIn = 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 || '')
// 持久化数据
const userData = computed(() => ({
isLoggedIn: isLoggedIn.value,
userId: userId.value,
nickname: nickname.value,
avatar: avatar.value,
wechat: wechat.value,
balance: balance.value,
vipLevel: vipLevel.value,
credits: credits.value,
}))
// 自动持久化
watch(userData, (data) => setJSON(STORAGE_KEY, data), { deep: true })
// 初始化从本地恢复
const hydrateFromStorage = async () => {
const saved = await getJSON(STORAGE_KEY)
if (!saved) return
isLoggedIn.value = !!saved.isLoggedIn
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)
}
hydrateFromStorage().then(() => {
isHydrated.value = true
})
// 登录
const login = (profile) => {
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)
}
// 获取用户信息
const fetchUserInfo = async () => {
try {
const userInfo = await getUserInfo()
if (userInfo) {
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) {
console.error('获取用户信息失败:', error)
}
}
// 登出
const logout = async () => {
try {
tokenManager.clearTokens()
clearUserInfoCache()
} catch (e) {
console.error('清空 token 失败:', e)
}
isLoggedIn.value = false
userId.value = ''
nickname.value = ''
avatar.value = ''
wechat.value = { openId: '', unionId: '', nickname: '', avatar: '' }
balance.value = 0
vipLevel.value = 0
credits.value = 0
await remove(STORAGE_KEY)
}
return {
isHydrated,
isLoggedIn,
userId,
nickname,
avatar,
wechat: wechat.value,
balance,
vipLevel,
credits,
displayName,
displayAvatar,
login,
fetchUserInfo,
logout,
}
})
export default useUserStore