123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React from "react";
  2. import { Button, Input, List, Popconfirm, Spin, Modal, message } from "antd";
  3. import useBool from "@/utils/hooks/useBool";
  4. import classNames from "classnames";
  5. import {
  6. getSysRole,
  7. postSysRole,
  8. putSysRole,
  9. deleteSysRole,
  10. } from "@/service/sysrole";
  11. import "./style.less";
  12. const Header = (props) => {
  13. return (
  14. <div className="role-list-header">
  15. <div className="role-list-header-body">{props.children}</div>
  16. <div className="role-list-header-action">{props.action}</div>
  17. </div>
  18. );
  19. };
  20. export default (props) => {
  21. const { current, onChange } = props;
  22. const [loading, startLoading, cancelLoading] = useBool();
  23. const [visible, openModel, hideModel] = useBool();
  24. const [finished, setFinished] = React.useState(false);
  25. const [inText, setInText] = React.useState();
  26. const [list, setList] = React.useState([]);
  27. const onAdd = () => {
  28. openModel();
  29. setInText();
  30. onChange();
  31. };
  32. const onEdit = (item) => {
  33. openModel();
  34. setInText(item.name);
  35. onChange(item);
  36. };
  37. const onDelete = (item) => {
  38. startLoading();
  39. // 删除角色
  40. deleteSysRole(item.roleId)
  41. .then(() => {
  42. setList(list.filter((x) => x.roleId !== item.roleId));
  43. cancelLoading();
  44. hideModel();
  45. onChange();
  46. })
  47. .catch(() => {
  48. cancelLoading();
  49. });
  50. };
  51. const handleOk = () => {
  52. if (!inText) {
  53. message.warn("请输入角色名称");
  54. return;
  55. }
  56. startLoading();
  57. const data = { name: inText };
  58. if (current) {
  59. // 修改角色
  60. putSysRole(current.roleId, data)
  61. .then((res) => {
  62. setList(list.map((x) => (x.roleId === current.roleId ? res : x)));
  63. cancelLoading();
  64. hideModel();
  65. })
  66. .catch(() => {
  67. cancelLoading();
  68. });
  69. } else {
  70. // 新增角色
  71. postSysRole(data)
  72. .then((res) => {
  73. setList([res].concat(list));
  74. cancelLoading();
  75. hideModel();
  76. onChange(res);
  77. })
  78. .catch(() => {
  79. cancelLoading();
  80. });
  81. }
  82. };
  83. const handleCancel = () => {
  84. if (loading) {
  85. return;
  86. }
  87. hideModel();
  88. };
  89. const queryRoles = React.useCallback((params) => {
  90. startLoading();
  91. getSysRole(params)
  92. .then((res) => {
  93. setList(res.records);
  94. cancelLoading();
  95. setFinished(res.current >= res.pages);
  96. })
  97. .catch(() => {
  98. cancelLoading();
  99. });
  100. }, []);
  101. React.useEffect(() => {
  102. queryRoles({ pageNum: 1, pageSize: 500 });
  103. }, []);
  104. return (
  105. <Spin spinning={loading}>
  106. <List
  107. header={
  108. <Header
  109. action={
  110. <Button type="primary" onClick={onAdd}>
  111. 新增
  112. </Button>
  113. }
  114. >
  115. 角色列表
  116. </Header>
  117. }
  118. style={{ background: "#fff" }}
  119. bordered
  120. dataSource={list}
  121. renderItem={(item) => (
  122. <List.Item
  123. className={classNames("role-list-item", {
  124. active: current && current.roleId === item.roleId,
  125. })}
  126. onClick={() => onChange(item)}
  127. actions={[
  128. <Button key="edit" type="link" onClick={() => onEdit(item)}>
  129. 编辑
  130. </Button>,
  131. <Popconfirm
  132. key="delete"
  133. title="确认删除?"
  134. onConfirm={() => onDelete(item)}
  135. >
  136. <Button type="link" danger>
  137. 删除
  138. </Button>
  139. </Popconfirm>,
  140. ]}
  141. >
  142. {item.name}
  143. </List.Item>
  144. )}
  145. />
  146. <Modal
  147. title="请输入角色名称"
  148. open={visible}
  149. onOk={handleOk}
  150. onCancel={handleCancel}
  151. >
  152. <Input value={inText} onChange={(e) => setInText(e.target.value)} />
  153. </Modal>
  154. </Spin>
  155. );
  156. };