page.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. "use client";
  2. import {fetchApi, fetchFile} 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. ProFormTextArea,
  13. ProTable,
  14. } from "@ant-design/pro-components";
  15. import {Button, Modal, Space, Tag} from "antd";
  16. import {useRouter} from "next/navigation";
  17. import {faCheck, faDownload, faPenToSquare, faToggleOff, faToggleOn, faXmark,} from "@fortawesome/free-solid-svg-icons";
  18. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  19. import React, {useRef, useState} from "react";
  20. //查询表格数据API
  21. const queryAPI = "/api/system/post/list";
  22. //新建数据API
  23. const newAPI = "/api/system/post";
  24. //修改数据API
  25. const modifyAPI = "/api/system/post";
  26. //查询详情数据API
  27. const queryDetailAPI = "/api/system/post";
  28. //删除API
  29. const deleteAPI = "/api/system/post";
  30. //导出API
  31. const exportAPI = "/api/system/post/export";
  32. //导出文件前缀名
  33. const exportFilePrefix = "post";
  34. export default function Post() {
  35. const { push } = useRouter();
  36. // 添加用于控制删除确认模态框的状态
  37. const [deleteModalVisible, setDeleteModalVisible] = useState(false);
  38. const [deletePostId, setDeletePostId] = useState<string | number | null>(null);
  39. //表格列定义
  40. const columns: ProColumns[] = [
  41. {
  42. title: "岗位编号",
  43. dataIndex: "postId",
  44. search: false,
  45. },
  46. {
  47. title: "岗位编码",
  48. fieldProps: {
  49. placeholder: "请输入岗位编码",
  50. },
  51. dataIndex: "postCode",
  52. ellipsis: true,
  53. order: 3,
  54. },
  55. {
  56. title: "岗位名称",
  57. fieldProps: {
  58. placeholder: "请输入岗位名称",
  59. },
  60. dataIndex: "postName",
  61. ellipsis: true,
  62. sorter: true,
  63. order: 2,
  64. },
  65. {
  66. title: "岗位排序",
  67. dataIndex: "postSort",
  68. search: false,
  69. sorter: true,
  70. },
  71. {
  72. title: "状态",
  73. fieldProps: {
  74. placeholder: "请选择岗位状态",
  75. },
  76. dataIndex: "status",
  77. valueType: "select",
  78. render: (_, record) => {
  79. return (
  80. <Space>
  81. <Tag
  82. color={record.status === "0" ? "green" : "red"}
  83. icon={
  84. record.status == 0 ? (
  85. <FontAwesomeIcon icon={faCheck} />
  86. ) : (
  87. <FontAwesomeIcon icon={faXmark} />
  88. )
  89. }
  90. >
  91. {_}
  92. </Tag>
  93. </Space>
  94. );
  95. },
  96. valueEnum: {
  97. 0: {
  98. text: "正常",
  99. status: "0",
  100. },
  101. 1: {
  102. text: "停用",
  103. status: "1",
  104. },
  105. },
  106. order: 1,
  107. },
  108. {
  109. title: "创建时间",
  110. dataIndex: "createTime",
  111. valueType: "dateTime",
  112. sorter: true,
  113. search: false,
  114. },
  115. {
  116. title: "操作",
  117. key: "option",
  118. search: false,
  119. render: (_, record) => [
  120. <Button
  121. key="modifyBtn"
  122. type="link"
  123. icon={<FontAwesomeIcon icon={faPenToSquare} />}
  124. onClick={() => onClickShowRowModifyModal(record)}
  125. >
  126. 修改
  127. </Button>,
  128. <Button
  129. key="deleteBtn"
  130. type="link"
  131. danger
  132. icon={<DeleteOutlined />}
  133. onClick={() => onClickDeleteRow(record)}
  134. >
  135. 删除
  136. </Button>,
  137. ],
  138. },
  139. ];
  140. //0.查询表格数据
  141. const queryTableData = async (params: any, sorter: any, filter: any) => {
  142. const searchParams = {
  143. pageNum: params.current,
  144. ...params,
  145. };
  146. delete searchParams.current;
  147. const queryParams = new URLSearchParams(searchParams);
  148. Object.keys(sorter).forEach((key) => {
  149. queryParams.append("orderByColumn", key);
  150. if (sorter[key] === "ascend") {
  151. queryParams.append("isAsc", "ascending");
  152. } else {
  153. queryParams.append("isAsc", "descending");
  154. }
  155. });
  156. const body = await fetchApi(`${queryAPI}?${queryParams}`, push);
  157. return body;
  158. };
  159. //1.新建
  160. //确定新建数据
  161. const executeAddData = async (values: any) => {
  162. const body = await fetchApi(newAPI, push, {
  163. method: "POST",
  164. headers: {
  165. "Content-Type": "application/json",
  166. },
  167. body: JSON.stringify(values),
  168. });
  169. if (body != undefined) {
  170. if (body.code == 200) {
  171. App.useApp().message.success(body.msg);
  172. if (actionTableRef.current) {
  173. actionTableRef.current.reload();
  174. }
  175. return true;
  176. }
  177. App.useApp().message.error(body.msg);
  178. return false;
  179. }
  180. return false;
  181. };
  182. //2.修改
  183. //是否展示修改对话框
  184. const [isShowModifyDataModal, setIsShowModifyDataModal] = useState(false);
  185. //展示修改对话框
  186. const onClickShowRowModifyModal = (record?: any) => {
  187. queryRowData(record);
  188. setIsShowModifyDataModal(true);
  189. };
  190. //修改数据表单引用
  191. const modifyFormRef = useRef<ProFormInstance>(null);
  192. //操作当前数据的附加数据
  193. const [operatRowData, setOperateRowData] = useState<{
  194. [key: string]: any;
  195. }>({});
  196. //查询并加载待修改数据的详细信息
  197. const queryRowData = async (record?: any) => {
  198. const postId = record !== undefined ? record.postId : selectedRow.postId;
  199. operatRowData["postId"] = postId;
  200. setOperateRowData(operatRowData);
  201. if (postId !== undefined) {
  202. const body = await fetchApi(`${queryDetailAPI}/${postId}`, push);
  203. if (body !== undefined) {
  204. if (body.code == 200) {
  205. modifyFormRef?.current?.setFieldsValue({
  206. //需要加载到修改表单中的数据
  207. postName: body.data.postName,
  208. postCode: body.data.postCode,
  209. postSort: body.data.postSort,
  210. status: body.data.status,
  211. remark: body.data.remark,
  212. });
  213. }
  214. }
  215. }
  216. };
  217. //确认修改数据
  218. const executeModifyData = async (values: any) => {
  219. values["postId"] = operatRowData["postId"];
  220. const body = await fetchApi(modifyAPI, push, {
  221. method: "PUT",
  222. headers: {
  223. "Content-Type": "application/json",
  224. },
  225. body: JSON.stringify(values),
  226. });
  227. if (body !== undefined) {
  228. if (body.code == 200) {
  229. App.useApp().message.success(body.msg);
  230. //刷新列表
  231. if (actionTableRef.current) {
  232. actionTableRef.current.reload();
  233. }
  234. setIsShowModifyDataModal(false);
  235. return true;
  236. }
  237. App.useApp().message.error(body.msg);
  238. return false;
  239. }
  240. };
  241. //3.删除
  242. //点击删除按钮,展示删除确认框
  243. const onClickDeleteRow = (record?: any) => {
  244. const postId = record !== undefined ? record.postId : selectedRowKeys.join(",");
  245. setDeletePostId(postId);
  246. setDeleteModalVisible(true);
  247. };
  248. //确定删除选中的数据
  249. const executeDeleteRow = async () => {
  250. if (deletePostId === null) return;
  251. const body = await fetchApi(`${deleteAPI}/${deletePostId}`, push, {
  252. method: "DELETE",
  253. });
  254. if (body !== undefined) {
  255. if (body.code == 200) {
  256. App.useApp().message.success("删除成功");
  257. //修改按钮变回不可点击
  258. setRowCanModify(false);
  259. //删除按钮变回不可点击
  260. setRowCanDelete(false);
  261. //选中行数据重置为空
  262. setSelectedRowKeys([]);
  263. //刷新列表
  264. if (actionTableRef.current) {
  265. actionTableRef.current.reload();
  266. }
  267. } else {
  268. App.useApp().message.error(body.msg);
  269. }
  270. }
  271. setDeleteModalVisible(false);
  272. setDeletePostId(null);
  273. };
  274. //4.导出
  275. //导出表格数据
  276. const exportTable = async () => {
  277. if (searchTableFormRef.current) {
  278. const formData = new FormData();
  279. const data = {
  280. pageNum: page,
  281. pageSize: pageSize,
  282. ...searchTableFormRef.current.getFieldsValue(),
  283. };
  284. Object.keys(data).forEach((key) => {
  285. if (data[key] !== undefined) {
  286. formData.append(key, data[key]);
  287. }
  288. });
  289. await fetchFile(
  290. exportAPI,
  291. push,
  292. {
  293. method: "POST",
  294. body: formData,
  295. },
  296. `${exportFilePrefix}_${new Date().getTime()}.xlsx`
  297. );
  298. }
  299. };
  300. //5.选择行
  301. //选中行操作
  302. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  303. const [selectedRow, setSelectedRow] = useState(undefined as any);
  304. //修改按钮是否可用,选中行时才可用
  305. const [rowCanModify, setRowCanModify] = useState(false);
  306. //删除按钮是否可用,选中行时才可用
  307. const [rowCanDelete, setRowCanDelete] = useState(false);
  308. //ProTable rowSelection
  309. const rowSelection = {
  310. onChange: (newSelectedRowKeys: React.Key[], selectedRows: any[]) => {
  311. setSelectedRowKeys(newSelectedRowKeys);
  312. setRowCanDelete(newSelectedRowKeys && newSelectedRowKeys.length > 0);
  313. if (newSelectedRowKeys && newSelectedRowKeys.length == 1) {
  314. setSelectedRow(selectedRows[0]);
  315. setRowCanModify(true);
  316. } else {
  317. setRowCanModify(false);
  318. setSelectedRow(undefined);
  319. }
  320. },
  321. //复选框的额外禁用判断
  322. // getCheckboxProps: (record) => ({
  323. // disabled: record.userId == 1,
  324. // }),
  325. };
  326. //搜索栏显示状态
  327. const [showSearch, setShowSearch] = useState(true);
  328. //action对象引用
  329. const actionTableRef = useRef<ActionType>(null);
  330. //搜索表单对象引用
  331. const searchTableFormRef = useRef<ProFormInstance>(null!);
  332. //当前页数和每页条数
  333. const [page, setPage] = useState(1);
  334. const defaultPageSize = 10;
  335. const [pageSize, setPageSize] = useState(defaultPageSize);
  336. const pageChange = (page: number, pageSize: number) => {
  337. setPage(page);
  338. setPageSize(pageSize);
  339. };
  340. return (
  341. <PageContainer title={false}>
  342. <ProTable
  343. formRef={searchTableFormRef}
  344. rowKey="postId"
  345. rowSelection={{
  346. selectedRowKeys,
  347. ...rowSelection,
  348. }}
  349. columns={columns}
  350. request={async (params: any, sorter: any, filter: any) => {
  351. // 表单搜索项会从 params 传入,传递给后端接口。
  352. const data = await queryTableData(params, sorter, filter);
  353. if (data !== undefined) {
  354. return Promise.resolve({
  355. data: data.rows,
  356. success: true,
  357. total: data.total,
  358. });
  359. }
  360. return Promise.resolve({
  361. data: [],
  362. success: true,
  363. });
  364. }}
  365. pagination={{
  366. defaultPageSize: defaultPageSize,
  367. showQuickJumper: true,
  368. showSizeChanger: true,
  369. onChange: pageChange,
  370. }}
  371. search={
  372. showSearch
  373. ? {
  374. defaultCollapsed: false,
  375. searchText: "搜索",
  376. }
  377. : false
  378. }
  379. dateFormatter="string"
  380. actionRef={actionTableRef}
  381. toolbar={{
  382. actions: [
  383. <ModalForm
  384. key="addmodal"
  385. title="添加岗位"
  386. trigger={
  387. <Button icon={<PlusOutlined />} type="primary">
  388. 新建
  389. </Button>
  390. }
  391. autoFocusFirstInput
  392. modalProps={{
  393. destroyOnHidden: true,
  394. }}
  395. submitTimeout={2000}
  396. onFinish={executeAddData}
  397. >
  398. <ProForm.Group>
  399. <ProFormText
  400. width="md"
  401. name="postName"
  402. label="岗位名称"
  403. placeholder="请输入岗位名称"
  404. rules={[{ required: true, message: "请输入岗位名称" }]}
  405. />
  406. <ProFormText
  407. width="md"
  408. name="postCode"
  409. label="岗位编码"
  410. placeholder="请输入岗位编码"
  411. rules={[{ required: true, message: "请输入岗位编码" }]}
  412. />
  413. </ProForm.Group>
  414. <ProForm.Group>
  415. <ProFormDigit
  416. fieldProps={{ precision: 0 }}
  417. width="md"
  418. name="postSort"
  419. initialValue="0"
  420. label="岗位排序"
  421. placeholder="请输入岗位排序"
  422. rules={[{ required: true, message: "请输入岗位排序" }]}
  423. />
  424. <ProFormRadio.Group
  425. name="status"
  426. width="sm"
  427. label="状态"
  428. initialValue="0"
  429. options={[
  430. {
  431. label: "正常",
  432. value: "0",
  433. },
  434. {
  435. label: "停用",
  436. value: "1",
  437. },
  438. ]}
  439. />
  440. </ProForm.Group>
  441. <ProFormTextArea
  442. name="remark"
  443. width={688}
  444. label="备注"
  445. placeholder="请输入内容"
  446. />
  447. </ModalForm>,
  448. <ModalForm
  449. key="modifymodal"
  450. title="修改岗位"
  451. formRef={modifyFormRef}
  452. trigger={
  453. <Button
  454. icon={<FontAwesomeIcon icon={faPenToSquare} />}
  455. disabled={!rowCanModify}
  456. onClick={() => onClickShowRowModifyModal()}
  457. >
  458. 修改
  459. </Button>
  460. }
  461. open={isShowModifyDataModal}
  462. autoFocusFirstInput
  463. modalProps={{
  464. destroyOnHidden: true,
  465. onCancel: () => {
  466. setIsShowModifyDataModal(false);
  467. },
  468. }}
  469. submitTimeout={2000}
  470. onFinish={executeModifyData}
  471. >
  472. <ProForm.Group>
  473. <ProFormText
  474. width="md"
  475. name="postName"
  476. label="岗位名称"
  477. placeholder="请输入岗位名称"
  478. rules={[{ required: true, message: "请输入岗位名称" }]}
  479. />
  480. <ProFormText
  481. width="md"
  482. name="postCode"
  483. label="岗位编码"
  484. placeholder="请输入岗位编码"
  485. rules={[{ required: true, message: "请输入岗位编码" }]}
  486. />
  487. </ProForm.Group>
  488. <ProForm.Group>
  489. <ProFormDigit
  490. fieldProps={{ precision: 0 }}
  491. width="md"
  492. name="postSort"
  493. initialValue="0"
  494. label="岗位排序"
  495. placeholder="请输入岗位排序"
  496. rules={[{ required: true, message: "请输入岗位排序" }]}
  497. />
  498. <ProFormRadio.Group
  499. name="status"
  500. width="sm"
  501. label="状态"
  502. initialValue="0"
  503. options={[
  504. {
  505. label: "正常",
  506. value: "0",
  507. },
  508. {
  509. label: "停用",
  510. value: "1",
  511. },
  512. ]}
  513. />
  514. </ProForm.Group>
  515. <ProFormTextArea
  516. name="remark"
  517. width={688}
  518. label="备注"
  519. placeholder="请输入内容"
  520. />
  521. </ModalForm>,
  522. <Button
  523. key="danger"
  524. danger
  525. icon={<DeleteOutlined />}
  526. disabled={!rowCanDelete}
  527. onClick={() => onClickDeleteRow()}
  528. >
  529. 删除
  530. </Button>,
  531. <Button
  532. key="export"
  533. type="primary"
  534. icon={<FontAwesomeIcon icon={faDownload} />}
  535. onClick={exportTable}
  536. >
  537. 导出
  538. </Button>,
  539. ],
  540. settings: [
  541. {
  542. key: "switch",
  543. icon: showSearch ? (
  544. <FontAwesomeIcon icon={faToggleOn} />
  545. ) : (
  546. <FontAwesomeIcon icon={faToggleOff} />
  547. ),
  548. tooltip: showSearch ? "隐藏搜索栏" : "显示搜索栏",
  549. onClick: (key: string | undefined) => {
  550. setShowSearch(!showSearch);
  551. },
  552. },
  553. {
  554. key: "refresh",
  555. tooltip: "刷新",
  556. icon: <ReloadOutlined />,
  557. onClick: (key: string | undefined) => {
  558. if (actionTableRef.current) {
  559. actionTableRef.current.reload();
  560. }
  561. },
  562. },
  563. ],
  564. }}
  565. />
  566. {/* 删除确认模态框 */}
  567. <Modal
  568. title={
  569. <div style={{ display: 'flex', alignItems: 'center' }}>
  570. <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
  571. <span>系统提示</span>
  572. </div>
  573. }
  574. open={deleteModalVisible}
  575. onOk={executeDeleteRow}
  576. onCancel={() => {
  577. setDeleteModalVisible(false);
  578. setDeletePostId(null);
  579. }}
  580. okText="确认"
  581. cancelText="取消"
  582. >
  583. <p>{`确定删除岗位编号为“${deletePostId}”的数据项?`}</p>
  584. </Modal>
  585. </PageContainer>
  586. );
  587. }