pages_know_info.tsx 10 KB

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