page.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. "use client";
  2. import {fetchApi} from "@/app/_modules/func";
  3. import {DeleteOutlined, ExclamationCircleFilled, PlusOutlined, ReloadOutlined,} from "@ant-design/icons";
  4. import type {ActionType, ProColumns, ProFormInstance,} from "@ant-design/pro-components";
  5. import {
  6. ModalForm,
  7. PageContainer,
  8. ProForm,
  9. ProFormDigit,
  10. ProFormRadio,
  11. ProFormText,
  12. ProFormTreeSelect,
  13. ProTable,
  14. } from "@ant-design/pro-components";
  15. import {Button, Modal, Space, Tag} from "antd";
  16. import {useRouter} from "next/navigation";
  17. import {
  18. faArrowsUpDown,
  19. faCheck,
  20. faPenToSquare,
  21. faToggleOff,
  22. faToggleOn,
  23. faXmark,
  24. } from "@fortawesome/free-solid-svg-icons";
  25. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  26. import {useRef, useState} from "react";
  27. import globalMessage from "@/app/_modules/globalMessage";
  28. //查询表格数据API
  29. const queryAPI = "/api/system/dept/list";
  30. //新建数据API
  31. const newAPI = "/api/system/dept";
  32. //修改数据API
  33. const modifyAPI = "/api/system/dept";
  34. //查询详情数据API
  35. const queryDetailAPI = "/api/system/dept";
  36. //删除API
  37. const deleteAPI = "/api/system/dept";
  38. export default function Dept() {
  39. const { push } = useRouter();
  40. // 添加用于控制删除确认模态框的状态
  41. const [deleteModalVisible, setDeleteModalVisible] = useState(false);
  42. const [deleteRecord, setDeleteRecord] = useState<any>(null);
  43. //表格列定义
  44. const columns: ProColumns[] = [
  45. {
  46. title: "部门名称",
  47. fieldProps: {
  48. placeholder: "请输入部门名称",
  49. },
  50. dataIndex: "deptName",
  51. order: 2,
  52. },
  53. {
  54. title: "排序",
  55. dataIndex: "orderNum",
  56. search: false,
  57. },
  58. {
  59. title: "状态",
  60. fieldProps: {
  61. placeholder: "请选择部门状态",
  62. },
  63. dataIndex: "status",
  64. valueType: "select",
  65. render: (_, record) => {
  66. return (
  67. <Space>
  68. <Tag
  69. color={record.status === "0" ? "green" : "red"}
  70. icon={
  71. record.status == 0 ? (
  72. <FontAwesomeIcon icon={faCheck} />
  73. ) : (
  74. <FontAwesomeIcon icon={faXmark} />
  75. )
  76. }
  77. >
  78. {_}
  79. </Tag>
  80. </Space>
  81. );
  82. },
  83. valueEnum: {
  84. 0: {
  85. text: "正常",
  86. status: "0",
  87. },
  88. 1: {
  89. text: "停用",
  90. status: "1",
  91. },
  92. },
  93. order: 1,
  94. },
  95. {
  96. title: "创建时间",
  97. dataIndex: "createTime",
  98. valueType: "dateTime",
  99. search: false,
  100. },
  101. {
  102. title: "操作",
  103. key: "option",
  104. search: false,
  105. render: (_, record) => {
  106. if (record.deptId == 100) {
  107. return [
  108. <Button
  109. key="modifyBtn"
  110. type="link"
  111. icon={<FontAwesomeIcon icon={faPenToSquare} />}
  112. onClick={() => onClickShowRowModifyModal(record)}
  113. >
  114. 修改
  115. </Button>,
  116. <Button
  117. key="newBtn"
  118. type="link"
  119. icon={<PlusOutlined />}
  120. onClick={() => onClickAdd(record)}
  121. >
  122. 新建
  123. </Button>,
  124. ];
  125. } else {
  126. return [
  127. <Button
  128. key="modifyBtn"
  129. type="link"
  130. icon={<FontAwesomeIcon icon={faPenToSquare} />}
  131. onClick={() => onClickShowRowModifyModal(record)}
  132. >
  133. 修改
  134. </Button>,
  135. <Button
  136. key="newBtn"
  137. type="link"
  138. icon={<PlusOutlined />}
  139. onClick={() => onClickAdd(record)}
  140. >
  141. 新建
  142. </Button>,
  143. <Button
  144. key="deleteBtn"
  145. type="link"
  146. danger
  147. icon={<DeleteOutlined />}
  148. onClick={() => onClickDeleteRow(record)}
  149. >
  150. 删除
  151. </Button>,
  152. ];
  153. }
  154. },
  155. },
  156. ];
  157. //0.查询表格数据
  158. //原始的可展开的所有行的 id
  159. const [defaultExpandKeys, setDefaultExpandKeys] = useState<any[]>([]);
  160. //控制行展开的数据
  161. const [expandKeys, setExpandKeys] = useState<any[]>([]);
  162. const queryTableData = async (params: any, sorter: any, filter: any) => {
  163. const searchParams = {
  164. ...params,
  165. };
  166. const queryParams = new URLSearchParams(searchParams);
  167. Object.keys(sorter).forEach((key) => {
  168. queryParams.append("orderByColumn", key);
  169. if (sorter[key] === "ascend") {
  170. queryParams.append("isAsc", "ascending");
  171. } else {
  172. queryParams.append("isAsc", "descending");
  173. }
  174. });
  175. const body = await fetchApi(`${queryAPI}?${queryParams}`, push);
  176. const root = getRoot(body.data);
  177. if (!root) {
  178. return body.data;
  179. }
  180. getChildren(body.data, root);
  181. const dataArray = [root];
  182. const newExpandedKeys: any[] = [];
  183. const render = (treeDatas: any[]) => {
  184. // 获取到所有可展开的父节点
  185. treeDatas.map((item) => {
  186. if (item.children) {
  187. newExpandedKeys.push(item.deptId);
  188. render(item.children);
  189. }
  190. });
  191. return newExpandedKeys;
  192. };
  193. const keys = render(dataArray);
  194. setDefaultExpandKeys(keys);
  195. setExpandKeys(keys);
  196. return dataArray;
  197. };
  198. const getRoot = (data: any[]) => {
  199. for (let index = 0; index < data.length; index++) {
  200. const item = data[index];
  201. if (item.parentId === 0) {
  202. return item;
  203. }
  204. }
  205. };
  206. const getChildren = (data: any[], parentNode: any) => {
  207. for (let index = 0; index < data.length; index++) {
  208. const item = data[index];
  209. if (item.parentId === parentNode.deptId) {
  210. parentNode.children.push(item);
  211. getChildren(data, item);
  212. }
  213. }
  214. if (parentNode.children.length == 0) {
  215. delete parentNode.children;
  216. }
  217. };
  218. //1.新建
  219. const [showAddModal, setShowAddModal] = useState(false);
  220. //新建表单是否带有父节点id
  221. const [rowParentId, setRowParentId] = useState(100);
  222. //点击新建,如果从行点击新建,给定父组织
  223. const onClickAdd = (record?: any) => {
  224. setRowParentId(record.deptId);
  225. setShowAddModal(true);
  226. };
  227. const cancelAddModal = () => {
  228. setShowAddModal(false);
  229. setRowParentId(100);
  230. };
  231. //确定新建数据
  232. const executeAddData = async (values: any) => {
  233. const body = await fetchApi(newAPI, push, {
  234. method: "POST",
  235. headers: {
  236. "Content-Type": "application/json",
  237. },
  238. body: JSON.stringify(values),
  239. });
  240. if (body != undefined) {
  241. if (body.code == 200) {
  242. globalMessage.success(body.msg);
  243. if (actionTableRef.current) {
  244. actionTableRef.current.reload();
  245. }
  246. setShowAddModal(false);
  247. return true;
  248. }
  249. globalMessage.error(body.msg);
  250. return false;
  251. }
  252. return false;
  253. };
  254. //2.修改
  255. //是否展示修改对话框
  256. const [isShowModifyDataModal, setIsShowModifyDataModal] = useState(false);
  257. //展示修改对话框
  258. const onClickShowRowModifyModal = (record: any) => {
  259. queryRowData(record);
  260. setIsShowModifyDataModal(true);
  261. };
  262. //修改数据表单引用
  263. const modifyFormRef = useRef<ProFormInstance>(null);
  264. //操作当前数据的附加数据
  265. const [operatRowData, setOperateRowData] = useState<{
  266. [key: string]: any;
  267. }>({});
  268. //查询并加载待修改数据的详细信息
  269. const queryRowData = async (record: any) => {
  270. const deptId = record.deptId;
  271. operatRowData["deptId"] = deptId;
  272. operatRowData["ancestors"] = record.ancestors;
  273. setOperateRowData(operatRowData);
  274. if (deptId !== undefined) {
  275. const body = await fetchApi(`${queryDetailAPI}/${deptId}`, push);
  276. if (body !== undefined) {
  277. if (body.code == 200) {
  278. console.log("modi:", modifyFormRef);
  279. modifyFormRef?.current?.setFieldsValue({
  280. //需要加载到修改表单中的数据
  281. parentId: body.data.parentId,
  282. deptName: body.data.deptName,
  283. orderNum: body.data.orderNum,
  284. leader: body.data.leader,
  285. phone: body.data.phone,
  286. email: body.data.email,
  287. status: body.data.status,
  288. });
  289. }
  290. }
  291. }
  292. };
  293. //确认修改数据
  294. const executeModifyData = async (values: any) => {
  295. values["deptId"] = operatRowData["deptId"];
  296. values["ancestors"] = operatRowData["ancestors"];
  297. const body = await fetchApi(modifyAPI, push, {
  298. method: "PUT",
  299. headers: {
  300. "Content-Type": "application/json",
  301. },
  302. body: JSON.stringify(values),
  303. });
  304. if (body !== undefined) {
  305. if (body.code == 200) {
  306. globalMessage.success(body.msg);
  307. //刷新列表
  308. if (actionTableRef.current) {
  309. actionTableRef.current.reload();
  310. }
  311. setIsShowModifyDataModal(false);
  312. return true;
  313. }
  314. globalMessage.error(body.msg);
  315. return false;
  316. }
  317. };
  318. //3.展开/折叠
  319. //点击展开/折叠按钮
  320. const onClickExpandRow = () => {
  321. if (expandKeys.length > 0) {
  322. setExpandKeys([]);
  323. } else {
  324. setExpandKeys(defaultExpandKeys);
  325. }
  326. };
  327. //处理行的展开/折叠逻辑
  328. const handleExpand = (expanded: boolean, record: any) => {
  329. let keys = [...expandKeys];
  330. if (expanded) {
  331. keys.push(record.deptId);
  332. } else {
  333. keys = keys.filter((key: number) => key !== record.deptId);
  334. }
  335. setExpandKeys(keys);
  336. };
  337. //4.导出
  338. //5.选择行
  339. //搜索栏显示状态
  340. const [showSearch, setShowSearch] = useState(true);
  341. //action对象引用
  342. const actionTableRef = useRef<ActionType>(null);
  343. //搜索表单对象引用
  344. const searchTableFormRef = useRef<ProFormInstance>(null!);
  345. const getDeptList = async () => {
  346. const body = await fetchApi(queryAPI, push);
  347. if (body !== undefined) {
  348. const root = getRoot(body.data);
  349. if (!root) {
  350. return body.data;
  351. }
  352. getChildren(body.data, root);
  353. return [root];
  354. }
  355. };
  356. //点击删除按钮
  357. const onClickDeleteRow = (record: any) => {
  358. setDeleteRecord(record);
  359. setDeleteModalVisible(true);
  360. };
  361. //确定删除选中的部门
  362. const executeDeleteRow = async () => {
  363. if (!deleteRecord) return;
  364. const body = await fetchApi(`${deleteAPI}/${deleteRecord.deptId}`, push, {
  365. method: "DELETE",
  366. });
  367. if (body !== undefined) {
  368. if (body.code == 200) {
  369. globalMessage.success("删除成功");
  370. //刷新列表
  371. if (actionTableRef.current) {
  372. actionTableRef.current.reload();
  373. }
  374. } else {
  375. globalMessage.error(body.msg);
  376. }
  377. }
  378. setDeleteModalVisible(false);
  379. setDeleteRecord(null);
  380. };
  381. //取消删除操作
  382. const cancelDeleteRow = () => {
  383. setDeleteModalVisible(false);
  384. setDeleteRecord(null);
  385. };
  386. return (
  387. <PageContainer title={false}>
  388. <ProTable
  389. formRef={searchTableFormRef}
  390. rowKey="deptId"
  391. columns={columns}
  392. expandable={{
  393. expandedRowKeys: expandKeys,
  394. onExpand: handleExpand,
  395. }}
  396. request={async (params: any, sorter: any, filter: any) => {
  397. // 表单搜索项会从 params 传入,传递给后端接口。
  398. const data = await queryTableData(params, sorter, filter);
  399. if (data !== undefined) {
  400. return Promise.resolve({
  401. data: data,
  402. success: true,
  403. total: data.length,
  404. });
  405. }
  406. return Promise.resolve({
  407. data: [],
  408. success: true,
  409. });
  410. }}
  411. pagination={false}
  412. search={
  413. showSearch
  414. ? {
  415. defaultCollapsed: false,
  416. searchText: "搜索",
  417. }
  418. : false
  419. }
  420. dateFormatter="string"
  421. actionRef={actionTableRef}
  422. toolbar={{
  423. actions: [
  424. <ModalForm
  425. key="addmodal"
  426. title="添加部门"
  427. open={showAddModal}
  428. trigger={
  429. <Button icon={<PlusOutlined />} type="primary">
  430. 新建
  431. </Button>
  432. }
  433. autoFocusFirstInput
  434. modalProps={{
  435. destroyOnHidden: true,
  436. onCancel: () => {
  437. cancelAddModal();
  438. },
  439. }}
  440. submitTimeout={2000}
  441. onFinish={executeAddData}
  442. >
  443. <ProForm.Group>
  444. <ProFormTreeSelect
  445. width="md"
  446. name="parentId"
  447. initialValue={rowParentId}
  448. label="上级部门"
  449. placeholder="请选择上级部门"
  450. rules={[{ required: true, message: "请选择上级部门" }]}
  451. request={getDeptList}
  452. fieldProps={{
  453. filterTreeNode: true,
  454. showSearch: true,
  455. treeNodeFilterProp: "label",
  456. fieldNames: {
  457. label: "deptName",
  458. value: "deptId",
  459. },
  460. }}
  461. />
  462. <ProFormText
  463. width="md"
  464. name="deptName"
  465. label="部门名称"
  466. placeholder="请输入部门名称"
  467. rules={[{ required: true, message: "请输入部门名称" }]}
  468. />
  469. </ProForm.Group>
  470. <ProForm.Group>
  471. <ProFormDigit
  472. fieldProps={{ precision: 0 }}
  473. width="md"
  474. name="orderNum"
  475. initialValue="0"
  476. label="排序"
  477. placeholder="请输入排序"
  478. rules={[{ required: true, message: "请输入排序" }]}
  479. />
  480. <ProFormRadio.Group
  481. name="status"
  482. width="sm"
  483. label="状态"
  484. initialValue="0"
  485. options={[
  486. {
  487. label: "正常",
  488. value: "0",
  489. },
  490. {
  491. label: "停用",
  492. value: "1",
  493. },
  494. ]}
  495. />
  496. </ProForm.Group>
  497. <ProForm.Group>
  498. <ProFormText
  499. width="md"
  500. name="leader"
  501. label="负责人"
  502. placeholder="请输入负责人"
  503. />
  504. <ProFormText
  505. width="md"
  506. name="phone"
  507. label="联系电话"
  508. placeholder="请输入联系电话"
  509. rules={[
  510. {
  511. pattern: /^1\d{10}$/,
  512. message: "请输入正确的手机号码",
  513. },
  514. ]}
  515. />
  516. </ProForm.Group>
  517. <ProForm.Group>
  518. <ProFormText
  519. width="md"
  520. name="email"
  521. label="联系邮箱"
  522. placeholder="请输入联系邮箱"
  523. rules={[{ type: "email", message: "请输入正确的邮箱地址" }]}
  524. />
  525. </ProForm.Group>
  526. </ModalForm>,
  527. <Button
  528. key="expand"
  529. icon={<FontAwesomeIcon icon={faArrowsUpDown} />}
  530. onClick={() => onClickExpandRow()}
  531. >
  532. 折叠/展开
  533. </Button>,
  534. ],
  535. settings: [
  536. {
  537. key: "switch",
  538. icon: showSearch ? (
  539. <FontAwesomeIcon icon={faToggleOn} />
  540. ) : (
  541. <FontAwesomeIcon icon={faToggleOff} />
  542. ),
  543. tooltip: showSearch ? "隐藏搜索栏" : "显示搜索栏",
  544. onClick: (key: string | undefined) => {
  545. setShowSearch(!showSearch);
  546. },
  547. },
  548. {
  549. key: "refresh",
  550. tooltip: "刷新",
  551. icon: <ReloadOutlined />,
  552. onClick: (key: string | undefined) => {
  553. if (actionTableRef.current) {
  554. actionTableRef.current.reload();
  555. }
  556. },
  557. },
  558. ],
  559. }}
  560. />
  561. <ModalForm
  562. key="modifymodal"
  563. title="修改部门"
  564. formRef={modifyFormRef}
  565. open={isShowModifyDataModal}
  566. autoFocusFirstInput
  567. modalProps={{
  568. destroyOnHidden: true,
  569. onCancel: () => {
  570. setIsShowModifyDataModal(false);
  571. },
  572. }}
  573. submitTimeout={2000}
  574. onFinish={executeModifyData}
  575. >
  576. <ProForm.Group>
  577. <ProFormTreeSelect
  578. width="md"
  579. name="parentId"
  580. label="上级部门"
  581. placeholder="请选择上级部门"
  582. rules={[{ required: true, message: "请选择上级部门" }]}
  583. request={getDeptList}
  584. fieldProps={{
  585. filterTreeNode: true,
  586. showSearch: true,
  587. treeNodeFilterProp: "label",
  588. fieldNames: {
  589. label: "deptName",
  590. value: "deptId",
  591. },
  592. }}
  593. />
  594. <ProFormText
  595. width="md"
  596. name="deptName"
  597. label="部门名称"
  598. placeholder="请输入部门名称"
  599. rules={[{ required: true, message: "请输入部门名称" }]}
  600. />
  601. </ProForm.Group>
  602. <ProForm.Group>
  603. <ProFormDigit
  604. fieldProps={{ precision: 0 }}
  605. width="md"
  606. name="orderNum"
  607. initialValue="0"
  608. label="排序"
  609. placeholder="请输入排序"
  610. rules={[{ required: true, message: "请输入排序" }]}
  611. />
  612. <ProFormRadio.Group
  613. name="status"
  614. width="sm"
  615. label="状态"
  616. initialValue="0"
  617. options={[
  618. {
  619. label: "正常",
  620. value: "0",
  621. },
  622. {
  623. label: "停用",
  624. value: "1",
  625. },
  626. ]}
  627. />
  628. </ProForm.Group>
  629. <ProForm.Group>
  630. <ProFormText
  631. width="md"
  632. name="leader"
  633. label="负责人"
  634. placeholder="请输入负责人"
  635. />
  636. <ProFormText
  637. width="md"
  638. name="phone"
  639. label="联系电话"
  640. placeholder="请输入联系电话"
  641. rules={[
  642. {
  643. pattern: /^1\d{10}$/,
  644. message: "请输入正确的手机号码",
  645. },
  646. ]}
  647. />
  648. </ProForm.Group>
  649. <ProForm.Group>
  650. <ProFormText
  651. width="md"
  652. name="email"
  653. label="联系邮箱"
  654. placeholder="请输入联系邮箱"
  655. rules={[{ type: "email", message: "请输入正确的邮箱地址" }]}
  656. />
  657. </ProForm.Group>
  658. </ModalForm>
  659. {/* 删除确认模态框 */}
  660. <Modal
  661. title={
  662. <div style={{ display: 'flex', alignItems: 'center' }}>
  663. <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
  664. <span>系统提示</span>
  665. </div>
  666. }
  667. open={deleteModalVisible}
  668. onOk={executeDeleteRow}
  669. onCancel={cancelDeleteRow}
  670. okText="确认"
  671. cancelText="取消"
  672. >
  673. <p>{`确定删除部门名称为“${deleteRecord?.deptName}”的数据项?`}</p>
  674. </Modal>
  675. </PageContainer>
  676. );
  677. }