前端
This commit is contained in:
152
frontend/app/web-gold/src/stores/user.js
Normal file
152
frontend/app/web-gold/src/stores/user.js
Normal file
@@ -0,0 +1,152 @@
|
||||
import { ref, computed, watch } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { getJSON, setJSON, remove } from '@/utils/storage'
|
||||
|
||||
// 本地持久化的 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 balance = ref(0)
|
||||
const vipLevel = ref(0)
|
||||
const credits = ref(0)
|
||||
|
||||
const displayName = computed(() => nickname.value || wechatNickname.value || '未命名用户')
|
||||
const displayAvatar = computed(() => avatar.value || wechatAvatar.value || '')
|
||||
|
||||
// 恢复本地
|
||||
async function hydrateFromStorage() {
|
||||
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 || ''
|
||||
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()
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
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
|
||||
await remove(STORAGE_KEY)
|
||||
}
|
||||
|
||||
// 初始化从本地恢复
|
||||
hydrateFromStorage()
|
||||
|
||||
return {
|
||||
// state
|
||||
isLoggedIn,
|
||||
userId,
|
||||
nickname,
|
||||
avatar,
|
||||
wechatOpenId,
|
||||
wechatUnionId,
|
||||
wechatNickname,
|
||||
wechatAvatar,
|
||||
balance,
|
||||
vipLevel,
|
||||
credits,
|
||||
// getters
|
||||
displayName,
|
||||
displayAvatar,
|
||||
// actions
|
||||
loginWithPhone,
|
||||
loginWithWeChat,
|
||||
updateBalance,
|
||||
logout,
|
||||
}
|
||||
})
|
||||
|
||||
export default useUserStore
|
||||
|
||||
|
||||
Reference in New Issue
Block a user