index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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: 'created_at',
  127. key: 'created_at',
  128. render: (v) => v && <span>{moment(v).format('YYYY-MM-DD HH:mm')}</span>,
  129. },
  130. {
  131. title: '备注',
  132. dataIndex: 'memo',
  133. key: 'memo',
  134. width: 250,
  135. },
  136. {
  137. title: '操作',
  138. key: 'action',
  139. render: (_, record) => (
  140. <Space size="middle">
  141. <a
  142. onClick={() => {
  143. toEdit(record);
  144. }}
  145. >
  146. 编辑
  147. </a>
  148. <a
  149. style={{ color: 'red' }}
  150. onClick={() => {
  151. toDel(record);
  152. }}
  153. >
  154. 删除
  155. </a>
  156. </Space>
  157. ),
  158. },
  159. ];
  160. const paginationProps = {
  161. showSizeChanger: true,
  162. showQuickJumper: true,
  163. showTotal: (total: number) => {
  164. return <span> 共 {total}条 </span>;
  165. },
  166. ...pagination,
  167. };
  168. return (
  169. <PageContainer>
  170. <div>
  171. <Card>
  172. <Form form={form} layout="inline" onFinish={onFinish}>
  173. <Form.Item name="name" label="角色名称">
  174. <Input placeholder="请输入角色名称" />
  175. </Form.Item>
  176. <Form.Item style={{ marginBottom: '10px' }}>
  177. <Space>
  178. <Button type="primary" htmlType="submit">
  179. <SearchOutlined />
  180. 查询
  181. </Button>
  182. <Button htmlType="button" onClick={onReset}>
  183. <ReloadOutlined />
  184. 重置
  185. </Button>
  186. </Space>
  187. </Form.Item>
  188. </Form>
  189. <Button htmlType="button" type="primary" style={{ margin: '20px 0' }} onClick={onAdd}>
  190. <PlusCircleOutlined />
  191. 新增角色
  192. </Button>
  193. <Table
  194. columns={columns}
  195. dataSource={dataList}
  196. rowKey={(record) => record.record_id}
  197. pagination={paginationProps}
  198. loading={loading}
  199. onChange={tableChange}
  200. />
  201. {visible && <Edit visible={visible} editCallback={handleEdit} params={editData} />}
  202. </Card>
  203. </div>
  204. </PageContainer>
  205. );
  206. };
  207. export default RoleManagement;