2025-11-10 00:59:40 +08:00
|
|
|
import { ref, computed, watch } from 'vue'
|
|
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
import { getJSON, setJSON, remove } from '@/utils/storage'
|
2025-11-25 00:58:51 +08:00
|
|
|
import tokenManager from '@gold/utils/token-manager'
|
2026-01-18 15:27:43 +08:00
|
|
|
import { getUserInfo, clearUserInfoCache } from '@/api/userinfo'
|
2025-11-10 00:59:40 +08:00
|
|
|
|
|
|
|
|
const STORAGE_KEY = 'user_store_v1'
|
|
|
|
|
|
|
|
|
|
export const useUserStore = defineStore('user', () => {
|
|
|
|
|
const isLoggedIn = ref(false)
|
|
|
|
|
const userId = ref('')
|
|
|
|
|
const nickname = ref('')
|
|
|
|
|
const avatar = ref('')
|
2026-01-17 14:43:42 +08:00
|
|
|
const wechat = ref({ openId: '', unionId: '', nickname: '', avatar: '' })
|
2025-11-10 00:59:40 +08:00
|
|
|
const balance = ref(0)
|
|
|
|
|
const vipLevel = ref(0)
|
|
|
|
|
const credits = ref(0)
|
2026-01-17 14:43:42 +08:00
|
|
|
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,
|
|
|
|
|
}))
|
2025-11-10 00:59:40 +08:00
|
|
|
|
2026-01-17 14:43:42 +08:00
|
|
|
// 自动持久化
|
|
|
|
|
watch(userData, (data) => setJSON(STORAGE_KEY, data), { deep: true })
|
2025-11-10 00:59:40 +08:00
|
|
|
|
2026-01-17 14:43:42 +08:00
|
|
|
// 初始化从本地恢复
|
|
|
|
|
const hydrateFromStorage = async () => {
|
2025-11-10 00:59:40 +08:00
|
|
|
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 || ''
|
2026-01-17 14:43:42 +08:00
|
|
|
wechat.value = saved.wechat || { openId: '', unionId: '', nickname: '', avatar: '' }
|
2025-11-10 00:59:40 +08:00
|
|
|
balance.value = Number(saved.balance || 0)
|
|
|
|
|
vipLevel.value = Number(saved.vipLevel || 0)
|
|
|
|
|
credits.value = Number(saved.credits || 0)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 14:43:42 +08:00
|
|
|
hydrateFromStorage().then(() => {
|
|
|
|
|
isHydrated.value = true
|
|
|
|
|
})
|
2025-11-10 00:59:40 +08:00
|
|
|
|
2026-01-17 14:43:42 +08:00
|
|
|
// 登录
|
|
|
|
|
const login = (profile) => {
|
2025-11-10 00:59:40 +08:00
|
|
|
isLoggedIn.value = true
|
2026-01-17 14:43:42 +08:00
|
|
|
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)
|
2025-11-10 00:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 14:43:42 +08:00
|
|
|
// 获取用户信息
|
|
|
|
|
const fetchUserInfo = async () => {
|
2025-11-12 22:45:29 +08:00
|
|
|
try {
|
2026-01-17 14:43:42 +08:00
|
|
|
const userInfo = await getUserInfo()
|
2025-11-12 22:45:29 +08:00
|
|
|
if (userInfo) {
|
2026-01-17 14:43:42 +08:00
|
|
|
userId.value = String(userInfo.id || userInfo.userId || '')
|
|
|
|
|
nickname.value = userInfo.nickname || ''
|
|
|
|
|
avatar.value = userInfo.avatar || ''
|
2025-11-12 22:45:29 +08:00
|
|
|
if (userInfo.point !== undefined) credits.value = Number(userInfo.point || 0)
|
2026-01-17 14:43:42 +08:00
|
|
|
isLoggedIn.value = true
|
2025-11-12 22:45:29 +08:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取用户信息失败:', error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 14:43:42 +08:00
|
|
|
// 登出
|
|
|
|
|
const logout = async () => {
|
2025-11-12 22:45:29 +08:00
|
|
|
try {
|
2025-11-25 00:58:51 +08:00
|
|
|
tokenManager.clearTokens()
|
2025-12-21 22:24:16 +08:00
|
|
|
clearUserInfoCache()
|
|
|
|
|
} catch (e) {
|
2026-01-17 14:43:42 +08:00
|
|
|
console.error('清空 token 失败:', e)
|
2025-12-21 22:24:16 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-10 00:59:40 +08:00
|
|
|
isLoggedIn.value = false
|
|
|
|
|
userId.value = ''
|
|
|
|
|
nickname.value = ''
|
|
|
|
|
avatar.value = ''
|
2026-01-17 14:43:42 +08:00
|
|
|
wechat.value = { openId: '', unionId: '', nickname: '', avatar: '' }
|
2025-11-10 00:59:40 +08:00
|
|
|
balance.value = 0
|
|
|
|
|
vipLevel.value = 0
|
|
|
|
|
credits.value = 0
|
2025-11-25 00:58:51 +08:00
|
|
|
|
2025-11-10 00:59:40 +08:00
|
|
|
await remove(STORAGE_KEY)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2025-11-12 22:45:29 +08:00
|
|
|
isHydrated,
|
2025-11-10 00:59:40 +08:00
|
|
|
isLoggedIn,
|
|
|
|
|
userId,
|
|
|
|
|
nickname,
|
|
|
|
|
avatar,
|
2026-01-17 14:43:42 +08:00
|
|
|
wechat: wechat.value,
|
2025-11-10 00:59:40 +08:00
|
|
|
balance,
|
|
|
|
|
vipLevel,
|
|
|
|
|
credits,
|
|
|
|
|
displayName,
|
|
|
|
|
displayAvatar,
|
2026-01-17 14:43:42 +08:00
|
|
|
login,
|
2025-11-12 22:45:29 +08:00
|
|
|
fetchUserInfo,
|
2025-11-10 00:59:40 +08:00
|
|
|
logout,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default useUserStore
|
|
|
|
|
|
|
|
|
|
|