feat: 功能优化

This commit is contained in:
2026-01-17 14:43:42 +08:00
parent 5ed0cfff07
commit fecd47e25d
60 changed files with 3529 additions and 827 deletions

View File

@@ -1,209 +1,122 @@
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 '@gold/hooks/web/useUserInfo'
// 本地持久化的 key
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 wechatOpenId = ref('')
const wechatUnionId = ref('')
const wechatNickname = ref('')
const wechatAvatar = 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 || wechatNickname.value || '未命名用户')
const displayAvatar = computed(() => avatar.value || wechatAvatar.value || '')
const displayName = computed(() => nickname.value || wechat.value.nickname || '未命名用户')
const displayAvatar = computed(() => avatar.value || wechat.value.avatar || '')
// 恢复本地
async function hydrateFromStorage() {
// 持久化数据
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 || ''
wechatOpenId.value = saved.wechatOpenId || ''
wechatUnionId.value = saved.wechatUnionId || ''
wechatNickname.value = saved.wechatNickname || ''
wechatAvatar.value = saved.wechatAvatar || ''
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)
}
// 持久化
async function persist() {
const payload = {
isLoggedIn: isLoggedIn.value,
userId: userId.value,
nickname: nickname.value,
avatar: avatar.value,
wechatOpenId: wechatOpenId.value,
wechatUnionId: wechatUnionId.value,
wechatNickname: wechatNickname.value,
wechatAvatar: wechatAvatar.value,
balance: balance.value,
vipLevel: vipLevel.value,
credits: credits.value,
}
await setJSON(STORAGE_KEY, payload)
}
// 监听关键字段做持久化
;[
isLoggedIn,
userId,
nickname,
avatar,
wechatOpenId,
wechatUnionId,
wechatNickname,
wechatAvatar,
balance,
vipLevel,
credits,
].forEach((s) => watch(s, persist))
// 登录/登出动作(示例:具体接入后端可在此对接)
async function loginWithPhone(payload) {
// payload: { phone, code, profile? }
isLoggedIn.value = true
userId.value = payload?.profile?.userId || userId.value
nickname.value = payload?.profile?.nickname || nickname.value
avatar.value = payload?.profile?.avatar || avatar.value
balance.value = payload?.profile?.balance ?? balance.value
vipLevel.value = payload?.profile?.vipLevel ?? vipLevel.value
credits.value = payload?.profile?.credits ?? credits.value
await persist()
}
async function loginWithWeChat(profile) {
// profile: { openId, unionId, nickname, avatar, balance, vipLevel, credits }
isLoggedIn.value = true
wechatOpenId.value = profile?.openId || ''
wechatUnionId.value = profile?.unionId || ''
wechatNickname.value = profile?.nickname || ''
wechatAvatar.value = profile?.avatar || ''
balance.value = profile?.balance ?? balance.value
vipLevel.value = profile?.vipLevel ?? vipLevel.value
credits.value = profile?.credits ?? credits.value
await persist()
}
async function updateBalance(delta) {
balance.value = Math.max(0, Number(balance.value) + Number(delta || 0))
await persist()
}
/**
* 获取用户信息(从后端)
* 登录成功后调用,更新用户信息
* 使用公共 hook @gold/hooks/web/useUserInfo
*/
async function fetchUserInfo() {
try {
// 使用公共 hook 获取用户信息
const { getUserInfo } = await import('@gold/hooks/web/useUserInfo')
// 导入 tokenManager 获取 token
const tokenManager = (await import('@gold/utils/token-manager')).default
const userInfo = await getUserInfo({
getToken: () => tokenManager.getAccessToken()
})
if (userInfo) {
// 更新用户信息
userId.value = String(userInfo.id || userInfo.userId || userId.value)
nickname.value = userInfo.nickname || nickname.value
avatar.value = userInfo.avatar || avatar.value
// 如果有其他字段,也可以更新
if (userInfo.point !== undefined) credits.value = Number(userInfo.point || 0)
if (userInfo.experience !== undefined) {
// experience 可以映射到其他字段,根据实际需求
}
await persist()
}
} catch (error) {
console.error('获取用户信息失败:', error)
// 不抛出错误,避免影响登录流程
}
}
async function logout() {
// 1. 清空所有 token
try {
tokenManager.clearTokens()
} catch (e) {
console.error('清空 token 失败:', e)
}
// 2. 清空用户信息缓存
try {
const { clearUserInfoCache } = await import('@gold/hooks/web/useUserInfo')
clearUserInfoCache()
} catch (e) {
console.error('清除用户信息缓存失败:', e)
}
// 3. 清空用户信息
isLoggedIn.value = false
userId.value = ''
nickname.value = ''
avatar.value = ''
wechatOpenId.value = ''
wechatUnionId.value = ''
wechatNickname.value = ''
wechatAvatar.value = ''
balance.value = 0
vipLevel.value = 0
credits.value = 0
// 4. 删除本地存储的用户数据
await remove(STORAGE_KEY)
}
// 初始化标志
const isHydrated = ref(false)
// 初始化从本地恢复
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,
// state
isLoggedIn,
userId,
nickname,
avatar,
wechatOpenId,
wechatUnionId,
wechatNickname,
wechatAvatar,
wechat: wechat.value,
balance,
vipLevel,
credits,
// getters
displayName,
displayAvatar,
// actions
loginWithPhone,
loginWithWeChat,
updateBalance,
login,
fetchUserInfo,
logout,
}