2025-04-16 18:51:17 +08:00
|
|
|
|
'use client'
|
|
|
|
|
|
import {Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger} from '@/components/ui/dialog'
|
|
|
|
|
|
import {Button} from '@/components/ui/button'
|
|
|
|
|
|
import alipay from '../_assets/alipay.svg'
|
|
|
|
|
|
import wechat from '../_assets/wechat.svg'
|
|
|
|
|
|
import balance from '../_assets/balance.svg'
|
|
|
|
|
|
import Image from 'next/image'
|
2025-04-18 16:22:50 +08:00
|
|
|
|
import {useContext, useRef, useState} from 'react'
|
2025-04-23 19:00:53 +08:00
|
|
|
|
import {StoreContext, useProfileStore} from '@/components/providers/StoreProvider'
|
2025-04-16 18:51:17 +08:00
|
|
|
|
import {Alert, AlertDescription} from '@/components/ui/alert'
|
|
|
|
|
|
import {
|
|
|
|
|
|
prepareResourceByAlipay,
|
|
|
|
|
|
prepareResourceByWechat,
|
|
|
|
|
|
CreateResourceReq,
|
|
|
|
|
|
CreateResourceResp,
|
|
|
|
|
|
createResourceByBalance,
|
|
|
|
|
|
createResourceByAlipay,
|
|
|
|
|
|
createResourceByWechat,
|
|
|
|
|
|
} from '@/actions/resource'
|
|
|
|
|
|
import {ApiResponse} from '@/lib/api'
|
|
|
|
|
|
import {toast} from 'sonner'
|
|
|
|
|
|
import {Loader} from 'lucide-react'
|
|
|
|
|
|
import {useRouter} from 'next/navigation'
|
2025-04-18 16:22:50 +08:00
|
|
|
|
import * as qrcode from 'qrcode'
|
2025-04-16 18:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
export type PayProps = {
|
|
|
|
|
|
method: 'alipay' | 'wechat' | 'balance'
|
|
|
|
|
|
amount: number
|
|
|
|
|
|
resource: CreateResourceReq
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function Pay(props: PayProps) {
|
|
|
|
|
|
|
2025-04-23 19:00:53 +08:00
|
|
|
|
const profile = useProfileStore(store=>store.profile)
|
|
|
|
|
|
const refreshProfile = useProfileStore(store=>store.refreshProfile)
|
|
|
|
|
|
|
2025-04-16 18:51:17 +08:00
|
|
|
|
const [open, setOpen] = useState(false)
|
|
|
|
|
|
const [payInfo, setPayInfo] = useState<CreateResourceResp | undefined>()
|
2025-04-18 16:22:50 +08:00
|
|
|
|
const canvas = useRef<HTMLCanvasElement>(null)
|
2025-04-16 18:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
const onOpen = async () => {
|
|
|
|
|
|
setOpen(true)
|
|
|
|
|
|
|
|
|
|
|
|
if (props.method === 'balance') {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let resp: ApiResponse<CreateResourceResp>
|
|
|
|
|
|
switch (props.method) {
|
|
|
|
|
|
case 'alipay':
|
|
|
|
|
|
resp = await prepareResourceByAlipay(props.resource)
|
|
|
|
|
|
break
|
|
|
|
|
|
case 'wechat':
|
|
|
|
|
|
resp = await prepareResourceByWechat(props.resource)
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
|
|
toast.error(`创建订单失败: ${resp.message}`)
|
|
|
|
|
|
setOpen(false)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setPayInfo(resp.data)
|
2025-04-18 16:22:50 +08:00
|
|
|
|
await qrcode.toCanvas(canvas.current, resp.data.pay_url)
|
2025-04-16 18:51:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
|
|
let resp: ApiResponse
|
|
|
|
|
|
try {
|
|
|
|
|
|
switch (props.method) {
|
|
|
|
|
|
case 'alipay':
|
2025-04-17 18:30:16 +08:00
|
|
|
|
resp = await createResourceByAlipay({
|
|
|
|
|
|
trade_no: payInfo!.trade_no,
|
|
|
|
|
|
})
|
2025-04-16 18:51:17 +08:00
|
|
|
|
break
|
|
|
|
|
|
case 'wechat':
|
2025-04-17 18:30:16 +08:00
|
|
|
|
resp = await createResourceByWechat({
|
|
|
|
|
|
trade_no: payInfo!.trade_no,
|
|
|
|
|
|
})
|
2025-04-16 18:51:17 +08:00
|
|
|
|
break
|
|
|
|
|
|
case 'balance':
|
|
|
|
|
|
resp = await createResourceByBalance(props.resource)
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
|
|
throw new Error(resp.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
toast.success('购买成功', {
|
|
|
|
|
|
duration: 10 * 1000,
|
|
|
|
|
|
closeButton: true,
|
|
|
|
|
|
action: {
|
|
|
|
|
|
label: `去提取`,
|
|
|
|
|
|
onClick: () => router.push('/admin/extract'),
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
setOpen(false)
|
2025-04-23 19:00:53 +08:00
|
|
|
|
await refreshProfile()
|
2025-04-16 18:51:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (e) {
|
|
|
|
|
|
console.log(e)
|
|
|
|
|
|
toast.error('购买失败', {
|
|
|
|
|
|
description: (e as Error).message,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
|
|
|
|
<DialogTrigger asChild>
|
|
|
|
|
|
<Button className={`mt-4 h-12`} onClick={onOpen}>
|
|
|
|
|
|
立即支付
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</DialogTrigger>
|
|
|
|
|
|
<DialogContent>
|
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
|
<DialogTitle className={`flex gap-2 items-center`}>
|
|
|
|
|
|
{props.method === 'alipay' && (<>
|
|
|
|
|
|
<Image src={alipay} alt={`支付宝`} width={20} height={20}/>
|
|
|
|
|
|
<span>支付宝</span>
|
|
|
|
|
|
</>)}
|
|
|
|
|
|
{props.method === 'wechat' && (<>
|
|
|
|
|
|
<Image src={wechat} alt={`微信`} width={20} height={20}/>
|
|
|
|
|
|
<span>微信</span>
|
|
|
|
|
|
</>)}
|
|
|
|
|
|
{props.method === 'balance' && (<>
|
|
|
|
|
|
<Image src={balance} alt={`余额`} width={20} height={20}/>
|
|
|
|
|
|
<span>余额支付</span>
|
|
|
|
|
|
</>)}
|
|
|
|
|
|
</DialogTitle>
|
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
|
|
|
|
|
|
{props.method === 'balance' ? (
|
2025-04-23 19:00:53 +08:00
|
|
|
|
profile && (
|
2025-04-16 18:51:17 +08:00
|
|
|
|
<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>
|
2025-04-23 19:00:53 +08:00
|
|
|
|
<span className={`text-lg`}>{profile.balance}元</span>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className="flex justify-between items-center">
|
|
|
|
|
|
<span className="text-weak text-sm">支付金额</span>
|
|
|
|
|
|
<span className="text-lg text-accent">- {props.amount}元</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<hr className="my-2"/>
|
|
|
|
|
|
<div className="flex justify-between items-center">
|
|
|
|
|
|
<span className="text-weak text-sm">支付后余额</span>
|
2025-04-23 19:00:53 +08:00
|
|
|
|
<span className={`text-lg ${profile.balance > props.amount ? 'text-done' : `text-fail`}`}>
|
|
|
|
|
|
{profile.balance - props.amount}元
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-04-23 19:00:53 +08:00
|
|
|
|
{profile.balance < props.amount && (
|
2025-04-16 18:51:17 +08:00
|
|
|
|
<Alert variant="fail">
|
|
|
|
|
|
<AlertDescription>
|
|
|
|
|
|
余额不足,请先充值或选择其他支付方式
|
|
|
|
|
|
</AlertDescription>
|
|
|
|
|
|
</Alert>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2025-04-23 19:00:53 +08:00
|
|
|
|
{profile.balance >= props.amount && (
|
2025-04-16 18:51:17 +08:00
|
|
|
|
<Alert>
|
|
|
|
|
|
<AlertDescription>
|
|
|
|
|
|
检查无误后,点击确认支付按钮完成支付
|
|
|
|
|
|
</AlertDescription>
|
|
|
|
|
|
</Alert>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="flex flex-col items-center gap-4">
|
|
|
|
|
|
<div className="flex flex-col items-center gap-3">
|
|
|
|
|
|
<div className="bg-gray-100 w-52 h-52 flex items-center justify-center">
|
|
|
|
|
|
{payInfo ? (
|
2025-04-18 16:22:50 +08:00
|
|
|
|
props.method === 'alipay'
|
|
|
|
|
|
? <iframe
|
|
|
|
|
|
src={payInfo.pay_url}
|
|
|
|
|
|
className="w-full h-full"
|
|
|
|
|
|
title="支付二维码"
|
|
|
|
|
|
/>
|
|
|
|
|
|
: <canvas ref={canvas} className="w-full h-full"/>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
) : (
|
2025-04-18 16:22:50 +08:00
|
|
|
|
<Loader size={40} className={`animate-spin text-weak`}/>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p className="text-sm text-gray-600 text-center">
|
|
|
|
|
|
请使用{props.method === 'alipay' ? '支付宝' : '微信'}扫码支付
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="text-center space-y-1">
|
|
|
|
|
|
<p className="font-medium">支付金额: <span className="text-accent">{props.amount}元</span></p>
|
|
|
|
|
|
<p className="text-xs text-gray-500">订单号: {payInfo?.trade_no || '创建订单中...'}</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
2025-04-23 19:00:53 +08:00
|
|
|
|
disabled={props.method === 'balance' && !!profile && profile.balance < props.amount}
|
2025-04-16 18:51:17 +08:00
|
|
|
|
onClick={onSubmit}
|
|
|
|
|
|
>
|
|
|
|
|
|
{props.method === 'balance' ? '确认支付' : '已完成支付'}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
theme="outline"
|
|
|
|
|
|
onClick={() => setOpen(false)}
|
|
|
|
|
|
>
|
|
|
|
|
|
取消
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
|
</DialogContent>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|