2025-04-09 17:17:39 +08:00
|
|
|
|
'use client'
|
2025-04-10 17:49:02 +08:00
|
|
|
|
import {useEffect, useState} from 'react'
|
2025-04-09 17:17:39 +08:00
|
|
|
|
import {PageRecord} from '@/lib/api'
|
2025-04-11 17:34:42 +08:00
|
|
|
|
import {Resource} from '@/lib/models'
|
2025-04-09 17:17:39 +08:00
|
|
|
|
import {useStatus} from '@/lib/states'
|
|
|
|
|
|
import {listResourcePss} from '@/actions/resource'
|
2025-04-11 17:34:42 +08:00
|
|
|
|
import {Box, Eraser, Search, Timer} from 'lucide-react'
|
2025-04-10 17:49:02 +08:00
|
|
|
|
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from '@/components/ui/select'
|
|
|
|
|
|
import {Button} from '@/components/ui/button'
|
2025-04-11 17:34:42 +08:00
|
|
|
|
import DataTable from '@/components/data-table'
|
2025-04-10 17:49:02 +08:00
|
|
|
|
import {format, intlFormatDistance, isAfter, isEqual, parse} from 'date-fns'
|
2025-04-11 17:34:42 +08:00
|
|
|
|
import DatePicker from '@/components/date-picker'
|
2025-04-10 17:49:02 +08:00
|
|
|
|
import {Form, FormField} from '@/components/ui/form'
|
|
|
|
|
|
import {useForm} from 'react-hook-form'
|
|
|
|
|
|
import zod from 'zod'
|
|
|
|
|
|
import {zodResolver} from '@hookform/resolvers/zod'
|
|
|
|
|
|
import {Label} from '@/components/ui/label'
|
2025-04-11 17:34:42 +08:00
|
|
|
|
import {Input} from '@/components/ui/input'
|
|
|
|
|
|
import Page from '@/components/page'
|
|
|
|
|
|
import {useSearchParams} from 'next/navigation'
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
2025-04-09 17:17:39 +08:00
|
|
|
|
export type ResourcesPageProps = {}
|
|
|
|
|
|
|
|
|
|
|
|
export default function ResourcesPage(props: ResourcesPageProps) {
|
|
|
|
|
|
|
2025-04-10 17:49:02 +08:00
|
|
|
|
// ======================
|
|
|
|
|
|
// 查询
|
|
|
|
|
|
// ======================
|
2025-04-09 17:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
const [status, setStatus] = useStatus()
|
2025-04-11 17:34:42 +08:00
|
|
|
|
const [data, setData] = useState<PageRecord<Resource>>({
|
2025-04-09 17:17:39 +08:00
|
|
|
|
page: 1,
|
|
|
|
|
|
size: 10,
|
|
|
|
|
|
total: 0,
|
|
|
|
|
|
list: [],
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const refresh = async (page: number, size: number) => {
|
|
|
|
|
|
setStatus('load')
|
|
|
|
|
|
try {
|
2025-04-10 17:49:02 +08:00
|
|
|
|
const type = {
|
|
|
|
|
|
all: undefined,
|
|
|
|
|
|
expire: 1,
|
|
|
|
|
|
quota: 2,
|
|
|
|
|
|
}[form.getValues('type')]
|
|
|
|
|
|
const create_after = form.getValues('create_after')
|
|
|
|
|
|
const create_before = form.getValues('create_before')
|
|
|
|
|
|
const expire_after = form.getValues('expire_after')
|
|
|
|
|
|
const expire_before = form.getValues('expire_before')
|
2025-04-11 17:34:42 +08:00
|
|
|
|
const resource_no = form.getValues('resource_no')
|
2025-04-10 17:49:02 +08:00
|
|
|
|
|
|
|
|
|
|
const res = await listResourcePss({
|
2025-04-11 17:34:42 +08:00
|
|
|
|
page, size,
|
|
|
|
|
|
type,
|
|
|
|
|
|
create_after,
|
|
|
|
|
|
create_before,
|
|
|
|
|
|
expire_after,
|
|
|
|
|
|
expire_before,
|
|
|
|
|
|
resource_no,
|
2025-04-10 17:49:02 +08:00
|
|
|
|
})
|
2025-04-09 17:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
if (res.success) {
|
2025-04-11 17:34:42 +08:00
|
|
|
|
console.log(res.data)
|
2025-04-09 17:17:39 +08:00
|
|
|
|
setData(res.data)
|
2025-04-10 17:49:02 +08:00
|
|
|
|
setStatus('done')
|
2025-04-09 17:17:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
throw new Error('Failed to load resource pss')
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (e) {
|
|
|
|
|
|
setStatus('fail')
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
refresh(1, 10).then()
|
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
2025-04-10 17:49:02 +08:00
|
|
|
|
// ======================
|
|
|
|
|
|
// 筛选
|
|
|
|
|
|
// ======================
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
2025-04-11 17:34:42 +08:00
|
|
|
|
const filterSchema = zod.object({
|
|
|
|
|
|
resource_no: zod.string().optional().default(''),
|
|
|
|
|
|
type: zod.enum(['expire', 'quota', 'all']).default('all'),
|
|
|
|
|
|
create_after: zod.date().optional(),
|
|
|
|
|
|
create_before: zod.date().optional(),
|
|
|
|
|
|
expire_after: zod.date().optional(),
|
|
|
|
|
|
expire_before: zod.date().optional(),
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
type FilterSchema = zod.infer<typeof filterSchema>
|
|
|
|
|
|
|
|
|
|
|
|
const params = useSearchParams()
|
|
|
|
|
|
let paramType = params.get('type')
|
|
|
|
|
|
if (paramType != 'all' && paramType != 'expire' && paramType != 'quota') {
|
|
|
|
|
|
paramType = 'all'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-10 17:49:02 +08:00
|
|
|
|
const form = useForm<FilterSchema>({
|
|
|
|
|
|
resolver: zodResolver(filterSchema),
|
|
|
|
|
|
defaultValues: {
|
2025-04-11 17:34:42 +08:00
|
|
|
|
resource_no: params.get('resource_no') || '',
|
|
|
|
|
|
type: paramType as 'expire' | 'quota' | 'all',
|
|
|
|
|
|
create_after: params.get('create_after') ? new Date(params.get('create_after')!) : undefined,
|
|
|
|
|
|
create_before: params.get('create_before') ? new Date(params.get('create_before')!) : undefined,
|
|
|
|
|
|
expire_after: params.get('expire_after') ? new Date(params.get('expire_after')!) : undefined,
|
|
|
|
|
|
expire_before: params.get('expire_before') ? new Date(params.get('expire_before')!) : undefined,
|
2025-04-10 17:49:02 +08:00
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const onSubmit = async (value: FilterSchema) => {
|
2025-04-11 17:34:42 +08:00
|
|
|
|
await refresh(1, data.size)
|
2025-04-10 17:49:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ======================
|
2025-04-09 17:17:39 +08:00
|
|
|
|
// render
|
2025-04-10 17:49:02 +08:00
|
|
|
|
// ======================
|
|
|
|
|
|
|
2025-04-08 11:21:58 +08:00
|
|
|
|
return (
|
2025-04-11 17:34:42 +08:00
|
|
|
|
<Page>
|
2025-04-10 17:49:02 +08:00
|
|
|
|
|
|
|
|
|
|
{/* 操作区 */}
|
|
|
|
|
|
<section className={`flex justify-between flex-wrap`}>
|
|
|
|
|
|
<div>
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-04-11 17:34:42 +08:00
|
|
|
|
<Form form={form} onSubmit={onSubmit} className={`flex items-end gap-4 flex-wrap`}>
|
|
|
|
|
|
<FormField name={`resource_no`} label={<span className={`text-sm`}>套餐编号</span>}>
|
|
|
|
|
|
{({id, field}) => (
|
|
|
|
|
|
<Input {...field} id={id} className={`h-9`}/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</FormField>
|
2025-04-10 17:49:02 +08:00
|
|
|
|
<FormField name={`type`} label={<span className={`text-sm`}>类型</span>}>
|
|
|
|
|
|
{({id, field}) => (
|
|
|
|
|
|
<Select value={field.value} onValueChange={field.onChange}>
|
|
|
|
|
|
<SelectTrigger className={`w-24`}>
|
|
|
|
|
|
<SelectValue placeholder={`选择套餐类型`}/>
|
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
|
<SelectContent>
|
|
|
|
|
|
<SelectItem value={`all`}>全部</SelectItem>
|
|
|
|
|
|
<SelectItem value={`expire`}>包时</SelectItem>
|
|
|
|
|
|
<SelectItem value={`quota`}>包量</SelectItem>
|
|
|
|
|
|
</SelectContent>
|
|
|
|
|
|
</Select>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</FormField>
|
|
|
|
|
|
<div className={`flex flex-col gap-2`}>
|
|
|
|
|
|
<Label className={`text-sm`}>开通时间</Label>
|
|
|
|
|
|
<div className={`flex items-center`}>
|
|
|
|
|
|
<FormField name={`create_after`}>
|
|
|
|
|
|
{({field}) => (
|
|
|
|
|
|
<DatePicker
|
|
|
|
|
|
{...field}
|
|
|
|
|
|
className={`w-36`}
|
|
|
|
|
|
placeholder={`开始时间`}
|
|
|
|
|
|
format={`yyyy-MM-dd`}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</FormField>
|
|
|
|
|
|
<span className={`px-1`}>-</span>
|
|
|
|
|
|
<FormField name={`create_before`}>
|
|
|
|
|
|
{({field}) => (
|
|
|
|
|
|
<DatePicker
|
|
|
|
|
|
{...field}
|
|
|
|
|
|
className={`w-36`}
|
|
|
|
|
|
placeholder={`结束时间`}
|
|
|
|
|
|
format={`yyyy-MM-dd`}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</FormField>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className={`flex flex-col gap-2`}>
|
|
|
|
|
|
<Label className={`text-sm`}>最后使用时间</Label>
|
|
|
|
|
|
<div className={`flex items-center`}>
|
|
|
|
|
|
<FormField name={`expire_after`}>
|
|
|
|
|
|
{({field}) => (
|
|
|
|
|
|
<DatePicker
|
|
|
|
|
|
{...field}
|
|
|
|
|
|
className={`w-36`}
|
|
|
|
|
|
placeholder={`开始时间`}
|
|
|
|
|
|
format={`yyyy-MM-dd`}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</FormField>
|
|
|
|
|
|
<span className={`px-1`}>-</span>
|
|
|
|
|
|
<FormField name={`expire_before`}>
|
|
|
|
|
|
{({field}) => (
|
|
|
|
|
|
<DatePicker
|
|
|
|
|
|
{...field}
|
|
|
|
|
|
className={`w-36`}
|
|
|
|
|
|
placeholder={`结束时间`}
|
|
|
|
|
|
format={`yyyy-MM-dd`}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</FormField>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
<div className={`flex gap-4`}>
|
|
|
|
|
|
<Button className={`h-9`}>
|
|
|
|
|
|
<Search/>
|
|
|
|
|
|
<span>筛选</span>
|
|
|
|
|
|
</Button>
|
2025-04-12 11:10:51 +08:00
|
|
|
|
<Button theme={`outline`} className={`h-9`} onClick={() => form.reset({
|
2025-04-11 17:34:42 +08:00
|
|
|
|
type: 'all',
|
|
|
|
|
|
resource_no: '',
|
|
|
|
|
|
create_after: undefined,
|
|
|
|
|
|
create_before: undefined,
|
|
|
|
|
|
expire_after: undefined,
|
|
|
|
|
|
expire_before: undefined,
|
|
|
|
|
|
})}>
|
|
|
|
|
|
<Eraser/>
|
|
|
|
|
|
<span>重置</span>
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
2025-04-10 17:49:02 +08:00
|
|
|
|
</Form>
|
|
|
|
|
|
</section>
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
2025-04-10 17:49:02 +08:00
|
|
|
|
{/* 数据表 */}
|
|
|
|
|
|
<DataTable
|
|
|
|
|
|
data={data.list}
|
|
|
|
|
|
status={status}
|
|
|
|
|
|
pagination={{
|
|
|
|
|
|
total: data.total,
|
|
|
|
|
|
page: data.page,
|
|
|
|
|
|
size: data.size,
|
|
|
|
|
|
onPageChange: async (page: number) => {
|
|
|
|
|
|
await refresh(page, data.size)
|
|
|
|
|
|
},
|
|
|
|
|
|
onSizeChange: async (size: number) => {
|
|
|
|
|
|
await refresh(data.page, size)
|
|
|
|
|
|
},
|
|
|
|
|
|
}}
|
|
|
|
|
|
columns={[
|
|
|
|
|
|
{
|
2025-04-11 17:34:42 +08:00
|
|
|
|
accessorKey: 'resource_no', header: `套餐编号`,
|
2025-04-10 17:49:02 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
accessorKey: 'type', header: `类型`, cell: ({row}) => (
|
|
|
|
|
|
<div className={`flex gap-2 items-center`}>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
{row.original.pss.type === 1 && (
|
2025-04-10 17:49:02 +08:00
|
|
|
|
<div className={`flex gap-2 items-center bg-green-50 w-fit px-2 py-1 rounded-md`}>
|
|
|
|
|
|
<Timer size={20}/>
|
|
|
|
|
|
<span>包时</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2025-04-11 17:34:42 +08:00
|
|
|
|
{row.original.pss.type === 2 && (
|
2025-04-10 17:49:02 +08:00
|
|
|
|
<div className={`flex gap-2 items-center bg-blue-50 w-fit px-2 py-1 rounded-md`}>
|
|
|
|
|
|
<Box size={20}/>
|
|
|
|
|
|
<span>包量</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
accessorKey: 'live', header: `IP 时效`, cell: ({row}) => (
|
|
|
|
|
|
<span>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
{row.original.pss.live / 60} 分钟
|
2025-04-10 17:49:02 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
accessorKey: 'expire', header: `使用情况`, cell: ({row}) => (
|
|
|
|
|
|
<div className={`flex gap-1`}>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
{row.original.pss.type === 1 ? (
|
2025-04-10 17:49:02 +08:00
|
|
|
|
<div className={`flex gap-1`}>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
{isAfter(row.original.pss.expire, new Date())
|
2025-04-10 17:49:02 +08:00
|
|
|
|
? <span className={`text-green-500`}>正常</span>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
: <span className={`text-red-500`}>过期</span>}
|
2025-04-10 17:49:02 +08:00
|
|
|
|
<span>|</span>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
<span>今日限额:{row.original.pss.daily_used} / {row.original.pss.daily_limit}</span>
|
2025-04-10 17:49:02 +08:00
|
|
|
|
<span>|</span>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
<span>{intlFormatDistance(row.original.pss.expire, new Date())} 到期</span>
|
2025-04-10 17:49:02 +08:00
|
|
|
|
</div>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
) : row.original.pss.type === 2 ? (
|
2025-04-10 17:49:02 +08:00
|
|
|
|
<div className={`flex gap-1`}>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
{row.original.pss.used < row.original.pss.quota
|
2025-04-10 17:49:02 +08:00
|
|
|
|
? <span className={`text-green-500`}>正常</span>
|
|
|
|
|
|
: <span className={`text-red-500`}>已用完</span>}
|
|
|
|
|
|
<span>|</span>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
<span>用量统计:{row.original.pss.used} / {row.original.pss.quota}</span>
|
2025-04-10 17:49:02 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<span>-</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
accessorKey: 'daily_last', header: '最近使用时间', cell: ({row}) => {
|
|
|
|
|
|
return (
|
2025-04-11 17:34:42 +08:00
|
|
|
|
isEqual(row.original.pss.daily_last, parse('0001-01-01 00:05:43', 'yyyy-MM-dd HH:mm:ss', new Date()))
|
2025-04-10 17:49:02 +08:00
|
|
|
|
? '-'
|
2025-04-11 17:34:42 +08:00
|
|
|
|
: format(row.original.pss.daily_last, 'yyyy-MM-dd HH:mm')
|
2025-04-10 17:49:02 +08:00
|
|
|
|
)
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
accessorKey: 'created_at', header: '开通时间', cell: ({row}) => (
|
|
|
|
|
|
format(row.getValue('created_at'), 'yyyy-MM-dd HH:mm')
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
accessorKey: 'action', header: `操作`, cell: (item) => (
|
|
|
|
|
|
<div className={`flex gap-2`}>
|
|
|
|
|
|
-
|
|
|
|
|
|
</div>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
]}
|
2025-04-11 17:34:42 +08:00
|
|
|
|
classNames={{
|
|
|
|
|
|
dataRow: `h-14`,
|
|
|
|
|
|
}}
|
2025-04-10 17:49:02 +08:00
|
|
|
|
/>
|
2025-04-11 17:34:42 +08:00
|
|
|
|
</Page>
|
2025-04-08 11:21:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
}
|