首页接口更新&&支付页面调试

This commit is contained in:
Eamon-meng
2025-06-23 18:59:31 +08:00
parent f15cefff4d
commit e4556bfe60
11 changed files with 232 additions and 209 deletions

View File

@@ -3,45 +3,19 @@ import {DialogContent, DialogHeader, DialogTitle} from '@/components/ui/dialog'
import {Button} from '@/components/ui/button'
import {completeResource} from '@/actions/resource'
import {toast} from 'sonner'
import {CheckCircle, Loader} from 'lucide-react'
import {Loader} from 'lucide-react'
import {useState, useEffect, useRef} from 'react'
import * as qrcode from 'qrcode'
import Image from 'next/image'
import {TradeMethod} from '@/lib/models/trade'
import {PaymentModalProps} from './payment-modal'
export function DesktopPayment(props: PaymentModalProps) {
console.log(props, 'props')
const [paymentVerified, setPaymentVerified] = useState(false)
const [loading, setLoading] = useState(false)
const canvasRef = useRef<HTMLCanvasElement>(null)
useEffect(() => {
if (!canvasRef.current || props.method === TradeMethod.Alipay) return
qrcode.toCanvas(canvasRef.current, props.pay_url, {width: 200})
.catch((err) => {
console.error('生成二维码失败:', err)
toast.error('生成支付二维码失败')
})
}, [props.method, props.pay_url])
const handleComplete = async () => {
const onSubmit = async () => {
setLoading(true)
try {
const resp = await completeResource({trade_no: props.inner_no})
if (!resp.success) throw new Error(resp.message)
toast.success('支付成功')
setPaymentVerified(true)
setTimeout(() => props.onSuccess?.(), 2000)
}
catch (e) {
toast.error('支付验证失败', {description: (e as Error).message})
}
finally {
setLoading(false)
}
await props.onConfirm?.()
setLoading(false)
}
console.log(props.decoration.text, 'props.decoration.text')
return (
<DialogContent>
@@ -62,52 +36,44 @@ export function DesktopPayment(props: PaymentModalProps) {
</DialogTitle>
</DialogHeader>
{paymentVerified ? (
<div className="text-center py-8">
<CheckCircle className="mx-auto text-green-500 w-12 h-12"/>
<p className="mt-4 text-lg font-medium"></p>
</div>
) : (
<div className="flex flex-col items-center gap-4">
<div className="bg-gray-100 w-52 h-52 flex items-center justify-center">
{props.method === TradeMethod.Alipay ? (
<iframe src={props.pay_url} className="w-full h-full"/>
) : (
<canvas ref={canvasRef} className="w-full h-full"/>
)}
</div>
<p className="text-sm text-gray-600">
使
{props.decoration.text}
<div className="flex flex-col items-center gap-4">
<Image
src={props.pay_url}
width={208}
height={208}
alt="二维码"
/>
<p className="text-sm text-gray-600">
使
{props.decoration.text}
</p>
<div className="w-full text-center space-y-2">
<p className="text-sm font-medium">
:
{' '}
<span className="text-accent">
¥
{props.amount?.toFixed(2) || '0.00'}
</span>
</p>
<p className="text-xs text-gray-500">
:
{props.inner_no}
</p>
<div className="w-full text-center space-y-2">
<p className="text-sm font-medium">
:
{' '}
<span className="text-accent">
¥
{props.amount?.toFixed(2) || '0.00'}
</span>
</p>
<p className="text-xs text-gray-500">
:
{props.inner_no}
</p>
</div>
<div className="flex gap-4 w-full justify-center">
<Button onClick={handleComplete} >
{loading && <Loader className="animate-spin mr-2"/>}
</Button>
<Button theme="outline" onClick={() => props.onClose?.()}>
</Button>
</div>
</div>
)}
<div className="flex gap-4 w-full justify-center">
<Button onClick={onSubmit}>
{loading && <Loader className="animate-spin mr-2"/>}
</Button>
<Button theme="outline" onClick={() => props.onClose?.()}>
</Button>
</div>
</div>
</DialogContent>
)
}

View File

@@ -9,91 +9,62 @@ import Image from 'next/image'
import {PaymentModalProps} from './payment-modal'
export function MobilePayment(props: PaymentModalProps) {
console.log(props, 'props')
const [paymentVerified, setPaymentVerified] = useState(false)
const [loading, setLoading] = useState(false)
const handleComplete = async () => {
setLoading(true)
try {
const resp = await completeResource({trade_no: props.inner_no})
if (!resp.success) throw new Error(resp.message)
toast.success('支付成功')
setPaymentVerified(true)
props.onClose?.()
}
catch (e) {
toast.error('支付验证失败', {description: (e as Error).message})
}
finally {
setLoading(false)
}
}
return (
<DialogContent className="max-w-[95vw] rounded-lg">
{paymentVerified ? (
<div className="py-6 text-center">
<CheckCircle className="mx-auto h-14 w-14 text-green-500"/>
<p className="mt-4 text-lg font-medium"></p>
<div className="flex flex-col gap-6">
{/* 支付确认信息 */}
<div className="text-center">
<div className="mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-blue-50 text-blue-500">
<CreditCard className="h-8 w-8"/>
</div>
<h3 className="text-lg font-semibold text-gray-800"></h3>
<p className="text-gray-500"></p>
</div>
) : (
<div className="flex flex-col gap-6">
{/* 支付确认信息 */}
<div className="text-center">
<div className="mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-blue-50 text-blue-500">
<CreditCard className="h-8 w-8"/>
</div>
<h3 className="text-lg font-semibold text-gray-800"></h3>
<p className="text-gray-500"></p>
</div>
{/* 支付详情 */}
<div className="space-y-4 rounded-lg bg-gray-50 p-4">
<div className="flex justify-between">
<span className="text-gray-600"></span>
<span className="font-medium">{props.inner_no}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600"></span>
<div className="flex items-center gap-2">
{props.decoration.icon && (
<Image
src={props.decoration.icon}
alt={props.decoration.text}
width={28}
height={28}
className="rounded-md"
/>
)}
<span>{props.decoration.text}</span>
</div>
</div>
<div className="flex justify-between">
<span className="text-gray-600"></span>
<span className="text-lg font-bold text-blue-600">
¥
{' '}
{props.amount.toFixed(2)}
</span>
{/* 支付详情 */}
<div className="space-y-4 rounded-lg bg-gray-50 p-4">
<div className="flex justify-between">
<span className="text-gray-600"></span>
<span className="font-medium">{props.inner_no}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600"></span>
<div className="flex items-center gap-2">
{props.decoration.icon && (
<Image
src={props.decoration.icon}
alt={props.decoration.text}
width={28}
height={28}
className="rounded-md"
/>
)}
<span>{props.decoration.text}</span>
</div>
</div>
{/* 操作按钮 */}
<div className="flex gap-3">
<Button
theme="outline"
className="flex-1 py-3 text-base"
onClick={props.onClose} >
</Button>
<Button className="flex-1 py-3 text-base" onClick={() => window.location.href = props.pay_url} >
</Button>
<div className="flex justify-between">
<span className="text-gray-600"></span>
<span className="text-lg font-bold text-blue-600">
¥
{' '}
{props.amount.toFixed(2)}
</span>
</div>
</div>
)}
{/* 操作按钮 */}
<div className="flex gap-3">
<Button
theme="outline"
className="flex-1 py-3 text-base"
onClick={props.onClose} >
</Button>
<Button className="flex-1 py-3 text-base" onClick={() => window.location.href = props.pay_url} >
</Button>
</div>
</div>
</DialogContent>
)
}

View File

@@ -38,7 +38,7 @@ export function PaymentButton({
{trade && (
<PaymentModal
{...trade}
onSuccess={onSuccess}
onConfirm={onSuccess}
/>
)}
</>

View File

@@ -6,17 +6,11 @@ import {Dialog} from '@/components/ui/dialog'
import {PaymentProps} from './type'
export type PaymentModalProps = {
onSuccess?: () => void
onConfirm?: () => Promise<void>
onClose?: () => void
} & PaymentProps
export function PaymentModal(props: PaymentModalProps) {
const handleClose = (success: boolean) => {
if (success && props.onSuccess) {
props.onSuccess()
}
}
return (
<Dialog
defaultOpen={true}

View File

@@ -25,8 +25,6 @@ export type PayProps = {
}
export default function Pay(props: PayProps) {
console.log(props, 'props')
const profile = useProfileStore(store => store.profile)
const refreshProfile = useProfileStore(store => store.refreshProfile)
const [open, setOpen] = useState(false)
@@ -44,7 +42,6 @@ export default function Pay(props: PayProps) {
: props.method === 'alipay'
? TradeMethod.SftAlipay
: TradeMethod.SftWechat
console.log(method, 'methodConfig')
const res = {
...props.resource,
@@ -111,7 +108,6 @@ export default function Pay(props: PayProps) {
</Button>
{/* 余额支付对话框 */}
{/* 余额支付对话框 */}
{props.method === 'balance' && (
<Dialog open={open} onOpenChange={setOpen}>
@@ -186,11 +182,21 @@ export default function Pay(props: PayProps) {
{props.method !== 'balance' && trade && (
<PaymentModal
{...trade}
onSuccess={() => {
toast.success('支付成功')
setTrade(null)
setOpen(false)
refreshProfile()
onConfirm={async () => {
try {
const resp = await completeResource({trade_no: trade.inner_no})
if (!resp.success) {
throw new Error(resp.message)
}
toast.success('支付成功')
setTrade(null)
setOpen(false)
refreshProfile()
}
catch (e) {
toast.error('支付验证失败', {description: (e as Error).message})
}
}}
onClose={() => {
setTrade(null)

View File

@@ -14,7 +14,7 @@ import FormOption from '@/components/composites/purchase/option'
import {RadioGroup} from '@/components/ui/radio-group'
import {zodResolver} from '@hookform/resolvers/zod'
import {toast} from 'sonner'
import {useMemo, useState} from 'react'
import {useState} from 'react'
import {RechargePrepare} from '@/actions/user'
import {useProfileStore} from '@/components/stores-provider'
import {merge} from '@/lib/utils'
@@ -75,6 +75,7 @@ export default function RechargeModal(props: RechargeModelProps) {
console.log(req, '请求参数')
const result = await RechargePrepare(req)
console.log(result, 'result返回结果')
if (result.success) {
setTrade({
@@ -98,15 +99,22 @@ export default function RechargeModal(props: RechargeModelProps) {
}
const handlePaymentSuccess = async () => {
if (!trade) return
try {
await refreshProfile()
const resp = await RechargeComplete({trade_no: trade.inner_no})
if (!resp.success) {
throw new Error(resp.message)
}
toast.success('充值成功')
setTrade(undefined) // 清除交易状态
setOpen(false) // 关闭弹窗
form.reset() // 重置表单
await refreshProfile()
}
catch (e) {
toast.error('刷新账户信息失败')
toast.error('支付验证失败', {description: (e as Error).message})
}
}
@@ -217,7 +225,7 @@ export default function RechargeModal(props: RechargeModelProps) {
) : (
<PaymentModal
{...trade}
onSuccess={handlePaymentSuccess}
onConfirm={handlePaymentSuccess}
onClose={handleClose}
/>
)}