page.tsx 18 KB

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