pages_know_info.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import React, { useState } from 'react';
  2. import { useQueryClient } from '@tanstack/react-query';
  3. import {
  4. Layout, Menu, Button, Table, Space,
  5. Form, Input, Select, message, Modal,
  6. Card, Spin, Row, Col, Breadcrumb, Avatar,
  7. Dropdown, ConfigProvider, theme, Typography,
  8. Switch, Badge, Image, Upload, Divider, Descriptions,
  9. Popconfirm, Tag, Statistic, DatePicker, Radio, Progress, Tabs, List, Alert, Collapse, Empty, Drawer
  10. } from 'antd';
  11. import {
  12. UploadOutlined,
  13. FileImageOutlined,
  14. FileExcelOutlined,
  15. FileWordOutlined,
  16. FilePdfOutlined,
  17. FileOutlined,
  18. } from '@ant-design/icons';
  19. import {
  20. useQuery,
  21. } from '@tanstack/react-query';
  22. import dayjs from 'dayjs';
  23. import weekday from 'dayjs/plugin/weekday';
  24. import localeData from 'dayjs/plugin/localeData';
  25. import 'dayjs/locale/zh-cn';
  26. import type {
  27. FileLibrary, FileCategory, KnowInfo
  28. } from '../share/types.ts';
  29. import {
  30. AuditStatus,AuditStatusNameMap,
  31. OssType,
  32. } from '../share/types.ts';
  33. import { getEnumOptions } from './utils.ts';
  34. import {
  35. FileAPI,
  36. UserAPI,
  37. KnowInfoAPI,
  38. type KnowInfoListResponse
  39. } from './api.ts';
  40. // 配置 dayjs 插件
  41. dayjs.extend(weekday);
  42. dayjs.extend(localeData);
  43. // 设置 dayjs 语言
  44. dayjs.locale('zh-cn');
  45. const { Title } = Typography;
  46. // 知识库管理页面组件
  47. export const KnowInfoPage = () => {
  48. const queryClient = useQueryClient();
  49. const [modalVisible, setModalVisible] = useState(false);
  50. const [formMode, setFormMode] = useState<'create' | 'edit'>('create');
  51. const [editingId, setEditingId] = useState<number | null>(null);
  52. const [form] = Form.useForm();
  53. const [searchParams, setSearchParams] = useState({
  54. title: '',
  55. category: '',
  56. page: 1,
  57. limit: 10,
  58. });
  59. // 使用React Query获取知识库文章列表
  60. const { data: articlesData, isLoading: isListLoading, refetch } = useQuery({
  61. queryKey: ['knowInfos', searchParams],
  62. queryFn: () => KnowInfoAPI.getKnowInfos({
  63. page: searchParams.page,
  64. pageSize: searchParams.limit,
  65. title: searchParams.title,
  66. category: searchParams.category
  67. }),
  68. placeholderData: {
  69. data: [],
  70. pagination: {
  71. current: 1,
  72. pageSize: 10,
  73. total: 0,
  74. totalPages: 1
  75. }
  76. }
  77. });
  78. const articles = React.useMemo(() => (articlesData as KnowInfoListResponse)?.data || [], [articlesData]);
  79. const pagination = React.useMemo(() => ({
  80. current: (articlesData as KnowInfoListResponse)?.pagination?.current || 1,
  81. pageSize: (articlesData as KnowInfoListResponse)?.pagination?.pageSize || 10,
  82. total: (articlesData as KnowInfoListResponse)?.pagination?.total || 0,
  83. totalPages: (articlesData as KnowInfoListResponse)?.pagination?.totalPages || 1
  84. }), [articlesData]);
  85. // 获取单个知识库文章
  86. const fetchArticle = async (id: number) => {
  87. try {
  88. const response = await KnowInfoAPI.getKnowInfo(id);
  89. return response.data;
  90. } catch (error) {
  91. message.error('获取知识库文章详情失败');
  92. return null;
  93. }
  94. };
  95. // 处理表单提交
  96. const handleSubmit = async (values: Partial<KnowInfo>) => {
  97. try {
  98. const response = formMode === 'create'
  99. ? await KnowInfoAPI.createKnowInfo(values)
  100. : await KnowInfoAPI.updateKnowInfo(editingId!, values);
  101. message.success(formMode === 'create' ? '创建知识库文章成功' : '更新知识库文章成功');
  102. setModalVisible(false);
  103. form.resetFields();
  104. refetch();
  105. } catch (error) {
  106. message.error((error as Error).message);
  107. }
  108. };
  109. // 处理编辑
  110. const handleEdit = async (id: number) => {
  111. const article = await fetchArticle(id);
  112. if (article) {
  113. setFormMode('edit');
  114. setEditingId(id);
  115. form.setFieldsValue(article);
  116. setModalVisible(true);
  117. }
  118. };
  119. // 处理删除
  120. const handleDelete = async (id: number) => {
  121. try {
  122. await KnowInfoAPI.deleteKnowInfo(id);
  123. message.success('删除知识库文章成功');
  124. refetch();
  125. } catch (error) {
  126. message.error((error as Error).message);
  127. }
  128. };
  129. // 处理搜索
  130. const handleSearch = async (values: any) => {
  131. try {
  132. queryClient.removeQueries({ queryKey: ['knowInfos'] });
  133. setSearchParams({
  134. title: values.title || '',
  135. category: values.category || '',
  136. page: 1,
  137. limit: searchParams.limit,
  138. });
  139. } catch (error) {
  140. message.error('搜索失败');
  141. }
  142. };
  143. // 处理分页
  144. const handlePageChange = (page: number, pageSize?: number) => {
  145. setSearchParams(prev => ({
  146. ...prev,
  147. page,
  148. limit: pageSize || prev.limit,
  149. }));
  150. };
  151. // 处理添加
  152. const handleAdd = () => {
  153. setFormMode('create');
  154. setEditingId(null);
  155. form.resetFields();
  156. setModalVisible(true);
  157. };
  158. // 审核状态映射
  159. const auditStatusOptions = getEnumOptions(AuditStatus, AuditStatusNameMap);
  160. // 表格列定义
  161. const columns = [
  162. {
  163. title: 'ID',
  164. dataIndex: 'id',
  165. key: 'id',
  166. width: 80,
  167. },
  168. {
  169. title: '标题',
  170. dataIndex: 'title',
  171. key: 'title',
  172. },
  173. {
  174. title: '分类',
  175. dataIndex: 'category',
  176. key: 'category',
  177. },
  178. {
  179. title: '标签',
  180. dataIndex: 'tags',
  181. key: 'tags',
  182. render: (tags: string) => tags ? tags.split(',').map(tag => (
  183. <Tag key={tag}>{tag}</Tag>
  184. )) : null,
  185. },
  186. {
  187. title: '作者',
  188. dataIndex: 'author',
  189. key: 'author',
  190. },
  191. {
  192. title: '审核状态',
  193. dataIndex: 'audit_status',
  194. key: 'audit_status',
  195. render: (status: AuditStatus) => {
  196. let color = '';
  197. let text = '';
  198. switch(status) {
  199. case AuditStatus.PENDING:
  200. color = 'orange';
  201. text = '待审核';
  202. break;
  203. case AuditStatus.APPROVED:
  204. color = 'green';
  205. text = '已通过';
  206. break;
  207. case AuditStatus.REJECTED:
  208. color = 'red';
  209. text = '已拒绝';
  210. break;
  211. default:
  212. color = 'default';
  213. text = '未知';
  214. }
  215. return <Tag color={color}>{text}</Tag>;
  216. },
  217. },
  218. {
  219. title: '创建时间',
  220. dataIndex: 'created_at',
  221. key: 'created_at',
  222. render: (date: string) => new Date(date).toLocaleString(),
  223. },
  224. {
  225. title: '操作',
  226. key: 'action',
  227. render: (_: any, record: KnowInfo) => (
  228. <Space size="middle">
  229. <Button type="link" onClick={() => handleEdit(record.id)}>编辑</Button>
  230. <Popconfirm
  231. title="确定要删除这篇文章吗?"
  232. onConfirm={() => handleDelete(record.id)}
  233. okText="确定"
  234. cancelText="取消"
  235. >
  236. <Button type="link" danger>删除</Button>
  237. </Popconfirm>
  238. </Space>
  239. ),
  240. },
  241. ];
  242. return (
  243. <div>
  244. <Card title="知识库管理" className="mb-4">
  245. <Form
  246. layout="inline"
  247. onFinish={handleSearch}
  248. style={{ marginBottom: '16px' }}
  249. >
  250. <Form.Item name="title" label="标题">
  251. <Input placeholder="请输入文章标题" />
  252. </Form.Item>
  253. <Form.Item name="category" label="分类">
  254. <Input placeholder="请输入文章分类" />
  255. </Form.Item>
  256. <Form.Item>
  257. <Space>
  258. <Button type="primary" htmlType="submit">
  259. 搜索
  260. </Button>
  261. <Button htmlType="reset" onClick={() => {
  262. setSearchParams({
  263. title: '',
  264. category: '',
  265. page: 1,
  266. limit: 10,
  267. });
  268. }}>
  269. 重置
  270. </Button>
  271. <Button type="primary" onClick={handleAdd}>
  272. 添加文章
  273. </Button>
  274. </Space>
  275. </Form.Item>
  276. </Form>
  277. <Table
  278. columns={columns}
  279. dataSource={articles}
  280. rowKey="id"
  281. loading={{
  282. spinning: isListLoading,
  283. tip: '正在加载数据...',
  284. }}
  285. pagination={{
  286. current: pagination.current,
  287. pageSize: pagination.pageSize,
  288. total: pagination.total,
  289. onChange: handlePageChange,
  290. showSizeChanger: true,
  291. showTotal: (total) => `共 ${total} 条`,
  292. }}
  293. />
  294. </Card>
  295. <Modal
  296. title={formMode === 'create' ? '添加知识库文章' : '编辑知识库文章'}
  297. open={modalVisible}
  298. onOk={() => form.submit()}
  299. onCancel={() => setModalVisible(false)}
  300. width={800}
  301. okText="确定"
  302. cancelText="取消"
  303. >
  304. <Form
  305. form={form}
  306. layout="vertical"
  307. onFinish={handleSubmit}
  308. initialValues={{
  309. audit_status: AuditStatus.PENDING,
  310. }}
  311. >
  312. <Row gutter={16}>
  313. <Col span={12}>
  314. <Form.Item
  315. name="title"
  316. label="文章标题"
  317. rules={[{ required: true, message: '请输入文章标题' }]}
  318. >
  319. <Input placeholder="请输入文章标题" />
  320. </Form.Item>
  321. </Col>
  322. <Col span={12}>
  323. <Form.Item
  324. name="category"
  325. label="文章分类"
  326. >
  327. <Input placeholder="请输入文章分类" />
  328. </Form.Item>
  329. </Col>
  330. </Row>
  331. <Form.Item
  332. name="tags"
  333. label="文章标签"
  334. help="多个标签请用英文逗号分隔,如: 服务器,网络,故障"
  335. >
  336. <Input placeholder="请输入文章标签,多个标签请用英文逗号分隔" />
  337. </Form.Item>
  338. <Form.Item
  339. name="content"
  340. label="文章内容"
  341. rules={[{ required: true, message: '请输入文章内容' }]}
  342. >
  343. <Input.TextArea rows={15} placeholder="请输入文章内容,支持Markdown格式" />
  344. </Form.Item>
  345. <Row gutter={16}>
  346. <Col span={12}>
  347. <Form.Item
  348. name="author"
  349. label="文章作者"
  350. >
  351. <Input placeholder="请输入文章作者" />
  352. </Form.Item>
  353. </Col>
  354. <Col span={12}>
  355. <Form.Item
  356. name="cover_url"
  357. label="封面图片URL"
  358. >
  359. <Input placeholder="请输入封面图片URL" />
  360. </Form.Item>
  361. </Col>
  362. </Row>
  363. <Form.Item
  364. name="audit_status"
  365. label="审核状态"
  366. >
  367. <Select options={auditStatusOptions} />
  368. </Form.Item>
  369. </Form>
  370. </Modal>
  371. </div>
  372. );
  373. };