2025-02-10 19:20:12 +08:00
|
|
|
import { Link } from "@/app/_lib/data/link"
|
|
|
|
import { LinkType } from "@/app/_lib/data/linkType"
|
2025-01-22 17:37:56 +08:00
|
|
|
import { mRequest } from "@/app/_lib/request"
|
|
|
|
import ImageUpload from "@/app/_ui/ImageUpload"
|
2025-01-23 16:46:23 +08:00
|
|
|
import { useAntdTable, useRequest } from "ahooks"
|
2025-01-22 17:37:56 +08:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Card,
|
2025-01-23 16:46:23 +08:00
|
|
|
DatePicker,
|
2025-01-22 17:37:56 +08:00
|
|
|
Form,
|
|
|
|
Image,
|
|
|
|
Input,
|
|
|
|
InputNumber,
|
|
|
|
message,
|
|
|
|
Modal,
|
|
|
|
Popconfirm,
|
2025-01-23 11:31:05 +08:00
|
|
|
Radio,
|
|
|
|
Select,
|
2025-01-22 17:37:56 +08:00
|
|
|
Space,
|
|
|
|
Table,
|
|
|
|
} from "antd"
|
2025-01-23 16:46:23 +08:00
|
|
|
import dayjs from "dayjs"
|
2025-02-13 15:54:18 +08:00
|
|
|
import { useState } from "react"
|
|
|
|
|
2025-01-22 17:37:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
export default function LinkTable(props: { id: string }) {
|
|
|
|
const [loading, setLoading] = useState(false)
|
2025-01-23 11:31:05 +08:00
|
|
|
const { data: LinkTypeList } = useRequest(async () => mRequest<{
|
|
|
|
list: LinkType[]
|
|
|
|
}>('GET', '/api/linkType'))
|
2025-01-23 16:46:23 +08:00
|
|
|
const { tableProps, refresh } = useAntdTable(
|
|
|
|
async ({ current, pageSize }) => {
|
|
|
|
return mRequest<{
|
|
|
|
total: number;
|
|
|
|
list: Link[]
|
|
|
|
}>(
|
|
|
|
"GET",
|
|
|
|
`/api/link?page=${current}&pageSize=${pageSize}&typeId=${props.id}`
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
// const refresh = useCallback(async () => {
|
|
|
|
// setLoading(true)
|
|
|
|
// const res = await mRequest<{ list: Link[] }>(
|
|
|
|
// "GET",
|
|
|
|
// `/api/link?typeId=${props.id}&page=1&pageSize=9999`
|
|
|
|
// )
|
|
|
|
// setList(res.list)
|
|
|
|
// setLoading(false)
|
|
|
|
// }, [props.id])
|
2025-01-23 11:31:05 +08:00
|
|
|
|
2025-01-22 17:37:56 +08:00
|
|
|
const [selected, setSelected] = useState<undefined | null | Link>(
|
|
|
|
undefined
|
|
|
|
)
|
|
|
|
return (
|
|
|
|
<div className="p-4">
|
|
|
|
<Card
|
|
|
|
title="链接列表"
|
|
|
|
extra={
|
|
|
|
<Space>
|
|
|
|
<Button onClick={refresh}>
|
|
|
|
刷新
|
|
|
|
</Button>
|
|
|
|
<Button type="primary" onClick={() => setSelected(null)}>
|
|
|
|
添加链接
|
|
|
|
</Button>
|
|
|
|
</Space>
|
|
|
|
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Table<Link>
|
2025-01-23 16:46:23 +08:00
|
|
|
{...tableProps}
|
2025-01-22 17:37:56 +08:00
|
|
|
loading={loading}
|
|
|
|
rowKey="_id"
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
title: "名称",
|
|
|
|
dataIndex: "name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "图标",
|
|
|
|
dataIndex: "logoLink",
|
|
|
|
render: (_, row) => (
|
|
|
|
<Image width={80} src={row.logoLink}></Image>
|
|
|
|
)
|
|
|
|
},
|
2025-01-23 11:31:05 +08:00
|
|
|
{
|
|
|
|
title: "是否热门",
|
|
|
|
dataIndex: "isHot",
|
|
|
|
render: (_, row) => (
|
|
|
|
row.isHot ? '是' : '否'
|
|
|
|
)
|
|
|
|
},
|
2025-01-22 17:37:56 +08:00
|
|
|
{
|
|
|
|
title: "操作",
|
|
|
|
fixed: "right",
|
|
|
|
width: 200,
|
|
|
|
render: (_, row) => (
|
|
|
|
<Space>
|
|
|
|
<Button type="link" onClick={() => setSelected(row)}>
|
|
|
|
编辑
|
|
|
|
</Button>
|
|
|
|
<Popconfirm
|
|
|
|
title="确认删除?"
|
|
|
|
onConfirm={async () => {
|
2025-01-23 11:31:05 +08:00
|
|
|
await mRequest("DELETE", "/api/link/" + row._id,)
|
2025-01-22 17:37:56 +08:00
|
|
|
refresh()
|
|
|
|
message.success("删除成功")
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Button type="link" danger>
|
|
|
|
删除
|
|
|
|
</Button>
|
|
|
|
</Popconfirm>
|
|
|
|
</Space>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</Card>
|
|
|
|
<Modal
|
|
|
|
open={selected !== undefined}
|
|
|
|
onCancel={() => setSelected(undefined)}
|
|
|
|
destroyOnClose
|
|
|
|
title={(selected ? "编辑" : "新增") + "链接"}
|
|
|
|
footer={false}
|
|
|
|
>
|
|
|
|
<Form
|
|
|
|
labelCol={{ span: 4 }}
|
|
|
|
initialValues={
|
|
|
|
selected
|
2025-01-23 16:46:23 +08:00
|
|
|
? {
|
|
|
|
...selected,
|
|
|
|
addTime: selected.addTime ? dayjs(selected.addTime * 1000) : dayjs(),
|
|
|
|
isHot: selected?.isHot ? 1 : 0
|
|
|
|
}
|
|
|
|
: { title: "", url: "", logoLink: "", description: "", priority: 0, type: props.id, isHot: 0, addTime: dayjs() }
|
2025-01-22 17:37:56 +08:00
|
|
|
}
|
|
|
|
onFinish={async (res) => {
|
|
|
|
if (selected) {
|
|
|
|
await mRequest("PUT", "/api/link", {
|
2025-01-23 11:31:05 +08:00
|
|
|
_id: selected._id,
|
|
|
|
...res,
|
2025-01-23 16:46:23 +08:00
|
|
|
type: props.id,
|
|
|
|
addTime: res.addTime.unix()
|
|
|
|
|
2025-01-22 17:37:56 +08:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
await mRequest("POST", "/api/link", {
|
2025-01-23 16:46:23 +08:00
|
|
|
...res, type: props.id,
|
|
|
|
addTime: res.addTime.unix()
|
2025-01-22 17:37:56 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
refresh()
|
|
|
|
setSelected(undefined)
|
|
|
|
message.success("操作成功")
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Form.Item
|
|
|
|
name="name"
|
|
|
|
label="图标名称"
|
|
|
|
rules={[{ required: true, message: "名称必填" }]}
|
|
|
|
>
|
|
|
|
<Input />
|
|
|
|
</Form.Item>
|
2025-01-23 11:31:05 +08:00
|
|
|
<Form.Item name="link" label="链接" rules={[{ required: true, message: "链接必填" }]}>
|
|
|
|
<Input />
|
|
|
|
</Form.Item>
|
2025-01-22 17:37:56 +08:00
|
|
|
<Form.Item
|
|
|
|
name="logoLink"
|
|
|
|
label="图标名称"
|
|
|
|
rules={[{ required: true, message: "名称必填" }]}
|
|
|
|
>
|
|
|
|
<ImageUpload
|
|
|
|
accept="image/*"
|
|
|
|
width={60}
|
|
|
|
height={60}
|
|
|
|
></ImageUpload>
|
|
|
|
</Form.Item>
|
2025-01-23 11:31:05 +08:00
|
|
|
|
|
|
|
<Form.Item name="description" label="详情"
|
|
|
|
rules={[{ required: true, message: "详情必填" }]}
|
|
|
|
>
|
2025-01-22 17:37:56 +08:00
|
|
|
<Input.TextArea />
|
|
|
|
</Form.Item>
|
2025-01-23 11:31:05 +08:00
|
|
|
<Form.Item name="type" label="分类"
|
|
|
|
rules={[{ required: true, message: "分类必填" }]}
|
|
|
|
|
|
|
|
>
|
|
|
|
<Select options={LinkTypeList?.list.map(item => ({
|
|
|
|
label: item.label,
|
|
|
|
value: item._id
|
|
|
|
}))}></Select>
|
|
|
|
</Form.Item>
|
2025-01-23 16:46:23 +08:00
|
|
|
<Form.Item name="priority" label="优先级"
|
|
|
|
rules={[{ required: true, message: "请输入优先级" }]}
|
|
|
|
|
|
|
|
>
|
|
|
|
<InputNumber min={0} max={9999999}></InputNumber>
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item name="addTime" label="收录时间">
|
|
|
|
<DatePicker
|
|
|
|
allowClear={false}
|
|
|
|
showTime
|
|
|
|
/>
|
2025-01-22 17:37:56 +08:00
|
|
|
</Form.Item>
|
2025-01-23 11:31:05 +08:00
|
|
|
<Form.Item name="isHot" label="是否热门">
|
|
|
|
<Radio.Group
|
|
|
|
options={[
|
|
|
|
{
|
|
|
|
label: '是',
|
|
|
|
value: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: '否',
|
|
|
|
value: 0
|
|
|
|
}
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</Form.Item>
|
2025-01-22 17:37:56 +08:00
|
|
|
<Form.Item className="flex justify-end">
|
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
确认
|
|
|
|
</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
2025-01-23 11:31:05 +08:00
|
|
|
</div >
|
2025-01-22 17:37:56 +08:00
|
|
|
)
|
|
|
|
}
|