index.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import React, { useEffect, useState } from 'react';
  2. import { PageContainer } from '@ant-design/pro-components';
  3. import { Button, Card, Form, Input, message, Modal, Select, Space, Table } from 'antd';
  4. import type { ColumnsType } from 'antd/es/table';
  5. import Edit from '@/pages/ParameterConfiguration/edit';
  6. import { ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  7. import {
  8. delParameters,
  9. disableParameters,
  10. enableParameters,
  11. queryParameters,
  12. } from '@/services/ParameterConfiguration';
  13. interface DataType {
  14. code: string;
  15. name: string;
  16. value: string;
  17. status: number;
  18. memo: string;
  19. record_id: string;
  20. }
  21. const ParameterConfiguration: React.FC = () => {
  22. const [form] = Form.useForm();
  23. const [loading, setLoading] = useState(false);
  24. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  25. const [dataList, setDataList] = useState([]);
  26. const [visible, setVisible] = useState(false);
  27. const [params, setParams] = useState(null);
  28. const [searchData, setSearchData] = useState<object | null>({});
  29. const getList = () => {
  30. const data = {
  31. q: 'page',
  32. current: pagination.current,
  33. pageSize: pagination.pageSize,
  34. ...searchData,
  35. };
  36. queryParameters(data).then((res) => {
  37. if (res.code === 0) {
  38. setDataList(res.data.list);
  39. setPagination(res.data.pagination);
  40. setLoading(false);
  41. }
  42. });
  43. };
  44. useEffect(() => {
  45. setLoading(true);
  46. getList();
  47. }, []);
  48. // 查询
  49. const onFinish = () => {
  50. form.validateFields().then((data) => {
  51. setLoading(true);
  52. setSearchData(data);
  53. });
  54. };
  55. useEffect(() => {
  56. getList();
  57. }, [searchData]);
  58. // 重置
  59. const onReset = () => {
  60. form.resetFields();
  61. setLoading(true);
  62. setSearchData(null);
  63. };
  64. // 切换列表
  65. const tableChange = () => {
  66. setLoading(true);
  67. const data = {
  68. q: 'page',
  69. current: pagination.current,
  70. pageSize: pagination.pageSize,
  71. ...searchData,
  72. };
  73. queryParameters(data).then((res) => {
  74. if (res.code === 0) {
  75. setDataList(res.data.list);
  76. setPagination(res.data.pagination);
  77. setLoading(false);
  78. }
  79. });
  80. };
  81. // 新增
  82. const onAdd = () => {
  83. setVisible(true);
  84. };
  85. // 编辑
  86. const toEdit = (record: any) => {
  87. setVisible(true);
  88. setParams(record);
  89. };
  90. // 删除
  91. const toDel = (record: DataType) => {
  92. Modal.confirm({
  93. title: '删除',
  94. content: `是否确认删除?`,
  95. onOk: () => {
  96. delParameters(record.record_id)
  97. .then((res) => {
  98. if (res.code === 0) {
  99. message.success('删除成功');
  100. } else {
  101. message.error('删除失败');
  102. }
  103. getList();
  104. })
  105. .catch((e) => {
  106. message.error(e?.message);
  107. });
  108. },
  109. });
  110. };
  111. // 弹框回调
  112. const editCallback = () => {
  113. setVisible(false);
  114. getList();
  115. };
  116. // 启用
  117. const toEnable = (record: DataType) => {
  118. Modal.confirm({
  119. content: `是否确认启用参数配置?`,
  120. onOk: () => {
  121. enableParameters(record.record_id)
  122. .then((res) => {
  123. if (res.code === 0) {
  124. message.success('启用成功');
  125. getList();
  126. } else {
  127. message.error('启用失败');
  128. }
  129. })
  130. .catch((e) => {
  131. message.error(e?.message);
  132. });
  133. },
  134. });
  135. };
  136. // 禁用
  137. const toDisable = (record: DataType) => {
  138. Modal.confirm({
  139. content: `是否确认禁用参数配置?`,
  140. onOk: () => {
  141. disableParameters(record.record_id)
  142. .then((res) => {
  143. if (res.code === 0) {
  144. message.success('禁用成功');
  145. getList();
  146. } else {
  147. message.error('禁用失败');
  148. }
  149. })
  150. .catch((e) => {
  151. message.error(e?.message);
  152. });
  153. },
  154. });
  155. };
  156. const columns: ColumnsType<DataType> = [
  157. {
  158. title: '序号',
  159. align: 'center',
  160. key: 'index',
  161. render: (_: any, row: any, index: number) => index + 1,
  162. },
  163. {
  164. title: '编号',
  165. dataIndex: 'code',
  166. key: 'code',
  167. },
  168. {
  169. title: '名称',
  170. dataIndex: 'name',
  171. key: 'version',
  172. },
  173. {
  174. title: '参数值',
  175. dataIndex: 'value',
  176. key: 'value',
  177. },
  178. {
  179. title: '状态',
  180. dataIndex: 'status',
  181. key: 'status',
  182. render: (v) => v && <span>{{ 1: '启用', 2: '禁用' }[v]}</span>,
  183. },
  184. {
  185. title: '备注',
  186. dataIndex: 'memo',
  187. key: 'memo',
  188. },
  189. {
  190. title: '操作',
  191. key: 'action',
  192. render: (_, record) => (
  193. <Space size="middle">
  194. <a
  195. onClick={() => {
  196. toEdit(record);
  197. }}
  198. >
  199. 编辑
  200. </a>
  201. <a
  202. style={{ color: 'red' }}
  203. onClick={() => {
  204. toDel(record);
  205. }}
  206. >
  207. 删除
  208. </a>
  209. {record.status === 1 && (
  210. <a
  211. style={{ color: 'red' }}
  212. onClick={() => {
  213. toDisable(record);
  214. }}
  215. >
  216. 禁用
  217. </a>
  218. )}
  219. {record.status === 2 && (
  220. <a
  221. onClick={() => {
  222. toEnable(record);
  223. }}
  224. >
  225. 启用
  226. </a>
  227. )}
  228. </Space>
  229. ),
  230. },
  231. ];
  232. const paginationProps = {
  233. showSizeChanger: true,
  234. showQuickJumper: true,
  235. showTotal: (total: number) => {
  236. return <span> 共 {total}条 </span>;
  237. },
  238. ...pagination,
  239. };
  240. return (
  241. <PageContainer>
  242. <div>
  243. <Card>
  244. <Form form={form} layout="inline" onFinish={onFinish}>
  245. <Form.Item name="like_name" label="名称">
  246. <Input placeholder="请输入名称" />
  247. </Form.Item>
  248. <Form.Item name="status" label="状态">
  249. <Select placeholder="请选择状态" style={{ width: '175px' }}>
  250. <Select.Option value={1} key={1}>
  251. 启用
  252. </Select.Option>
  253. <Select.Option value={2} key={2}>
  254. 禁用
  255. </Select.Option>
  256. </Select>
  257. </Form.Item>
  258. <Form.Item style={{ marginBottom: '10px' }}>
  259. <Space>
  260. <Button type="primary" htmlType="submit">
  261. <SearchOutlined />
  262. 查询
  263. </Button>
  264. <Button htmlType="button" onClick={onReset}>
  265. <ReloadOutlined />
  266. 重置
  267. </Button>
  268. </Space>
  269. </Form.Item>
  270. </Form>
  271. <Button type="primary" onClick={onAdd} style={{ margin: '20px 0' }}>
  272. 新增
  273. </Button>
  274. <Table
  275. columns={columns}
  276. dataSource={dataList}
  277. rowKey={(record) => record.record_id}
  278. pagination={paginationProps}
  279. loading={loading}
  280. onChange={tableChange}
  281. />
  282. {visible && <Edit editCallback={editCallback} params={params} visible={visible} />}
  283. </Card>
  284. </div>
  285. </PageContainer>
  286. );
  287. };
  288. export default ParameterConfiguration;