index.tsx 5.4 KB

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