index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import BreadTit from "@/components/BreadTit";
  2. import { RootState } from "@/store";
  3. import {
  4. delUserAPI,
  5. getUserListAPI,
  6. userEditStatusAPI,
  7. } from "@/store/action/system";
  8. import { Button, Input, message, Popconfirm, Switch, Table } from "antd";
  9. import React, {
  10. useCallback,
  11. useEffect,
  12. useMemo,
  13. useRef,
  14. useState,
  15. } from "react";
  16. import { useDispatch, useSelector } from "react-redux";
  17. import styles from "./index.module.scss";
  18. import "./index.css";
  19. import UserModal from "./UserModal";
  20. function System1() {
  21. const dispatch = useDispatch();
  22. const pageNumRef = useRef(1);
  23. const pagePageRef = useRef(10);
  24. // 筛选和分页
  25. const [tableSelect, setTableSelect] = useState({
  26. searchKey: "",
  27. pageSize: 10,
  28. pageNum: 1,
  29. });
  30. // 账号的输入
  31. const nameTime = useRef(-1);
  32. const nameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  33. clearTimeout(nameTime.current);
  34. nameTime.current = window.setTimeout(() => {
  35. setTableSelect({ ...tableSelect, searchKey: e.target.value, pageNum: 1 });
  36. }, 500);
  37. };
  38. useEffect(() => {
  39. pageNumRef.current = tableSelect.pageNum;
  40. pagePageRef.current = tableSelect.pageSize;
  41. dispatch(getUserListAPI(tableSelect));
  42. }, [dispatch, tableSelect]);
  43. // 点击新增或者编辑按钮
  44. const addSystem = (id?: any) => {
  45. if (id) idRef.current = id;
  46. else idRef.current = null;
  47. setOpen(true);
  48. };
  49. // 点击删除按钮
  50. const delOne = useCallback(
  51. async (id: number) => {
  52. const res: any = await delUserAPI(id);
  53. if (res.code === 0) {
  54. message.success("删除成功!");
  55. // 重新发请求更新页面
  56. dispatch(getUserListAPI(tableSelect));
  57. }
  58. },
  59. [dispatch, tableSelect]
  60. );
  61. // ---------关于表格
  62. // 切换表格中的启用停用状态
  63. const isEnabledClickFu = useCallback(
  64. async (val: boolean, id: number) => {
  65. const isDisable = val ? 1 : 0;
  66. const res: any = await userEditStatusAPI(id, isDisable);
  67. if (res.code === 0) dispatch(getUserListAPI(tableSelect));
  68. },
  69. [dispatch, tableSelect]
  70. );
  71. // 页码变化
  72. const paginationChange = (pageNum: number, pageSize: number) => {
  73. pageNumRef.current = pageNum;
  74. pagePageRef.current = pageSize;
  75. setTableSelect({ ...tableSelect, pageNum, pageSize });
  76. };
  77. const results = useSelector(
  78. (state: RootState) => state.systemStore.tableList
  79. );
  80. const columns = useMemo(() => {
  81. return [
  82. {
  83. title: "序号",
  84. render: (text: any, record: any, index: any) =>
  85. index + 1 + (pageNumRef.current - 1) * pagePageRef.current,
  86. },
  87. {
  88. title: "账号",
  89. dataIndex: "userName",
  90. },
  91. {
  92. title: "角色",
  93. dataIndex: "creatorName",
  94. },
  95. {
  96. title: "真实姓名",
  97. dataIndex: "realName",
  98. },
  99. {
  100. title: "手机号",
  101. render: (item: any) => (item.isAdmin === 1 ? "-" : item.phone),
  102. },
  103. {
  104. title: "状态",
  105. render: (item: any) => (
  106. <Switch
  107. checkedChildren="启用"
  108. unCheckedChildren="停用"
  109. checked={item.isEnabled === 1}
  110. onChange={(val) => isEnabledClickFu(val, item.id)}
  111. disabled={item.isAdmin === 1}
  112. />
  113. ),
  114. },
  115. {
  116. title: "操作",
  117. render: (item: any) => (
  118. <>
  119. {item.isAdmin === 1 ? (
  120. "-"
  121. ) : (
  122. <>
  123. <Button type="text" danger onClick={() => addSystem(item.id)}>
  124. 编辑
  125. </Button>
  126. <Popconfirm
  127. title="确定删除吗?"
  128. okText="确定"
  129. cancelText="取消"
  130. onConfirm={() => delOne(item.id)}
  131. >
  132. <Button type="text" danger>
  133. 删除
  134. </Button>
  135. </Popconfirm>
  136. </>
  137. )}
  138. </>
  139. ),
  140. },
  141. ];
  142. }, [delOne, isEnabledClickFu]);
  143. // 关于弹窗
  144. const [open, setOpen] = useState(false);
  145. const idRef = useRef<any>(null);
  146. // 在弹窗里面点击提交成功之后
  147. const btnOkSuccFu = useCallback(() => {
  148. dispatch(getUserListAPI(tableSelect));
  149. setOpen(false);
  150. }, [dispatch, tableSelect]);
  151. return (
  152. <div className={styles.System1}>
  153. <div className="breadTit">
  154. <BreadTit>
  155. <div className="breadTitRow active">用户管理</div>
  156. </BreadTit>
  157. </div>
  158. <div className="objectSonMain">
  159. <div className="tableSelectBox">
  160. <div className="row">
  161. <span>账号:</span>
  162. <Input
  163. maxLength={10}
  164. style={{ width: 150 }}
  165. placeholder="请输入"
  166. allowClear
  167. onChange={(e) => nameChange(e)}
  168. />
  169. </div>
  170. <div className="row">
  171. <Button type="primary" onClick={() => addSystem()}>
  172. 新增用户
  173. </Button>
  174. </div>
  175. </div>
  176. {/* 表格主体 */}
  177. <div className="tableMain">
  178. <Table
  179. scroll={{ y: 480 }}
  180. dataSource={results.list}
  181. columns={columns}
  182. rowKey="id"
  183. pagination={{
  184. showQuickJumper: true,
  185. position: ["bottomCenter"],
  186. showSizeChanger: true,
  187. current: tableSelect.pageNum,
  188. pageSize: tableSelect.pageSize,
  189. total: results.total,
  190. onChange: paginationChange,
  191. }}
  192. />
  193. </div>
  194. </div>
  195. {/* 点击新增或者编辑出来的弹窗 */}
  196. {open ? (
  197. <UserModal
  198. btnOkSuccFu={btnOkSuccFu}
  199. id={idRef.current}
  200. closeFu={() => setOpen(false)}
  201. />
  202. ) : null}
  203. </div>
  204. );
  205. }
  206. const MemoSystem1 = React.memo(System1);
  207. export default MemoSystem1;