This commit is contained in:
2026-02-03 11:50:59 +08:00
parent a7d781a25b
commit 9a08273541
7 changed files with 213 additions and 383 deletions

View File

@@ -193,6 +193,8 @@ interface StoreState {
getLiveQRCodeUrl: (qrId: string) => string | null
exportData: () => string
fetchSettings: () => Promise<void>
/** 从服务端刷新当前用户资料(含可提现金额等),进入「我的」页时调用 */
refreshUserProfile: () => Promise<void>
}
const initialSettings: Settings = {
@@ -542,6 +544,31 @@ export const useStore = create<StoreState>()(
}
},
refreshUserProfile: async () => {
const { user } = get()
if (!user?.id) return
try {
const res = await fetch(`/api/user/profile?userId=${encodeURIComponent(user.id)}`)
const json = await res.json()
if (!json.success || !json.data) return
const d = json.data
set((state) =>
state.user
? {
user: {
...state.user,
earnings: typeof d.earnings === 'number' ? d.earnings : parseFloat(d.earnings) || state.user.earnings,
pendingEarnings: typeof d.pendingEarnings === 'number' ? d.pendingEarnings : parseFloat(d.pendingEarnings) || state.user.pendingEarnings,
referralCount: typeof d.referralCount === 'number' ? d.referralCount : Number(d.referralCount) || state.user.referralCount,
},
}
: state
)
} catch (e) {
console.warn('[Store] refreshUserProfile failed', e)
}
},
deleteUser: (userId: string) => {
if (typeof window === "undefined") return
const users = JSON.parse(localStorage.getItem("users") || "[]") as User[]