pages_know_info.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 [isLoading, setIsLoading] = useState(false);
  54. const [searchParams, setSearchParams] = useState({
  55. title: '',
  56. category: '',
  57. page: 1,
  58. limit: 10,
  59. });
  60. // 使用React Query获取知识库文章列表
  61. const { data: articlesData, isLoading: isListLoading, refetch } = useQuery<KnowInfoListResponse>({
  62. queryKey: ['knowInfos', searchParams],
  63. queryFn: () => KnowInfoAPI.getKnowInfos({
  64. page: searchParams.page,
  65. pageSize: searchParams.limit,
  66. title: searchParams.title,
  67. category: searchParams.category
  68. })
  69. });
  70. const articles = articlesData?.data || [];
  71. const pagination = articlesData?.pagination || { current: 1, pageSize: 10, total: 0 };
  72. // 获取单个知识库文章
  73. const fetchArticle = async (id: number) => {
  74. try {
  75. const response = await KnowInfoAPI.getKnowInfo(id);
  76. return response.data;
  77. } catch (error) {
  78. message.error('获取知识库文章详情失败');
  79. return null;
  80. }
  81. };
  82. // 处理表单提交
  83. const handleSubmit = async (values: Partial<KnowInfo>) => {
  84. setIsLoading(true);
  85. try {
  86. const response = formMode === 'create'
  87. ? await KnowInfoAPI.createKnowInfo(values)
  88. : await KnowInfoAPI.updateKnowInfo(editingId!, values);
  89. message.success(formMode === 'create' ? '创建知识库文章成功' : '更新知识库文章成功');
  90. setModalVisible(false);
  91. form.resetFields();
  92. refetch();
  93. } catch (error) {
  94. message.error((error as Error).message);
  95. } finally {
  96. setIsLoading(false);
  97. }
  98. };
  99. // 处理编辑
  100. const handleEdit = async (id: number) => {
  101. const article = await fetchArticle(id);
  102. if (article) {
  103. setFormMode('edit');
  104. setEditingId(id);
  105. form.setFieldsValue(article);
  106. setModalVisible(true);
  107. }
  108. };
  109. // 处理删除
  110. const handleDelete = async (id: number) => {
  111. try {
  112. await KnowInfoAPI.deleteKnowInfo(id);
  113. message.success('删除知识库文章成功');
  114. refetch();
  115. } catch (error) {
  116. message.error((error as Error).message);
  117. }
  118. };
  119. // 处理搜索
  120. const handleSearch = (values: any) => {
  121. queryClient.removeQueries({ queryKey: ['knowInfos'] });
  122. setSearchParams(prev => ({
  123. ...prev,
  124. title: values.title || '',
  125. category: values.category || '',
  126. page: 1,
  127. }));
  128. };
  129. // 处理分页
  130. const handlePageChange = (page: number, pageSize?: number) => {
  131. setSearchParams(prev => ({
  132. ...prev,
  133. page,
  134. limit: pageSize || prev.limit,
  135. }));
  136. };
  137. // 处理添加
  138. const handleAdd = () => {
  139. setFormMode('create');
  140. setEditingId(null);
  141. form.resetFields();
  142. setModalVisible(true);
  143. };
  144. // 审核状态映射
  145. const auditStatusOptions = getEnumOptions(AuditStatus, AuditStatusNameMap);
  146. // 表格列定义
  147. const columns = [
  148. {
  149. title: 'ID',
  150. dataIndex: 'id',
  151. key: 'id',
  152. width: 80,
  153. },
  154. {
  155. title: '标题',
  156. dataIndex: 'title',
  157. key: 'title',
  158. },
  159. {
  160. title: '分类',
  161. dataIndex: 'category',
  162. key: 'category',
  163. },
  164. {
  165. title: '标签',
  166. dataIndex: 'tags',
  167. key: 'tags',
  168. render: (tags: string) => tags ? tags.split(',').map(tag => (
  169. <Tag key={tag}>{tag}</Tag>
  170. )) : null,
  171. },
  172. {
  173. title: '作者',
  174. dataIndex: 'author',
  175. key: 'author',
  176. },
  177. {
  178. title: '审核状态',
  179. dataIndex: 'audit_status',
  180. key: 'audit_status',
  181. render: (status: AuditStatus) => {
  182. let color = '';
  183. let text = '';
  184. switch(status) {
  185. case AuditStatus.PENDING:
  186. color = 'orange';
  187. text = '待审核';
  188. break;
  189. case AuditStatus.APPROVED:
  190. color = 'green';
  191. text = '已通过';
  192. break;
  193. case AuditStatus.REJECTED:
  194. color = 'red';
  195. text = '已拒绝';
  196. break;
  197. default:
  198. color = 'default';
  199. text = '未知';
  200. }
  201. return <Tag color={color}>{text}</Tag>;
  202. },
  203. },
  204. {
  205. title: '创建时间',
  206. dataIndex: 'created_at',
  207. key: 'created_at',
  208. render: (date: string) => new Date(date).toLocaleString(),
  209. },
  210. {
  211. title: '操作',
  212. key: 'action',
  213. render: (_: any, record: KnowInfo) => (
  214. <Space size="middle">
  215. <Button type="link" onClick={() => handleEdit(record.id)}>编辑</Button>
  216. <Popconfirm
  217. title="确定要删除这篇文章吗?"
  218. onConfirm={() => handleDelete(record.id)}
  219. okText="确定"
  220. cancelText="取消"
  221. >
  222. <Button type="link" danger>删除</Button>
  223. </Popconfirm>
  224. </Space>
  225. ),
  226. },
  227. ];
  228. return (
  229. <div>
  230. <Card title="知识库管理" className="mb-4">
  231. <Form
  232. layout="inline"
  233. onFinish={handleSearch}
  234. style={{ marginBottom: '16px' }}
  235. >
  236. <Form.Item name="title" label="标题">
  237. <Input placeholder="请输入文章标题" />
  238. </Form.Item>
  239. <Form.Item name="category" label="分类">
  240. <Input placeholder="请输入文章分类" />
  241. </Form.Item>
  242. <Form.Item>
  243. <Space>
  244. <Button type="primary" htmlType="submit">
  245. 搜索
  246. </Button>
  247. <Button onClick={() => {
  248. setSearchParams({
  249. title: '',
  250. category: '',
  251. page: 1,
  252. limit: 10,
  253. });
  254. }}>
  255. 重置
  256. </Button>
  257. <Button type="primary" onClick={handleAdd}>
  258. 添加文章
  259. </Button>
  260. </Space>
  261. </Form.Item>
  262. </Form>
  263. <Table
  264. columns={columns}
  265. dataSource={articles}
  266. rowKey="id"
  267. loading={isListLoading}
  268. pagination={{
  269. current: pagination.current,
  270. pageSize: pagination.pageSize,
  271. total: pagination.total,
  272. onChange: handlePageChange,
  273. showSizeChanger: true,
  274. }}
  275. />
  276. </Card>
  277. <Modal
  278. title={formMode === 'create' ? '添加知识库文章' : '编辑知识库文章'}
  279. open={modalVisible}
  280. onOk={() => form.submit()}
  281. onCancel={() => setModalVisible(false)}
  282. width={800}
  283. >
  284. <Form
  285. form={form}
  286. layout="vertical"
  287. onFinish={handleSubmit}
  288. initialValues={{
  289. audit_status: AuditStatus.PENDING,
  290. }}
  291. >
  292. <Row gutter={16}>
  293. <Col span={12}>
  294. <Form.Item
  295. name="title"
  296. label="文章标题"
  297. rules={[{ required: true, message: '请输入文章标题' }]}
  298. >
  299. <Input placeholder="请输入文章标题" />
  300. </Form.Item>
  301. </Col>
  302. <Col span={12}>
  303. <Form.Item
  304. name="category"
  305. label="文章分类"
  306. >
  307. <Input placeholder="请输入文章分类" />
  308. </Form.Item>
  309. </Col>
  310. </Row>
  311. <Form.Item
  312. name="tags"
  313. label="文章标签"
  314. help="多个标签请用英文逗号分隔,如: 服务器,网络,故障"
  315. >
  316. <Input placeholder="请输入文章标签,多个标签请用英文逗号分隔" />
  317. </Form.Item>
  318. <Form.Item
  319. name="content"
  320. label="文章内容"
  321. rules={[{ required: true, message: '请输入文章内容' }]}
  322. >
  323. <Input.TextArea rows={15} placeholder="请输入文章内容,支持Markdown格式" />
  324. </Form.Item>
  325. <Row gutter={16}>
  326. <Col span={12}>
  327. <Form.Item
  328. name="author"
  329. label="文章作者"
  330. >
  331. <Input placeholder="请输入文章作者" />
  332. </Form.Item>
  333. </Col>
  334. <Col span={12}>
  335. <Form.Item
  336. name="cover_url"
  337. label="封面图片URL"
  338. >
  339. <Input placeholder="请输入封面图片URL" />
  340. </Form.Item>
  341. </Col>
  342. </Row>
  343. <Form.Item
  344. name="audit_status"
  345. label="审核状态"
  346. >
  347. <Select options={auditStatusOptions} />
  348. </Form.Item>
  349. <Form.Item>
  350. <Space>
  351. <Button type="primary" htmlType="submit" loading={isLoading}>
  352. {formMode === 'create' ? '创建' : '保存'}
  353. </Button>
  354. <Button onClick={() => setModalVisible(false)}>取消</Button>
  355. </Space>
  356. </Form.Item>
  357. </Form>
  358. </Modal>
  359. </div>
  360. );
  361. };