feat(wallet): add runtime balance flow and role-gated order posting
This commit is contained in:
@@ -11,12 +11,13 @@ import { Input } from "@/components/ui/input"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { Separator } from "@/components/ui/separator"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import { mockPlayers, mockServices, walletBalance } from "@/lib/mock"
|
||||
import { mockPlayers, mockServices } from "@/lib/mock"
|
||||
import { notifySuccess } from "@/lib/toast"
|
||||
import { useRequireAuth } from "@/lib/use-require-auth"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useChatStore } from "@/store/chat"
|
||||
import { useOrderStore } from "@/store/orders"
|
||||
import { useWalletStore } from "@/store/wallet"
|
||||
|
||||
export default function NewOrderPage() {
|
||||
const router = useRouter()
|
||||
@@ -24,6 +25,8 @@ export default function NewOrderPage() {
|
||||
const { requireAuth } = useRequireAuth()
|
||||
const createOrder = useOrderStore((state) => state.createOrder)
|
||||
const ensureOrderSession = useChatStore((state) => state.ensureOrderSession)
|
||||
const balance = useWalletStore((state) => state.balance)
|
||||
const deductBalance = useWalletStore((state) => state.deductBalance)
|
||||
const serviceId = searchParams.get("serviceId")
|
||||
|
||||
const service = mockServices.find((s) => s.id === serviceId)
|
||||
@@ -163,9 +166,9 @@ export default function NewOrderPage() {
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
||||
<CreditCard className="h-4 w-4" />
|
||||
<span>钱包余额: ¥{walletBalance}</span>
|
||||
{walletBalance < totalPrice && <span className="text-destructive">(余额不足)</span>}
|
||||
{walletBalance < totalPrice && (
|
||||
<span>钱包余额: ¥{balance.toFixed(2)}</span>
|
||||
{balance < totalPrice && <span className="text-destructive">(余额不足)</span>}
|
||||
{balance < totalPrice && (
|
||||
<Button variant="outline" size="sm" asChild>
|
||||
<Link href="/wallet">充值</Link>
|
||||
</Button>
|
||||
@@ -181,7 +184,7 @@ export default function NewOrderPage() {
|
||||
<Button
|
||||
className="w-full"
|
||||
size="lg"
|
||||
disabled={walletBalance < totalPrice}
|
||||
disabled={balance < totalPrice}
|
||||
onClick={() =>
|
||||
requireAuth(async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500))
|
||||
@@ -201,6 +204,11 @@ export default function NewOrderPage() {
|
||||
status: "pending_accept",
|
||||
})
|
||||
|
||||
const paid = deductBalance(order.id, totalPrice)
|
||||
if (!paid) {
|
||||
return
|
||||
}
|
||||
|
||||
ensureOrderSession(order)
|
||||
setSubmitted(true)
|
||||
notifySuccess("下单成功")
|
||||
|
||||
Reference in New Issue
Block a user