index.tsx 5.5 KB

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