2026-03-27 15:51:40 +08:00
|
|
|
|
"use client"
|
|
|
|
|
|
import { format } from "date-fns"
|
|
|
|
|
|
import { Suspense, useState } from "react"
|
|
|
|
|
|
import { toast } from "sonner"
|
|
|
|
|
|
import { deleteCoupon, getPagCoupon } from "@/actions/coupon"
|
2026-04-28 11:16:54 +08:00
|
|
|
|
import { Auth } from "@/components/auth"
|
2026-03-27 15:51:40 +08:00
|
|
|
|
import { DataTable, useDataTable } from "@/components/data-table"
|
2026-04-11 17:12:16 +08:00
|
|
|
|
import { Page } from "@/components/page"
|
2026-03-27 15:51:40 +08:00
|
|
|
|
import {
|
|
|
|
|
|
AlertDialog,
|
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
|
AlertDialogTrigger,
|
|
|
|
|
|
} from "@/components/ui/alert-dialog"
|
|
|
|
|
|
import { Button } from "@/components/ui/button"
|
2026-04-28 11:16:54 +08:00
|
|
|
|
import { ScopeCouponWriteAssign } from "@/lib/scopes"
|
2026-03-27 15:51:40 +08:00
|
|
|
|
import type { Coupon } from "@/models/coupon"
|
|
|
|
|
|
import { CreateDiscount } from "./create"
|
2026-04-28 11:16:54 +08:00
|
|
|
|
import { IssueCoupon } from "./issue"
|
2026-03-27 15:51:40 +08:00
|
|
|
|
import { UpdateCoupon } from "./update"
|
|
|
|
|
|
|
|
|
|
|
|
export default function CouponPage() {
|
|
|
|
|
|
const table = useDataTable((page, size) => getPagCoupon({ page, size }))
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2026-04-11 17:12:16 +08:00
|
|
|
|
<Page>
|
2026-03-27 15:51:40 +08:00
|
|
|
|
<div className="flex justify-between items-stretch">
|
|
|
|
|
|
<div className="flex gap-3">
|
|
|
|
|
|
<CreateDiscount onSuccess={table.refresh} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<Suspense>
|
|
|
|
|
|
<DataTable<Coupon>
|
|
|
|
|
|
{...table}
|
|
|
|
|
|
columns={[
|
2026-04-28 11:16:54 +08:00
|
|
|
|
{ header: "优惠券名称", accessorKey: "name" },
|
|
|
|
|
|
{ header: "优惠券数量", accessorKey: "count" },
|
|
|
|
|
|
{ header: "优惠券金额", accessorKey: "amount" },
|
2026-03-27 15:51:40 +08:00
|
|
|
|
{ header: "最低消费金额", accessorKey: "min_amount" },
|
|
|
|
|
|
{
|
2026-04-28 11:16:54 +08:00
|
|
|
|
header: "优惠券状态",
|
2026-03-27 15:51:40 +08:00
|
|
|
|
accessorKey: "status",
|
|
|
|
|
|
cell: ({ row }) => {
|
|
|
|
|
|
const status = row.original.status
|
|
|
|
|
|
if (status === 0) {
|
2026-04-28 11:16:54 +08:00
|
|
|
|
return <span className="text-yellow-600">禁用</span>
|
2026-03-27 15:51:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (status === 1) {
|
2026-04-28 11:16:54 +08:00
|
|
|
|
return <span className="text-green-600">正常</span>
|
2026-03-27 15:51:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
return <span>-</span>
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-04-28 11:16:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
header: "过期类型",
|
|
|
|
|
|
accessorFn: row => {
|
|
|
|
|
|
switch (row.expire_type) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
return "不过期"
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
return "固定日期"
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
return "相对日期"
|
|
|
|
|
|
default:
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
header: "过期时长(天)",
|
|
|
|
|
|
accessorKey: "expire_in",
|
|
|
|
|
|
},
|
2026-03-27 15:51:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
header: "过期时间",
|
|
|
|
|
|
accessorKey: "expire_at",
|
2026-04-28 11:16:54 +08:00
|
|
|
|
cell: ({ row }) => {
|
|
|
|
|
|
const coupon = row.original
|
|
|
|
|
|
if (coupon.expire_type === 2 && coupon.expire_in) {
|
|
|
|
|
|
const expireDate = new Date(coupon.created_at)
|
|
|
|
|
|
expireDate.setDate(expireDate.getDate() + coupon.expire_in)
|
|
|
|
|
|
return format(expireDate, "yyyy-MM-dd HH:mm")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (coupon.expire_type === 1 && coupon.expire_at) {
|
|
|
|
|
|
return format(new Date(coupon.expire_at), "yyyy-MM-dd HH:mm")
|
|
|
|
|
|
}
|
|
|
|
|
|
return <span>永久有效</span>
|
|
|
|
|
|
},
|
2026-03-27 15:51:40 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
header: "创建时间",
|
|
|
|
|
|
accessorKey: "created_at",
|
|
|
|
|
|
cell: ({ row }) =>
|
|
|
|
|
|
format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
2026-03-28 15:36:08 +08:00
|
|
|
|
id: "action",
|
|
|
|
|
|
meta: { pin: "right" },
|
2026-03-27 15:51:40 +08:00
|
|
|
|
header: "操作",
|
|
|
|
|
|
cell: ({ row }) => (
|
|
|
|
|
|
<div className="flex gap-2">
|
|
|
|
|
|
<UpdateCoupon
|
|
|
|
|
|
coupon={row.original}
|
|
|
|
|
|
onSuccess={table.refresh}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<DeleteCoupon
|
|
|
|
|
|
coupon={row.original}
|
|
|
|
|
|
onSuccess={table.refresh}
|
|
|
|
|
|
/>
|
2026-04-28 11:16:54 +08:00
|
|
|
|
<Auth scope={ScopeCouponWriteAssign}>
|
|
|
|
|
|
<IssueCoupon
|
|
|
|
|
|
coupon={row.original}
|
|
|
|
|
|
onSuccess={table.refresh}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Auth>
|
2026-03-27 15:51:40 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
]}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Suspense>
|
2026-04-11 17:12:16 +08:00
|
|
|
|
</Page>
|
2026-03-27 15:51:40 +08:00
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function DeleteCoupon({
|
|
|
|
|
|
coupon,
|
|
|
|
|
|
onSuccess,
|
|
|
|
|
|
}: {
|
|
|
|
|
|
coupon: Coupon
|
|
|
|
|
|
onSuccess?: () => void
|
|
|
|
|
|
}) {
|
|
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
|
|
|
|
|
|
|
const handleConfirm = async () => {
|
|
|
|
|
|
setLoading(true)
|
|
|
|
|
|
try {
|
|
|
|
|
|
const resp = await deleteCoupon(coupon.id)
|
|
|
|
|
|
if (resp.success) {
|
|
|
|
|
|
toast.success("删除成功")
|
|
|
|
|
|
onSuccess?.()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
toast.error(resp.message ?? "删除失败")
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
const message = error instanceof Error ? error.message : error
|
|
|
|
|
|
toast.error(`接口请求错误: ${message}`)
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<AlertDialog>
|
|
|
|
|
|
<AlertDialogTrigger asChild>
|
|
|
|
|
|
<Button size="sm" variant="destructive" disabled={loading}>
|
|
|
|
|
|
删除
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</AlertDialogTrigger>
|
|
|
|
|
|
<AlertDialogContent size="sm">
|
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogTitle>确认删除</AlertDialogTitle>
|
|
|
|
|
|
<AlertDialogDescription>
|
2026-04-28 11:16:54 +08:00
|
|
|
|
确定要删除折扣「{coupon.name}」吗?此操作不可撤销。
|
2026-03-27 15:51:40 +08:00
|
|
|
|
</AlertDialogDescription>
|
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
|
<AlertDialogCancel>取消</AlertDialogCancel>
|
|
|
|
|
|
<AlertDialogAction variant="destructive" onClick={handleConfirm}>
|
|
|
|
|
|
删除
|
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|