index.tsx 5.4 KB

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