index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Card, Form, Input, message, Modal, Space, Table } from 'antd';
  3. import type { ColumnsType } from 'antd/es/table';
  4. import moment from 'moment/moment';
  5. import { PlusCircleOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  6. import Edit from '@/pages/cms/ArticleManagement/edit';
  7. import { PageContainer } from '@ant-design/pro-components';
  8. import { queryArticle, delArticle } from '@/services/cms/ArticleManagement';
  9. interface DataType {
  10. title: string;
  11. created_at: string;
  12. creator: string;
  13. record_id: string;
  14. }
  15. /**
  16. * 文章管理
  17. * @constructor
  18. */
  19. const ArticleManagement: React.FC = () => {
  20. const [form] = Form.useForm();
  21. const [loading, setLoading] = useState(false);
  22. const [searchData, setSearchData] = useState<object | null>({});
  23. const [visible, setVisible] = useState(false);
  24. const [editData, setEditData] = useState(null);
  25. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  26. const [dataList, setDataList]: any = useState([]);
  27. // 获取列表数据
  28. const getList = () => {
  29. const params = {
  30. q: 'page',
  31. current: pagination.current,
  32. pageSize: pagination.pageSize,
  33. ...searchData,
  34. };
  35. queryArticle(params).then((res) => {
  36. if (res && res.code === 0) {
  37. setDataList(res.data.list);
  38. setPagination(res.data.pagination);
  39. setLoading(false);
  40. }
  41. });
  42. };
  43. useEffect(() => {
  44. setLoading(true);
  45. getList();
  46. }, []);
  47. // 搜索
  48. const onFinish = () => {
  49. form.validateFields().then((data) => {
  50. setLoading(true);
  51. setSearchData(data);
  52. });
  53. };
  54. useEffect(() => {
  55. getList();
  56. }, [searchData]);
  57. // 重置
  58. const onReset = () => {
  59. form.resetFields();
  60. setLoading(true);
  61. setSearchData(null);
  62. };
  63. // 新增弹框
  64. const onAdd = () => {
  65. setEditData(null);
  66. setVisible(true);
  67. };
  68. // 打开编辑弹框
  69. const toEdit = (data: any) => {
  70. setEditData(data);
  71. setVisible(true);
  72. };
  73. // 分页切换
  74. const tableChange = (page: any) => {
  75. setLoading(true);
  76. const params = {
  77. q: 'page',
  78. current: page.current,
  79. pageSize: page.pageSize,
  80. ...searchData,
  81. };
  82. queryArticle(params).then((res) => {
  83. if (res.code === 0) {
  84. setDataList(res.data.list);
  85. setPagination(res.data.pagination);
  86. setLoading(false);
  87. }
  88. });
  89. };
  90. // 删除
  91. const toDel = (record: any) => {
  92. console.log(record);
  93. Modal.confirm({
  94. title: '删除',
  95. content: `确认删除项目:[${record.title}]`,
  96. onOk: () => {
  97. delArticle(record?.record_id)
  98. .then((res) => {
  99. if (res.code === 0) {
  100. message.success('删除成功');
  101. } else {
  102. message.error('删除失败');
  103. }
  104. getList();
  105. })
  106. .catch((e) => {
  107. message.error(e?.message);
  108. });
  109. },
  110. });
  111. };
  112. // 编辑弹框回调
  113. const handleEdit = () => {
  114. setVisible(false);
  115. getList();
  116. };
  117. const columns: ColumnsType<DataType> = [
  118. {
  119. title: '序号',
  120. align: 'center',
  121. key: 'index',
  122. render: (_: any, row: any, index: number) => index + 1,
  123. },
  124. {
  125. title: '文章标题',
  126. dataIndex: 'title',
  127. key: 'title',
  128. },
  129. {
  130. title: '创建者',
  131. dataIndex: 'creator',
  132. key: 'creator',
  133. },
  134. {
  135. title: '创建时间',
  136. dataIndex: 'created_at',
  137. key: 'created_at',
  138. render: (v) => v && <span>{moment(v).format('YYYY-MM-DD HH:mm')}</span>,
  139. },
  140. {
  141. title: '操作',
  142. key: 'action',
  143. render: (_, record) => (
  144. <Space size="middle">
  145. <a
  146. onClick={() => {
  147. toEdit(record);
  148. }}
  149. >
  150. 编辑
  151. </a>
  152. <a
  153. style={{ color: 'red' }}
  154. onClick={() => {
  155. toDel(record);
  156. }}
  157. >
  158. 删除
  159. </a>
  160. </Space>
  161. ),
  162. },
  163. ];
  164. const paginationProps = {
  165. showSizeChanger: true,
  166. showQuickJumper: true,
  167. showTotal: (total: number) => {
  168. return <span> 共 {total}条 </span>;
  169. },
  170. ...pagination,
  171. };
  172. return (
  173. <PageContainer>
  174. <div>
  175. <Card>
  176. <Form form={form} layout="inline" onFinish={onFinish}>
  177. <Form.Item name="title" label="文章标题">
  178. <Input placeholder="请输入文章标题" />
  179. </Form.Item>
  180. <Form.Item style={{ marginBottom: '10px' }}>
  181. <Space>
  182. <Button type="primary" htmlType="submit">
  183. <SearchOutlined />
  184. 查询
  185. </Button>
  186. <Button htmlType="button" onClick={onReset}>
  187. <ReloadOutlined />
  188. 重置
  189. </Button>
  190. </Space>
  191. </Form.Item>
  192. </Form>
  193. <Button htmlType="button" type="primary" style={{ margin: '20px 0' }} onClick={onAdd}>
  194. <PlusCircleOutlined />
  195. 新增文章
  196. </Button>
  197. <Table
  198. columns={columns}
  199. dataSource={dataList}
  200. rowKey={(record) => record.record_id}
  201. pagination={paginationProps}
  202. loading={loading}
  203. onChange={tableChange}
  204. />
  205. {visible && <Edit visible={visible} editCallback={handleEdit} params={editData} />}
  206. </Card>
  207. </div>
  208. </PageContainer>
  209. );
  210. };
  211. export default ArticleManagement;