pages_know_info.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. console.log('handleSearch', values)
  133. queryClient.removeQueries({ queryKey: ['knowInfos'] });
  134. setSearchParams({
  135. title: values.title || '',
  136. category: values.category || '',
  137. page: 1,
  138. limit: searchParams.limit,
  139. });
  140. } catch (error) {
  141. message.error('搜索失败');
  142. }
  143. };
  144. // 处理分页
  145. const handlePageChange = (page: number, pageSize?: number) => {
  146. setSearchParams(prev => ({
  147. ...prev,
  148. page,
  149. limit: pageSize || prev.limit,
  150. }));
  151. };
  152. // 处理添加
  153. const handleAdd = () => {
  154. setFormMode('create');
  155. setEditingId(null);
  156. form.resetFields();
  157. setModalVisible(true);
  158. };
  159. // 审核状态映射
  160. const auditStatusOptions = getEnumOptions(AuditStatus, AuditStatusNameMap);
  161. // 表格列定义
  162. const columns = [
  163. {
  164. title: 'ID',
  165. dataIndex: 'id',
  166. key: 'id',
  167. width: 80,
  168. },
  169. {
  170. title: '标题',
  171. dataIndex: 'title',
  172. key: 'title',
  173. },
  174. {
  175. title: '分类',
  176. dataIndex: 'category',
  177. key: 'category',
  178. },
  179. {
  180. title: '标签',
  181. dataIndex: 'tags',
  182. key: 'tags',
  183. render: (tags: string) => tags ? tags.split(',').map(tag => (
  184. <Tag key={tag}>{tag}</Tag>
  185. )) : null,
  186. },
  187. {
  188. title: '作者',
  189. dataIndex: 'author',
  190. key: 'author',
  191. },
  192. {
  193. title: '审核状态',
  194. dataIndex: 'audit_status',
  195. key: 'audit_status',
  196. render: (status: AuditStatus) => {
  197. let color = '';
  198. let text = '';
  199. switch(status) {
  200. case AuditStatus.PENDING:
  201. color = 'orange';
  202. text = '待审核';
  203. break;
  204. case AuditStatus.APPROVED:
  205. color = 'green';
  206. text = '已通过';
  207. break;
  208. case AuditStatus.REJECTED:
  209. color = 'red';
  210. text = '已拒绝';
  211. break;
  212. default:
  213. color = 'default';
  214. text = '未知';
  215. }
  216. return <Tag color={color}>{text}</Tag>;
  217. },
  218. },
  219. {
  220. title: '创建时间',
  221. dataIndex: 'created_at',
  222. key: 'created_at',
  223. render: (date: string) => new Date(date).toLocaleString(),
  224. },
  225. {
  226. title: '操作',
  227. key: 'action',
  228. render: (_: any, record: KnowInfo) => (
  229. <Space size="middle">
  230. <Button type="link" onClick={() => handleEdit(record.id)}>编辑</Button>
  231. <Popconfirm
  232. title="确定要删除这篇文章吗?"
  233. onConfirm={() => handleDelete(record.id)}
  234. okText="确定"
  235. cancelText="取消"
  236. >
  237. <Button type="link" danger>删除</Button>
  238. </Popconfirm>
  239. </Space>
  240. ),
  241. },
  242. ];
  243. return (
  244. <div>
  245. <Card title="知识库管理" className="mb-4">
  246. <Form
  247. layout="inline"
  248. onFinish={handleSearch}
  249. style={{ marginBottom: '16px' }}
  250. >
  251. <Form.Item name="title" label="标题">
  252. <Input placeholder="请输入文章标题" />
  253. </Form.Item>
  254. <Form.Item name="category" label="分类">
  255. <Input placeholder="请输入文章分类" />
  256. </Form.Item>
  257. <Form.Item>
  258. <Space>
  259. <Button type="primary" htmlType="submit">
  260. 搜索
  261. </Button>
  262. <Button htmlType="reset" onClick={() => {
  263. setSearchParams({
  264. title: '',
  265. category: '',
  266. page: 1,
  267. limit: 10,
  268. });
  269. }}>
  270. 重置
  271. </Button>
  272. <Button type="primary" onClick={handleAdd}>
  273. 添加文章
  274. </Button>
  275. </Space>
  276. </Form.Item>
  277. </Form>
  278. <Table
  279. columns={columns}
  280. dataSource={articles}
  281. rowKey="id"
  282. loading={{
  283. spinning: isListLoading,
  284. tip: '正在加载数据...',
  285. }}
  286. pagination={{
  287. current: pagination.current,
  288. pageSize: pagination.pageSize,
  289. total: pagination.total,
  290. onChange: handlePageChange,
  291. showSizeChanger: true,
  292. showTotal: (total) => `共 ${total} 条`,
  293. }}
  294. />
  295. </Card>
  296. <Modal
  297. title={formMode === 'create' ? '添加知识库文章' : '编辑知识库文章'}
  298. open={modalVisible}
  299. onOk={() => form.submit()}
  300. onCancel={() => setModalVisible(false)}
  301. width={800}
  302. okText="确定"
  303. cancelText="取消"
  304. >
  305. <Form
  306. form={form}
  307. layout="vertical"
  308. onFinish={handleSubmit}
  309. initialValues={{
  310. audit_status: AuditStatus.PENDING,
  311. }}
  312. >
  313. <Row gutter={16}>
  314. <Col span={12}>
  315. <Form.Item
  316. name="title"
  317. label="文章标题"
  318. rules={[{ required: true, message: '请输入文章标题' }]}
  319. >
  320. <Input placeholder="请输入文章标题" />
  321. </Form.Item>
  322. </Col>
  323. <Col span={12}>
  324. <Form.Item
  325. name="category"
  326. label="文章分类"
  327. >
  328. <Input placeholder="请输入文章分类" />
  329. </Form.Item>
  330. </Col>
  331. </Row>
  332. <Form.Item
  333. name="tags"
  334. label="文章标签"
  335. help="多个标签请用英文逗号分隔,如: 服务器,网络,故障"
  336. >
  337. <Input placeholder="请输入文章标签,多个标签请用英文逗号分隔" />
  338. </Form.Item>
  339. <Form.Item
  340. name="content"
  341. label="文章内容"
  342. rules={[{ required: true, message: '请输入文章内容' }]}
  343. >
  344. <Input.TextArea rows={15} placeholder="请输入文章内容,支持Markdown格式" />
  345. </Form.Item>
  346. <Row gutter={16}>
  347. <Col span={12}>
  348. <Form.Item
  349. name="author"
  350. label="文章作者"
  351. >
  352. <Input placeholder="请输入文章作者" />
  353. </Form.Item>
  354. </Col>
  355. <Col span={12}>
  356. <Form.Item
  357. name="cover_url"
  358. label="封面图片URL"
  359. >
  360. <Input placeholder="请输入封面图片URL" />
  361. </Form.Item>
  362. </Col>
  363. </Row>
  364. <Form.Item
  365. name="audit_status"
  366. label="审核状态"
  367. >
  368. <Select options={auditStatusOptions} />
  369. </Form.Item>
  370. </Form>
  371. </Modal>
  372. </div>
  373. );
  374. };