重构认证逻辑,优化登录和用户信息获取流程,新增全局缓存支持

This commit is contained in:
2025-04-23 19:00:53 +08:00
parent 22d3b8f3e3
commit 9473413def
23 changed files with 438 additions and 474 deletions

View File

@@ -6,7 +6,7 @@ import wechat from '../_assets/wechat.svg'
import balance from '../_assets/balance.svg'
import Image from 'next/image'
import {useContext, useRef, useState} from 'react'
import {AuthContext} from '@/components/providers/AuthProvider'
import {StoreContext, useProfileStore} from '@/components/providers/StoreProvider'
import {Alert, AlertDescription} from '@/components/ui/alert'
import {
prepareResourceByAlipay,
@@ -32,7 +32,9 @@ export type PayProps = {
export default function Pay(props: PayProps) {
const ctx = useContext(AuthContext)
const profile = useProfileStore(store=>store.profile)
const refreshProfile = useProfileStore(store=>store.refreshProfile)
const [open, setOpen] = useState(false)
const [payInfo, setPayInfo] = useState<CreateResourceResp | undefined>()
const canvas = useRef<HTMLCanvasElement>(null)
@@ -97,7 +99,7 @@ export default function Pay(props: PayProps) {
})
setOpen(false)
await ctx.refreshProfile()
await refreshProfile()
}
catch (e) {
console.log(e)
@@ -133,12 +135,12 @@ export default function Pay(props: PayProps) {
</DialogHeader>
{props.method === 'balance' ? (
ctx.profile && (
profile && (
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-1">
<div className="flex justify-between items-center">
<span className="text-weak text-sm"></span>
<span className={`text-lg`}>{ctx.profile.balance}</span>
<span className={`text-lg`}>{profile.balance}</span>
</div>
<div className="flex justify-between items-center">
<span className="text-weak text-sm"></span>
@@ -147,13 +149,13 @@ export default function Pay(props: PayProps) {
<hr className="my-2"/>
<div className="flex justify-between items-center">
<span className="text-weak text-sm"></span>
<span className={`text-lg ${ctx.profile.balance > props.amount ? 'text-done' : `text-fail`}`}>
{ctx.profile.balance - props.amount}
<span className={`text-lg ${profile.balance > props.amount ? 'text-done' : `text-fail`}`}>
{profile.balance - props.amount}
</span>
</div>
</div>
{ctx.profile.balance < props.amount && (
{profile.balance < props.amount && (
<Alert variant="fail">
<AlertDescription>
@@ -161,7 +163,7 @@ export default function Pay(props: PayProps) {
</Alert>
)}
{ctx.profile.balance >= props.amount && (
{profile.balance >= props.amount && (
<Alert>
<AlertDescription>
@@ -200,7 +202,7 @@ export default function Pay(props: PayProps) {
<DialogFooter>
<Button
type="button"
disabled={props.method === 'balance' && !!ctx.profile && ctx.profile.balance < props.amount}
disabled={props.method === 'balance' && !!profile && profile.balance < props.amount}
onClick={onSubmit}
>
{props.method === 'balance' ? '确认支付' : '已完成支付'}