index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { userApi } from "@/api";
  2. import { GetLogListParams, UserTableListType } from "@/types";
  3. import {
  4. Button,
  5. Form,
  6. FormInstance,
  7. Input,
  8. Popconfirm,
  9. Switch,
  10. Table,
  11. message,
  12. } from "antd";
  13. import { debounce } from "lodash";
  14. import { useCallback, useEffect, useMemo, useRef, useState } from "react";
  15. import UserAdd from "./components/UserAdd";
  16. import { DageTableActions } from "@dage/pc-components";
  17. const DEFAULT_PARAMS: GetLogListParams = {
  18. pageNum: 1,
  19. pageSize: 20,
  20. startTime: "",
  21. endTime: "",
  22. };
  23. export default function IndustrialMeta() {
  24. const formRef = useRef<FormInstance>(null);
  25. const [list, setList] = useState<UserTableListType[]>([]);
  26. const [total, setTotal] = useState(0);
  27. const [loading, setLoading] = useState(false);
  28. const [editPageShow, setEditPageShow] = useState(false);
  29. const editId = useRef(0);
  30. const [params, setParams] = useState<GetLogListParams>({
  31. ...DEFAULT_PARAMS,
  32. });
  33. const getList = useCallback(async () => {
  34. setLoading(true);
  35. try {
  36. const data = await userApi.getList(params);
  37. setList(data.records);
  38. setTotal(data.total);
  39. } finally {
  40. setLoading(false);
  41. }
  42. }, [params]);
  43. useEffect(() => {
  44. getList();
  45. }, [getList, params]);
  46. const debounceSearch = useMemo(
  47. () =>
  48. debounce((changedVal: unknown, vals: GetLogListParams) => {
  49. setParams({ ...params, ...vals });
  50. }, 500),
  51. [params]
  52. );
  53. // 切换表格中的启用停用状态
  54. const isEnabledClickFu = useCallback(
  55. async (val: boolean, id: number) => {
  56. const isDisable = val ? 1 : 0;
  57. await userApi.handleType(id, isDisable);
  58. getList();
  59. },
  60. [getList]
  61. );
  62. const paginationChange = useCallback(
  63. () => (pageNum: number, pageSize: number) => {
  64. setParams({ ...params, pageNum, pageSize });
  65. },
  66. [params]
  67. );
  68. const handleReset = useCallback(() => {
  69. setParams({ ...DEFAULT_PARAMS });
  70. formRef.current?.resetFields();
  71. }, [formRef]);
  72. // 点击删除
  73. const delTableFu = useCallback(
  74. async (id: number) => {
  75. await userApi.del(id);
  76. message.open({
  77. type: "success",
  78. content: "操作成功",
  79. });
  80. getList();
  81. },
  82. [getList]
  83. );
  84. // 点击重置密码
  85. const resetPassFu = useCallback(async (id: number) => {
  86. await userApi.resetPwd(id);
  87. message.open({
  88. type: "success",
  89. content: "操作成功",
  90. });
  91. }, []);
  92. const openEditPageFu = useCallback(
  93. (id: number) => {
  94. if (id === 0 && total >= 50)
  95. return message.open({
  96. type: "warning",
  97. content: "最多支持50个用户!",
  98. });
  99. editId.current = id;
  100. setEditPageShow(true);
  101. },
  102. [total]
  103. );
  104. const COLUMNS = useMemo(() => {
  105. return [
  106. {
  107. title: "账号名",
  108. dataIndex: "userName",
  109. },
  110. {
  111. title: "用户昵称",
  112. dataIndex: "nickName",
  113. },
  114. {
  115. title: "真实姓名",
  116. dataIndex: "realName",
  117. },
  118. {
  119. title: "注册时间",
  120. dataIndex: "createTime",
  121. },
  122. {
  123. title: "启用状态",
  124. render: (item: UserTableListType) => (
  125. <Switch
  126. disabled={item.isAdmin === 1}
  127. checkedChildren="启用"
  128. unCheckedChildren="停用"
  129. checked={item.isEnabled === 1}
  130. onChange={(val) => isEnabledClickFu(val, item.id)}
  131. />
  132. ),
  133. },
  134. {
  135. title: "操作",
  136. render: (item: UserTableListType) => {
  137. return item.isAdmin === 1 ? (
  138. "-"
  139. ) : (
  140. <DageTableActions
  141. renderBefore={
  142. <Popconfirm
  143. title="密码重制后为123456,是否重置?"
  144. okText="重置"
  145. cancelText="取消"
  146. onConfirm={() => resetPassFu(item.id)}
  147. >
  148. <Button size="small" type="text">
  149. 重置密码
  150. </Button>
  151. </Popconfirm>
  152. }
  153. onEdit={openEditPageFu.bind(undefined, item.id)}
  154. onDelete={delTableFu.bind(undefined, item.id)}
  155. />
  156. );
  157. },
  158. },
  159. ];
  160. }, [delTableFu, isEnabledClickFu, resetPassFu, openEditPageFu]);
  161. return (
  162. <div className="user">
  163. <Form ref={formRef} layout="inline" onValuesChange={debounceSearch}>
  164. <Form.Item label="账号" name="searchKey">
  165. <Input
  166. className="w220"
  167. placeholder="请输入关键字"
  168. maxLength={30}
  169. showCount
  170. allowClear
  171. />
  172. </Form.Item>
  173. <Form.Item>
  174. <Button type="primary" onClick={openEditPageFu.bind(undefined, 0)}>
  175. 新增
  176. </Button>
  177. </Form.Item>
  178. <Form.Item>
  179. <Button onClick={handleReset}>重置</Button>
  180. </Form.Item>
  181. </Form>
  182. <Table
  183. loading={loading}
  184. className="page-table"
  185. dataSource={list}
  186. columns={COLUMNS}
  187. rowKey="id"
  188. pagination={{
  189. showQuickJumper: true,
  190. position: ["bottomCenter"],
  191. showSizeChanger: true,
  192. current: params.pageNum,
  193. pageSize: params.pageSize,
  194. total,
  195. onChange: paginationChange(),
  196. }}
  197. />
  198. {/* 点击新增或者编辑 */}
  199. {editPageShow ? (
  200. <UserAdd
  201. id={editId.current}
  202. closePage={() => setEditPageShow(false)}
  203. upTableList={getList}
  204. addTableList={handleReset}
  205. />
  206. ) : null}
  207. </div>
  208. );
  209. }