Files
sionrui/frontend/app/web-gold/src/stores/user.js

139 lines
3.6 KiB
JavaScript
Raw Normal View History

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'
import tokenManager from '@gold/utils/token-manager'
2026-01-18 15:27:43 +08:00
import { getUserInfo, clearUserInfoCache } from '@/api/userinfo'
2026-02-23 02:05:48 +08:00
import { getUserProfile } from '@/api/auth'
2025-11-10 00:59:40 +08:00
const STORAGE_KEY = 'user_store_v1'
export const useUserStore = defineStore('user', () => {
const isLoggedIn = ref(false)
2026-02-23 02:05:48 +08:00
const isHydrated = ref(false)
// 基础信息
2025-11-10 00:59:40 +08:00
const userId = ref('')
const nickname = ref('')
const avatar = ref('')
2026-02-23 02:27:32 +08:00
const mobile = ref('')
2026-01-17 14:43:42 +08:00
2026-02-23 02:05:48 +08:00
// 档案数据(来自 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 || '')
2026-01-17 14:43:42 +08:00
// 持久化数据
const userData = computed(() => ({
isLoggedIn: isLoggedIn.value,
userId: userId.value,
nickname: nickname.value,
avatar: avatar.value,
2026-02-23 02:27:32 +08:00
mobile: mobile.value,
2026-02-23 02:05:48 +08:00
profile: profile.value,
2026-01-17 14:43:42 +08:00
}))
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-02-23 02:27:32 +08:00
mobile.value = saved.mobile || ''
2026-02-23 02:05:48 +08:00
profile.value = saved.profile || null
2025-11-10 00:59:40 +08:00
}
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
// 登录
2026-02-23 02:05:48 +08:00
const login = (data) => {
2025-11-10 00:59:40 +08:00
isLoggedIn.value = true
2026-02-23 02:05:48 +08:00
userId.value = String(data.userId || data.id || '')
nickname.value = data.nickname || ''
avatar.value = data.avatar || ''
2026-02-23 02:27:32 +08:00
mobile.value = data.mobile || ''
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 || ''
2026-02-23 02:27:32 +08:00
mobile.value = userInfo.mobile || ''
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-02-23 02:05:48 +08:00
// 获取用户档案
const fetchUserProfile = async () => {
try {
const profileData = await getUserProfile()
if (profileData) {
profile.value = profileData
}
} 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 {
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-02-23 02:27:32 +08:00
mobile.value = ''
2026-02-23 02:05:48 +08:00
profile.value = null
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-02-23 02:27:32 +08:00
mobile,
2026-02-23 02:05:48 +08:00
profile,
remainingPoints,
remainingStorage,
usedStorage,
totalStorage,
totalRecharge,
2025-11-10 00:59:40 +08:00
displayName,
displayAvatar,
2026-01-17 14:43:42 +08:00
login,
2025-11-12 22:45:29 +08:00
fetchUserInfo,
2026-02-23 02:05:48 +08:00
fetchUserProfile,
2025-11-10 00:59:40 +08:00
logout,
}
})
export default useUserStore