page.tsx 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. "use client";
  2. import {fetchApi, fetchFile} from "@/app/_modules/func";
  3. import {
  4. CaretDownOutlined,
  5. CheckOutlined,
  6. CloseOutlined,
  7. DeleteOutlined,
  8. ExclamationCircleFilled,
  9. FileAddOutlined,
  10. KeyOutlined,
  11. LoadingOutlined,
  12. PlusOutlined,
  13. ReloadOutlined,
  14. SearchOutlined,
  15. } from "@ant-design/icons";
  16. import type {ActionType, ProColumns, ProFormInstance,} from "@ant-design/pro-components";
  17. import {
  18. ModalForm,
  19. PageContainer,
  20. ProCard,
  21. ProForm,
  22. ProFormRadio,
  23. ProFormSelect,
  24. ProFormText,
  25. ProFormTextArea,
  26. ProFormTreeSelect,
  27. ProTable,
  28. } from "@ant-design/pro-components";
  29. import type {GetProp, TreeDataNode, UploadProps} from "antd";
  30. import {
  31. Button,
  32. Checkbox,
  33. Col,
  34. Dropdown,
  35. Flex,
  36. Form,
  37. Input,
  38. Modal,
  39. Row,
  40. Space,
  41. Spin,
  42. Switch,
  43. Tree,
  44. Typography,
  45. Upload,
  46. } from "antd";
  47. import {useRouter} from "next/navigation";
  48. import {
  49. faDownload,
  50. faPenToSquare,
  51. faToggleOff,
  52. faToggleOn,
  53. faUpload,
  54. faUsers,
  55. } from "@fortawesome/free-solid-svg-icons";
  56. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  57. import {useEffect, useMemo, useRef, useState} from "react";
  58. type FileType = Parameters<GetProp<UploadProps, "beforeUpload">>[0];
  59. const { Dragger } = Upload;
  60. export type OptionType = {
  61. label: string;
  62. value: string | number;
  63. };
  64. export default function User() {
  65. const { push } = useRouter();
  66. // 添加用于控制删除确认模态框的状态
  67. const [deleteModalVisible, setDeleteModalVisible] = useState(false);
  68. const [deleteUserId, setDeleteUserId] = useState<string | number | null>(null);
  69. // 添加用于控制切换状态确认模态框的状态
  70. const [statusModalVisible, setStatusModalVisible] = useState(false);
  71. const [statusChecked, setStatusChecked] = useState<boolean>(true);
  72. const [statusRecord, setStatusRecord] = useState<any>(null);
  73. //新建用户预置密码值
  74. const [defaultPassword, setDefaultPassword] = useState("");
  75. useEffect(() => {
  76. queryDefaultPassword();
  77. queryPostion();
  78. queryOrgTree();
  79. }, []);
  80. //控制行的状态值的恢复
  81. const [rowStatusMap, setRowStatusMap] = useState<{ [key: number]: boolean }>(
  82. {}
  83. );
  84. //表格列定义
  85. const columns: ProColumns[] = [
  86. {
  87. title: "用户编号",
  88. dataIndex: "userId",
  89. search: false,
  90. },
  91. {
  92. title: "用户名称",
  93. fieldProps: {
  94. placeholder: "请输入用户名称",
  95. },
  96. dataIndex: "userName",
  97. ellipsis: true,
  98. sorter: true,
  99. order: 4,
  100. },
  101. {
  102. title: "用户昵称",
  103. dataIndex: "nickName",
  104. ellipsis: true,
  105. sorter: true,
  106. search: false,
  107. },
  108. {
  109. title: "部门名称",
  110. key: "deptName",
  111. search: false,
  112. render: (text, record) => record.dept?.deptName,
  113. },
  114. {
  115. title: "手机号",
  116. fieldProps: {
  117. placeholder: "请输入手机号",
  118. },
  119. dataIndex: "phonenumber",
  120. order: 3,
  121. },
  122. {
  123. title: "状态",
  124. fieldProps: {
  125. placeholder: "请选择用户状态",
  126. },
  127. dataIndex: "status",
  128. valueType: "select",
  129. order: 2,
  130. valueEnum: {
  131. 0: {
  132. text: "正常",
  133. status: "0",
  134. },
  135. 1: {
  136. text: "停用",
  137. status: "1",
  138. },
  139. },
  140. render: (text, record) => {
  141. return (
  142. <Space>
  143. <Switch
  144. checkedChildren={<CheckOutlined />}
  145. unCheckedChildren={<CloseOutlined />}
  146. defaultChecked={record.status === "0"}
  147. checked={rowStatusMap[record.userId]}
  148. disabled={record.userId == 1}
  149. onChange={(checked, event) => {
  150. showSwitchUserStatusModal(checked, record);
  151. }}
  152. />
  153. </Space>
  154. );
  155. },
  156. },
  157. {
  158. title: "创建时间",
  159. dataIndex: "createTime",
  160. valueType: "dateTime",
  161. search: false,
  162. sorter: true,
  163. },
  164. {
  165. title: "创建时间",
  166. fieldProps: {
  167. placeholder: ["开始日期", "结束日期"],
  168. },
  169. dataIndex: "createTimeRange",
  170. valueType: "dateRange",
  171. hideInTable: true,
  172. order: 1,
  173. search: {
  174. transform: (value) => {
  175. return {
  176. "params[beginTime]": `${value[0]} 00:00:00`,
  177. "params[endTime]": `${value[1]} 23:59:59`,
  178. };
  179. },
  180. },
  181. },
  182. {
  183. title: "操作",
  184. key: "option",
  185. search: false,
  186. render: (_, record) => {
  187. if (record.userId != 1)
  188. return [
  189. <Button
  190. key="modifyBtn"
  191. type="link"
  192. icon={<FontAwesomeIcon icon={faPenToSquare} />}
  193. onClick={() => showRowModifyModal(record)}
  194. >
  195. 修改
  196. </Button>,
  197. <Button
  198. key="deleteBtn"
  199. type="link"
  200. danger
  201. icon={<DeleteOutlined />}
  202. onClick={() => onClickDeleteRow(record)}
  203. >
  204. 删除
  205. </Button>,
  206. <Dropdown
  207. key="moreDrop"
  208. menu={{
  209. items: [
  210. {
  211. key: "1",
  212. label: (
  213. <a
  214. onClick={() => {
  215. modifyUserPwd(record);
  216. }}
  217. >
  218. 重置密码
  219. </a>
  220. ),
  221. icon: <KeyOutlined />,
  222. },
  223. {
  224. key: "2",
  225. label: (
  226. <a
  227. onClick={() =>
  228. push(`/system/user/auth/${record.userId}`)
  229. }
  230. >
  231. 分配角色
  232. </a>
  233. ),
  234. icon: <FontAwesomeIcon icon={faUsers} />,
  235. },
  236. ],
  237. }}
  238. >
  239. <a onClick={(e) => e.preventDefault()}>
  240. <Space>
  241. 更多
  242. <CaretDownOutlined />
  243. </Space>
  244. </a>
  245. </Dropdown>,
  246. ];
  247. },
  248. },
  249. ];
  250. //是否展示修改用户对话框
  251. const [showModifyUserModal, setShowModifyUserModal] = useState(false);
  252. //展示修改用户对话框
  253. const showRowModifyModal = (record?: any) => {
  254. queryUserInfo(record);
  255. setShowModifyUserModal(true);
  256. };
  257. //是否展示修改密码
  258. const [showModifyUserPwdModal, setShowModifyUserPwdModal] = useState(false);
  259. //重置密码表单引用
  260. const [pwdFormRef] = Form.useForm();
  261. const modifyUserPwd = (record: any) => {
  262. attachUserdata["userId"] = record.userId;
  263. attachUserdata["userName"] = record.userName;
  264. setAttachUserdata(attachUserdata);
  265. setShowModifyUserPwdModal(true);
  266. };
  267. //确认重置密码
  268. const confirmModifyUserPwd = () => {
  269. pwdFormRef.submit();
  270. };
  271. //取消重置密码
  272. const cancelModifyUserPwd = () => {
  273. setShowModifyUserPwdModal(false);
  274. };
  275. //执行重置密码
  276. const executeModifyUserPwd = async (values: any) => {
  277. setShowModifyUserPwdModal(false);
  278. values["userId"] = attachUserdata["userId"];
  279. const body = await fetchApi("/api/system/user/resetPwd", push, {
  280. method: "PUT",
  281. headers: {
  282. "Content-Type": "application/json",
  283. },
  284. body: JSON.stringify(values),
  285. });
  286. if (body != undefined) {
  287. if (body.code == 200) {
  288. App.useApp().message.success(`修改${attachUserdata["userName"]}密码成功`);
  289. } else {
  290. App.useApp().message.error(body.msg);
  291. }
  292. }
  293. pwdFormRef.resetFields();
  294. };
  295. //查询用户数据
  296. const getUser = async (params: any, sorter: any, filter: any) => {
  297. const searchParams = {
  298. pageNum: params.current,
  299. ...params,
  300. };
  301. delete searchParams.current;
  302. const queryParams = new URLSearchParams(searchParams);
  303. Object.keys(sorter).forEach((key) => {
  304. queryParams.append("orderByColumn", key);
  305. if (sorter[key] === "ascend") {
  306. queryParams.append("isAsc", "ascending");
  307. } else {
  308. queryParams.append("isAsc", "descending");
  309. }
  310. });
  311. //如果有组织id,添加相应查询参数
  312. if (searchDeptId != 0) {
  313. queryParams.append("deptId", searchDeptId.toString());
  314. }
  315. const body = await fetchApi(`/api/system/user/list?${queryParams}`, push);
  316. if (body !== undefined) {
  317. body.rows.forEach((row: any) => {
  318. setRowStatusMap({ ...rowStatusMap, [row.userId]: row.status === "0" });
  319. });
  320. }
  321. return body;
  322. };
  323. //展示切换用户状态对话框
  324. const showSwitchUserStatusModal = (checked: boolean, record: any) => {
  325. setRowStatusMap({ ...rowStatusMap, [record.userId]: checked });
  326. setStatusChecked(checked);
  327. setStatusRecord(record);
  328. setStatusModalVisible(true);
  329. };
  330. //确认变更用户状态
  331. const executeSwitchStatus = async (
  332. checked: boolean,
  333. userId: string,
  334. erroCallback: () => void
  335. ) => {
  336. const modifyData = {
  337. userId: userId,
  338. status: checked ? "0" : "1",
  339. };
  340. const body = await fetchApi(`/api/system/user/changeStatus`, push, {
  341. method: "PUT",
  342. headers: {
  343. "Content-Type": "application/json",
  344. },
  345. body: JSON.stringify(modifyData),
  346. });
  347. if (body !== undefined) {
  348. if (body.code == 200) {
  349. App.useApp().message.success(body.msg);
  350. } else {
  351. App.useApp().message.error(body.msg);
  352. erroCallback();
  353. }
  354. }
  355. };
  356. //确定切换状态
  357. const confirmSwitchStatus = () => {
  358. if (!statusRecord) return;
  359. executeSwitchStatus(statusChecked, statusRecord.userId, () => {
  360. setRowStatusMap({ ...rowStatusMap, [statusRecord.userId]: !statusChecked });
  361. });
  362. setStatusModalVisible(false);
  363. setStatusRecord(null);
  364. };
  365. //取消切换状态
  366. const cancelSwitchStatus = () => {
  367. if (!statusRecord) return;
  368. setRowStatusMap({ ...rowStatusMap, [statusRecord.userId]: !statusChecked });
  369. setStatusModalVisible(false);
  370. setStatusRecord(null);
  371. };
  372. //删除按钮是否可用,选中行时才可用
  373. const [rowCanDelete, setRowCanDelete] = useState(false);
  374. //选中行操作
  375. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  376. const [selectedRow, setSelectedRow] = useState(undefined as any);
  377. //修改按钮是否可用
  378. const [rowCanModify, setRowCanModify] = useState(false);
  379. const rowSelection = {
  380. onChange: (newSelectedRowKeys: React.Key[], selectedRows: any[]) => {
  381. setSelectedRowKeys(newSelectedRowKeys);
  382. setRowCanDelete(newSelectedRowKeys && newSelectedRowKeys.length > 0);
  383. if (newSelectedRowKeys && newSelectedRowKeys.length == 1) {
  384. setSelectedRow(selectedRows[0]);
  385. setRowCanModify(true);
  386. } else {
  387. setRowCanModify(false);
  388. setSelectedRow(undefined);
  389. }
  390. },
  391. getCheckboxProps: (record: any) => ({
  392. disabled: record.userId == 1,
  393. }),
  394. };
  395. //查询用的组织id
  396. const [searchDeptId, setSearchDeptId] = useState(0);
  397. //选择组织树执行过滤
  398. const selectOrgData = (selectedDeptKey: React.Key[]) => {
  399. if (selectedDeptKey && selectedDeptKey.length > 0) {
  400. setSearchDeptId(selectedDeptKey[0] as number);
  401. } else {
  402. setSearchDeptId(0);
  403. }
  404. if (formRef.current) {
  405. formRef.current.submit();
  406. }
  407. };
  408. //用于搜索的组织选择数据
  409. const [orgTreeData, setOrgTreeData] = useState([] as Array<TreeDataNode>);
  410. //用于对话框的组织选择数据
  411. const [orgSelectData, setOrgSelectData] = useState([]);
  412. //查询组织树
  413. const queryOrgTree = async () => {
  414. const body = await fetchApi("/api/system/user/deptTree", push);
  415. if (body !== undefined) {
  416. setOrgTreeData(generateOrgTree(body.data));
  417. setSearchValue("");
  418. setOrgSelectData(body.data);
  419. }
  420. };
  421. //搜索部门的值
  422. const [searchValue, setSearchValue] = useState("");
  423. //搜索组织树数据
  424. const onSearchDept = (e: React.ChangeEvent<HTMLInputElement>) => {
  425. setSearchValue(e.target.value);
  426. };
  427. //搜索过滤后的组织树展示数据
  428. const filterOrgTree = useMemo(() => {
  429. const loop = (data: TreeDataNode[]): TreeDataNode[] =>
  430. data.map((item) => {
  431. const strTitle = item.title as string;
  432. const index = strTitle.indexOf(searchValue);
  433. const beforeStr = strTitle.substring(0, index);
  434. const afterStr = strTitle.slice(index + searchValue.length);
  435. const title =
  436. index > -1 ? (
  437. <span>
  438. {beforeStr}
  439. <span style={{ color: "#f50" }}>{searchValue}</span>
  440. {afterStr}
  441. </span>
  442. ) : (
  443. <span>{strTitle}</span>
  444. );
  445. if (item.children) {
  446. return { title, key: item.key, children: loop(item.children) };
  447. }
  448. return {
  449. title,
  450. key: item.key,
  451. };
  452. });
  453. const data = loop(orgTreeData);
  454. return data;
  455. }, [orgTreeData, searchValue]);
  456. const generateOrgTree = (orgData: []) => {
  457. const children: Array<TreeDataNode> = new Array<TreeDataNode>();
  458. orgData.forEach((parent: any) => {
  459. const hasChild = parent.children && parent.children.length > 0;
  460. const node: TreeDataNode = {
  461. title: parent.label,
  462. key: parent.id,
  463. };
  464. children.push(node);
  465. if (hasChild) {
  466. generateOrgChildTree(parent.children, node);
  467. }
  468. });
  469. return children;
  470. };
  471. const generateOrgChildTree = (orgData: [], parent: TreeDataNode) => {
  472. const children: Array<TreeDataNode> = new Array<TreeDataNode>();
  473. orgData.forEach((item: any) => {
  474. const hasChild = item.children && item.children.length > 0;
  475. const node: TreeDataNode = {
  476. title: item.label,
  477. key: item.id,
  478. isLeaf: !hasChild,
  479. };
  480. children.push(node);
  481. if (hasChild) {
  482. generateOrgChildTree(item.children, node);
  483. }
  484. });
  485. parent.children = children;
  486. return parent;
  487. };
  488. //查询性别分类
  489. const querySexType = async () => {
  490. const body = await fetchApi(
  491. "/api/system/dict/data/type/sys_user_sex",
  492. push
  493. );
  494. if (body !== undefined) {
  495. return body.data;
  496. }
  497. };
  498. //查询新建用户预置密码
  499. const queryDefaultPassword = async () => {
  500. const body = await fetchApi(
  501. "/api/system/config/configKey/sys.user.initPassword",
  502. push
  503. );
  504. if (body !== undefined) {
  505. setDefaultPassword(body.msg);
  506. }
  507. };
  508. //岗位数据
  509. const [positionValue, setPositionValue] = useState<{ [key: number]: string }>(
  510. {}
  511. );
  512. //角色数据
  513. const [roleValue, setRoleValue] = useState<{ [key: number]: string }>({});
  514. //查询岗位
  515. const queryPostion = async () => {
  516. const body = await fetchApi("/api/system/user/", push);
  517. if (body !== undefined) {
  518. body.posts.forEach((post: any) => {
  519. positionValue[post.postId] = post.postName;
  520. setPositionValue(positionValue);
  521. });
  522. body.roles.forEach((role: any) => {
  523. roleValue[role.roleId] = role.roleName;
  524. setRoleValue(roleValue);
  525. });
  526. }
  527. };
  528. //确定新建用户
  529. const executeAddUser = async (values: any) => {
  530. const body = await fetchApi("/api/system/user", push, {
  531. method: "POST",
  532. headers: {
  533. "Content-Type": "application/json",
  534. },
  535. body: JSON.stringify(values),
  536. });
  537. if (body != undefined) {
  538. if (body.code == 200) {
  539. App.useApp().message.success(body.msg);
  540. if (actionRef.current) {
  541. actionRef.current.reload();
  542. }
  543. return true;
  544. }
  545. App.useApp().message.error(body.msg);
  546. return false;
  547. }
  548. return false;
  549. };
  550. //修改用户表单引用
  551. const modifyFormRef = useRef<ProFormInstance>(null);
  552. //待修改用户的岗位可选数据
  553. const [modifyPositionValue, setModifyPositionValue] = useState(
  554. [] as Array<OptionType>
  555. );
  556. //待修改用户的角色可选数据
  557. const [modifyRoleValue, setModifyRoleValue] = useState(
  558. [] as Array<OptionType>
  559. );
  560. //操作用户的附加数据
  561. const [attachUserdata, setAttachUserdata] = useState<{ [key: string]: any }>(
  562. {}
  563. );
  564. //查询用户信息
  565. const queryUserInfo = async (record?: any) => {
  566. const userId = record !== undefined ? record.userId : selectedRow.userId;
  567. const userName =
  568. record !== undefined ? record.userName : selectedRow.userName;
  569. attachUserdata["userId"] = userId;
  570. attachUserdata["userName"] = userName;
  571. setAttachUserdata(attachUserdata);
  572. if (userId !== undefined) {
  573. const body = await fetchApi(`/api/system/user/${userId}`, push);
  574. if (body !== undefined) {
  575. if (body.code == 200) {
  576. const positionArray: Array<OptionType> = new Array<OptionType>();
  577. body.posts.forEach((post: any) => {
  578. const option: OptionType = {
  579. label: post.postName,
  580. value: post.postId,
  581. };
  582. positionArray.push(option);
  583. });
  584. setModifyPositionValue(positionArray);
  585. const roeArray: Array<OptionType> = new Array<OptionType>();
  586. body.roles.forEach((role: any) => {
  587. const option: OptionType = {
  588. label: role.roleName,
  589. value: role.roleId,
  590. };
  591. roeArray.push(option);
  592. });
  593. setModifyRoleValue(roeArray);
  594. modifyFormRef?.current?.setFieldsValue({
  595. nickName: body.data.nickName,
  596. deptId: body.data.deptId,
  597. phonenumber: body.data.phonenumber,
  598. email: body.data.email,
  599. sex: body.data.sex,
  600. status: body.data.status,
  601. postIds: body.postIds,
  602. roleIds: body.roleIds,
  603. remark: body.data.remark,
  604. });
  605. }
  606. }
  607. }
  608. };
  609. //确认修改用户
  610. const executeModifyUser = async (values: any) => {
  611. values["userId"] = attachUserdata["userId"];
  612. values["userName"] = attachUserdata["userName"];
  613. const body = await fetchApi("/api/system/user", push, {
  614. method: "PUT",
  615. headers: {
  616. "Content-Type": "application/json",
  617. },
  618. body: JSON.stringify(values),
  619. });
  620. if (body !== undefined) {
  621. if (body.code == 200) {
  622. App.useApp().message.success(body.msg);
  623. //刷新列表
  624. if (actionRef.current) {
  625. actionRef.current.reload();
  626. }
  627. return true;
  628. }
  629. App.useApp().message.error(body.msg);
  630. return false;
  631. }
  632. };
  633. //点击删除按钮
  634. const onClickDeleteRow = (record?: any) => {
  635. const userId = record !== undefined ? record.userId : selectedRowKeys.join(",");
  636. setDeleteUserId(userId);
  637. setDeleteModalVisible(true);
  638. };
  639. //确定删除选中的用户
  640. const executeDeleteRow = async () => {
  641. if (deleteUserId === null) return;
  642. const body = await fetchApi(`/api/system/user/${deleteUserId}`, push, {
  643. method: "DELETE",
  644. });
  645. if (body !== undefined) {
  646. if (body.code == 200) {
  647. App.useApp().message.success("删除成功");
  648. //修改按钮变回不可点击
  649. setRowCanModify(false);
  650. //删除按钮变回不可点击
  651. setRowCanDelete(false);
  652. //选中行数据重置为空
  653. setSelectedRowKeys([]);
  654. //刷新列表
  655. if (actionRef.current) {
  656. actionRef.current.reload();
  657. }
  658. } else {
  659. App.useApp().message.error(body.msg);
  660. }
  661. }
  662. setDeleteModalVisible(false);
  663. setDeleteUserId(null);
  664. };
  665. //选中上传文件列表
  666. const [fileList, setFileList] = useState<FileType[]>([]);
  667. //上传前检查
  668. const beforeUpload = (file: FileType) => {
  669. setFileList([file]);
  670. const isExcel =
  671. file.type ===
  672. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  673. if (!isExcel) {
  674. App.useApp().message.error("请上传 xls、xlsx 格式文件!");
  675. setFileList([]);
  676. }
  677. return false;
  678. };
  679. //移除待上传文件
  680. const removeFile = () => {
  681. setFileList([]);
  682. };
  683. //上传文件是否刷新已有用户数据
  684. const [uploadSupport, setUploadSupport] = useState(false);
  685. //文件上传状态
  686. const [uploading, setUploading] = useState(false);
  687. //上传处理,手动上传下不会执行
  688. const handleChange: UploadProps["onChange"] = (info: any) => {
  689. if (info.file.status === "uploading") {
  690. setUploading(true);
  691. return;
  692. }
  693. if (info.file.status === "done") {
  694. setUploading(false);
  695. console.log(info.file.response);
  696. if (info.file.response.code == 200) {
  697. App.useApp().message.success(info.file.response.msg);
  698. } else {
  699. App.useApp().message.error(info.file.response.msg);
  700. }
  701. }
  702. };
  703. //导入对话框是否展示
  704. const [showImportModal, setShowImportModal] = useState(false);
  705. //点击导入按钮
  706. const onClickImport = () => {
  707. setShowImportModal(true);
  708. };
  709. //确定导入
  710. const executeImport = async () => {
  711. if (fileList.length == 0) {
  712. App.useApp().message.error("请选择上传的文件");
  713. return;
  714. }
  715. setUploading(true);
  716. const file = fileList[0];
  717. const formData = new FormData();
  718. formData.append("file", file);
  719. const body = await fetchApi(
  720. `/api/system/user/importData?updateSupport=${uploadSupport}`,
  721. push,
  722. {
  723. method: "POST",
  724. body: formData,
  725. }
  726. );
  727. setUploading(false);
  728. setUploadSupport(false);
  729. if (body !== undefined) {
  730. setFileList([]);
  731. if (body.code == 200) {
  732. App.useApp().message.success("用户导入成功");
  733. //刷新列表
  734. if (actionRef.current) {
  735. actionRef.current.reload();
  736. }
  737. } else {
  738. App.useApp().message.error(body.msg);
  739. }
  740. }
  741. };
  742. //取消导入对话框
  743. const cancelImportModal = () => {
  744. setShowImportModal(false);
  745. setUploadSupport(false);
  746. setFileList([]);
  747. };
  748. //搜索栏显示状态
  749. const [showSearch, setShowSearch] = useState(true);
  750. //action对象引用
  751. const actionRef = useRef<ActionType>(null);
  752. //表单对象引用
  753. const formRef = useRef<ProFormInstance>(null!);
  754. //当前页数和每页条数
  755. const [page, setPage] = useState(1);
  756. const defaultPageSize = 10;
  757. const [pageSize, setPageSize] = useState(defaultPageSize);
  758. const pageChange = (page: number, pageSize: number) => {
  759. setPage(page);
  760. setPageSize(pageSize);
  761. };
  762. //导出用户
  763. const exportTable = async () => {
  764. if (formRef.current) {
  765. const formData = new FormData();
  766. const data = {
  767. pageNum: page,
  768. pageSize: pageSize,
  769. ...formRef.current.getFieldsValue(),
  770. };
  771. Object.keys(data).forEach((key) => {
  772. if (data[key] !== undefined) {
  773. formData.append(key, data[key]);
  774. }
  775. });
  776. await fetchFile(
  777. "/api/system/user/export",
  778. push,
  779. {
  780. method: "POST",
  781. body: formData,
  782. },
  783. `user_${new Date().getTime()}.xlsx`
  784. );
  785. }
  786. };
  787. return (
  788. <PageContainer title={false}>
  789. <Row gutter={{ xs: 8, sm: 8, md: 8 }}>
  790. <Col xs={24} sm={6} md={6}>
  791. <ProCard>
  792. <Input
  793. style={{ marginBottom: 16 }}
  794. placeholder="输入部门名称搜索"
  795. prefix={<SearchOutlined />}
  796. onChange={onSearchDept}
  797. />
  798. {filterOrgTree.length > 0 ? (
  799. <Flex>
  800. <Tree
  801. switcherIcon={<CaretDownOutlined />}
  802. defaultExpandAll
  803. onSelect={selectOrgData}
  804. treeData={filterOrgTree}
  805. />
  806. </Flex>
  807. ) : (
  808. <Flex justify="center" style={{ marginTop: "16px" }}>
  809. <Spin />
  810. </Flex>
  811. )}
  812. </ProCard>
  813. </Col>
  814. <Col xs={24} sm={18} md={18}>
  815. <ProTable
  816. formRef={formRef}
  817. rowKey="userId"
  818. rowSelection={{
  819. selectedRowKeys,
  820. ...rowSelection,
  821. }}
  822. columns={columns}
  823. request={async (params: any, sorter: any, filter: any) => {
  824. // 表单搜索项会从 params 传入,传递给后端接口。
  825. const data = await getUser(params, sorter, filter);
  826. if (data !== undefined) {
  827. return Promise.resolve({
  828. data: data.rows,
  829. success: true,
  830. total: data.total,
  831. });
  832. }
  833. return Promise.resolve({
  834. data: [],
  835. success: true,
  836. });
  837. }}
  838. pagination={{
  839. defaultPageSize: defaultPageSize,
  840. showQuickJumper: true,
  841. showSizeChanger: true,
  842. onChange: pageChange,
  843. }}
  844. search={
  845. showSearch
  846. ? {
  847. defaultCollapsed: false,
  848. searchText: "搜索",
  849. }
  850. : false
  851. }
  852. dateFormatter="string"
  853. actionRef={actionRef}
  854. toolbar={{
  855. actions: [
  856. <ModalForm
  857. key="addmodal"
  858. title="添加用户"
  859. trigger={
  860. <Button icon={<PlusOutlined />} type="primary">
  861. 新建
  862. </Button>
  863. }
  864. autoFocusFirstInput
  865. modalProps={{
  866. destroyOnHidden: true,
  867. }}
  868. submitTimeout={2000}
  869. onFinish={executeAddUser}
  870. >
  871. <ProForm.Group>
  872. <ProFormText
  873. width="md"
  874. name="nickName"
  875. label="用户昵称"
  876. placeholder="请输入用户昵称"
  877. rules={[{ required: true, message: "请输入用户昵称" }]}
  878. />
  879. <ProFormTreeSelect
  880. width="md"
  881. name="deptId"
  882. label="归属部门"
  883. placeholder="请选择归属部门"
  884. request={async () => {
  885. return orgSelectData;
  886. }}
  887. fieldProps={{
  888. filterTreeNode: true,
  889. showSearch: true,
  890. treeNodeFilterProp: "name",
  891. fieldNames: {
  892. label: "name",
  893. value: "id",
  894. },
  895. }}
  896. />
  897. </ProForm.Group>
  898. <ProForm.Group>
  899. <ProFormText
  900. width="md"
  901. name="phonenumber"
  902. label="手机号码"
  903. placeholder="请输入手机号码"
  904. rules={[
  905. {
  906. pattern: /^1\d{10}$/,
  907. message: "请输入正确的手机号码",
  908. },
  909. ]}
  910. />
  911. <ProFormText
  912. width="md"
  913. name="email"
  914. label="邮箱"
  915. placeholder="请输入邮箱"
  916. rules={[
  917. { type: "email", message: "请输入正确的邮箱地址" },
  918. ]}
  919. />
  920. </ProForm.Group>
  921. <ProForm.Group>
  922. <ProFormText
  923. width="md"
  924. name="userName"
  925. label="用户名称"
  926. placeholder="请输入用户名称"
  927. rules={[{ required: true, message: "请输入用户名称" }]}
  928. />
  929. <ProFormText.Password
  930. width="md"
  931. name="password"
  932. label="用户密码"
  933. initialValue={defaultPassword}
  934. placeholder="请输入用户密码"
  935. />
  936. </ProForm.Group>
  937. <ProForm.Group>
  938. <ProFormSelect
  939. width="md"
  940. name="sex"
  941. label="用户性别"
  942. request={querySexType}
  943. fieldProps={{
  944. fieldNames: {
  945. label: "dictLabel",
  946. value: "dictValue",
  947. },
  948. }}
  949. />
  950. <ProFormRadio.Group
  951. name="status"
  952. width="sm"
  953. label="状态"
  954. initialValue="0"
  955. options={[
  956. {
  957. label: "正常",
  958. value: "0",
  959. },
  960. {
  961. label: "停用",
  962. value: "1",
  963. },
  964. ]}
  965. />
  966. </ProForm.Group>
  967. <ProForm.Group>
  968. <ProFormSelect
  969. width="md"
  970. name="postIds"
  971. label="岗位"
  972. fieldProps={{
  973. mode: "multiple",
  974. }}
  975. valueEnum={positionValue}
  976. />
  977. <ProFormSelect
  978. width="md"
  979. name="roleIds"
  980. label="角色"
  981. fieldProps={{
  982. mode: "multiple",
  983. }}
  984. valueEnum={roleValue}
  985. />
  986. </ProForm.Group>
  987. <ProFormTextArea
  988. name="remark"
  989. width={688}
  990. label="备注"
  991. placeholder="请输入内容"
  992. />
  993. </ModalForm>,
  994. <ModalForm
  995. key="modifymodal"
  996. title="修改用户"
  997. formRef={modifyFormRef}
  998. trigger={
  999. <Button
  1000. icon={<FontAwesomeIcon icon={faPenToSquare} />}
  1001. disabled={!rowCanModify}
  1002. onClick={() => showRowModifyModal()}
  1003. >
  1004. 修改
  1005. </Button>
  1006. }
  1007. open={showModifyUserModal}
  1008. autoFocusFirstInput
  1009. modalProps={{
  1010. destroyOnHidden: true,
  1011. onCancel: () => {
  1012. setShowModifyUserModal(false);
  1013. },
  1014. }}
  1015. submitTimeout={2000}
  1016. onFinish={executeModifyUser}
  1017. >
  1018. <ProForm.Group>
  1019. <ProFormText
  1020. width="md"
  1021. name="nickName"
  1022. label="用户昵称"
  1023. placeholder="请输入用户昵称"
  1024. rules={[{ required: true, message: "请输入用户昵称" }]}
  1025. />
  1026. <ProFormTreeSelect
  1027. width="md"
  1028. name="deptId"
  1029. label="归属部门"
  1030. placeholder="请选择归属部门"
  1031. request={async () => {
  1032. return orgSelectData;
  1033. }}
  1034. fieldProps={{
  1035. filterTreeNode: true,
  1036. showSearch: true,
  1037. treeNodeFilterProp: "label",
  1038. fieldNames: {
  1039. label: "label",
  1040. value: "key",
  1041. },
  1042. }}
  1043. />
  1044. </ProForm.Group>
  1045. <ProForm.Group>
  1046. <ProFormText
  1047. width="md"
  1048. name="phonenumber"
  1049. label="手机号码"
  1050. placeholder="请输入手机号码"
  1051. rules={[
  1052. {
  1053. pattern: /^1\d{10}$/,
  1054. message: "请输入正确的手机号码",
  1055. },
  1056. ]}
  1057. />
  1058. <ProFormText
  1059. width="md"
  1060. name="email"
  1061. label="邮箱"
  1062. placeholder="请输入邮箱"
  1063. rules={[
  1064. { type: "email", message: "请输入正确的邮箱地址" },
  1065. ]}
  1066. />
  1067. </ProForm.Group>
  1068. <ProForm.Group>
  1069. <ProFormSelect
  1070. width="md"
  1071. name="sex"
  1072. label="用户性别"
  1073. request={querySexType}
  1074. fieldProps={{
  1075. fieldNames: {
  1076. label: "dictLabel",
  1077. value: "dictValue",
  1078. },
  1079. }}
  1080. />
  1081. <ProFormRadio.Group
  1082. name="status"
  1083. width="sm"
  1084. label="状态"
  1085. options={[
  1086. {
  1087. label: "正常",
  1088. value: "0",
  1089. },
  1090. {
  1091. label: "停用",
  1092. value: "1",
  1093. },
  1094. ]}
  1095. />
  1096. </ProForm.Group>
  1097. <ProForm.Group>
  1098. <ProFormSelect
  1099. width="md"
  1100. name="postIds"
  1101. label="岗位"
  1102. fieldProps={{
  1103. mode: "multiple",
  1104. }}
  1105. options={modifyPositionValue}
  1106. />
  1107. <ProFormSelect
  1108. width="md"
  1109. name="roleIds"
  1110. label="角色"
  1111. fieldProps={{
  1112. mode: "multiple",
  1113. }}
  1114. options={modifyRoleValue}
  1115. />
  1116. </ProForm.Group>
  1117. <ProFormTextArea
  1118. name="remark"
  1119. width={688}
  1120. label="备注"
  1121. placeholder="请输入内容"
  1122. />
  1123. </ModalForm>,
  1124. <Button
  1125. key="danger"
  1126. danger
  1127. icon={<DeleteOutlined />}
  1128. disabled={!rowCanDelete}
  1129. onClick={() => onClickDeleteRow()}
  1130. >
  1131. 删除
  1132. </Button>,
  1133. <Button
  1134. key="import"
  1135. type="primary"
  1136. icon={<FontAwesomeIcon icon={faUpload} />}
  1137. onClick={onClickImport}
  1138. >
  1139. 导入
  1140. </Button>,
  1141. <Button
  1142. key="export"
  1143. type="primary"
  1144. icon={<FontAwesomeIcon icon={faDownload} />}
  1145. onClick={exportTable}
  1146. >
  1147. 导出
  1148. </Button>,
  1149. ],
  1150. settings: [
  1151. {
  1152. key: "switch",
  1153. icon: showSearch ? (
  1154. <FontAwesomeIcon icon={faToggleOn} />
  1155. ) : (
  1156. <FontAwesomeIcon icon={faToggleOff} />
  1157. ),
  1158. tooltip: showSearch ? "隐藏搜索栏" : "显示搜索栏",
  1159. onClick: (key: string | undefined) => {
  1160. setShowSearch(!showSearch);
  1161. },
  1162. },
  1163. {
  1164. key: "refresh",
  1165. tooltip: "刷新",
  1166. icon: <ReloadOutlined />,
  1167. onClick: (key: string | undefined) => {
  1168. if (actionRef.current) {
  1169. actionRef.current.reload();
  1170. }
  1171. },
  1172. },
  1173. ],
  1174. }}
  1175. />
  1176. </Col>
  1177. </Row>
  1178. <Modal
  1179. title={`修改${attachUserdata["userName"]}密码`}
  1180. open={showModifyUserPwdModal}
  1181. onOk={confirmModifyUserPwd}
  1182. onCancel={cancelModifyUserPwd}
  1183. >
  1184. <Form form={pwdFormRef} onFinish={executeModifyUserPwd}>
  1185. <Form.Item
  1186. label="新密码"
  1187. name="password"
  1188. rules={[{ required: true, message: "请输入新密码" }]}
  1189. >
  1190. <Input.Password />
  1191. </Form.Item>
  1192. </Form>
  1193. </Modal>
  1194. <Modal
  1195. title="用户导入"
  1196. open={showImportModal}
  1197. onOk={executeImport}
  1198. onCancel={cancelImportModal}
  1199. >
  1200. <Flex justify="center">
  1201. <div>
  1202. <Dragger
  1203. name="file"
  1204. accept=".xls,.xlsx"
  1205. listType="text"
  1206. multiple={false}
  1207. fileList={fileList}
  1208. beforeUpload={beforeUpload}
  1209. onChange={handleChange}
  1210. onRemove={removeFile}
  1211. showUploadList={{
  1212. showDownloadIcon: false,
  1213. showRemoveIcon: true,
  1214. removeIcon: <CloseOutlined />,
  1215. }}
  1216. >
  1217. <p className="ant-upload-drag-icon">
  1218. {uploading ? <LoadingOutlined /> : <FileAddOutlined />}
  1219. </p>
  1220. <p className="ant-upload-text">点击此处或拖曳文件到此处上传</p>
  1221. <p className="ant-upload-hint">仅支持 xls、xlsx 格式文件</p>
  1222. </Dragger>
  1223. </div>
  1224. </Flex>
  1225. <Flex justify="center" style={{ marginTop: 30 }}>
  1226. <Typography.Text>
  1227. <Checkbox
  1228. checked={uploadSupport}
  1229. onChange={(e) => {
  1230. setUploadSupport(e.target.checked);
  1231. }}
  1232. >
  1233. 允许更新已有用户的数据
  1234. </Checkbox>
  1235. </Typography.Text>
  1236. </Flex>
  1237. </Modal>
  1238. {/* 删除确认模态框 */}
  1239. <Modal
  1240. title={
  1241. <div style={{ display: 'flex', alignItems: 'center' }}>
  1242. <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
  1243. <span>系统提示</span>
  1244. </div>
  1245. }
  1246. open={deleteModalVisible}
  1247. onOk={executeDeleteRow}
  1248. onCancel={() => {
  1249. setDeleteModalVisible(false);
  1250. setDeleteUserId(null);
  1251. }}
  1252. okText="确认"
  1253. cancelText="取消"
  1254. >
  1255. <p>{`确定删除用户编号为“${deleteUserId}”的数据项?`}</p>
  1256. </Modal>
  1257. {/* 切换状态确认模态框 */}
  1258. <Modal
  1259. title={
  1260. <div style={{ display: 'flex', alignItems: 'center' }}>
  1261. <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
  1262. <span>系统提示</span>
  1263. </div>
  1264. }
  1265. open={statusModalVisible}
  1266. onOk={confirmSwitchStatus}
  1267. onCancel={cancelSwitchStatus}
  1268. okText="确认"
  1269. cancelText="取消"
  1270. >
  1271. <p>{`确认要${statusChecked ? "启用" : "停用"}"${statusRecord?.userName}"用户吗?`}</p>
  1272. </Modal>
  1273. </PageContainer>
  1274. );
  1275. }