index.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import BreadTit from "@/components/BreadTit";
  2. import classNames from "classnames";
  3. import { useEffect, useMemo, useRef, useState } from "react";
  4. import styles from "./index.module.scss";
  5. import {
  6. Select,
  7. Input,
  8. DatePicker,
  9. Table,
  10. Button,
  11. Popconfirm,
  12. } from "antd";
  13. import AuthButton from "@/components/AuthButton";
  14. import history from "@/utils/history";
  15. import { useLocation } from "react-router-dom";
  16. import { useDispatch, useSelector } from "react-redux";
  17. import { RootState } from "@/store";
  18. import {
  19. getObject1List,
  20. getObject1ListNum,
  21. object1DelAPI,
  22. } from "@/store/action/object1";
  23. import { MessageFu } from "@/utils/message";
  24. const { RangePicker } = DatePicker;
  25. export default function Object1() {
  26. const dispatch = useDispatch();
  27. // 获取顶部数量
  28. useEffect(() => {
  29. dispatch(getObject1ListNum());
  30. }, [dispatch]);
  31. // 顶部的状态改变了,统一管理,传到二级页码
  32. const statusRef = useRef<null | number>(null);
  33. const dataTit = useSelector(
  34. (state: RootState) => state.object1Store.infoNum1
  35. );
  36. // 封装发送请求的函数
  37. const getList = () => {
  38. const data = {
  39. ...tableSelect,
  40. pageNum: pageNumRef.current,
  41. status: statusRef.current,
  42. };
  43. dispatch(getObject1List(data));
  44. };
  45. // 获取地址栏参数
  46. const location = useLocation();
  47. const pageNumRef = useRef(1);
  48. // 如果有参数 根据参数页码在次发送请求
  49. useEffect(() => {
  50. const urlParam = location.state || {};
  51. setTableSelect({
  52. ...tableSelect,
  53. pageNum: Number(urlParam.k) ? Number(urlParam.k) : 1,
  54. // eslint-disable-next-line eqeqeq
  55. status: urlParam.d && urlParam.d != "null" ? Number(urlParam.d) : null,
  56. });
  57. // eslint-disable-next-line react-hooks/exhaustive-deps
  58. }, [location]);
  59. // 顶部筛选
  60. const [tableSelect, setTableSelect] = useState({
  61. status: null as null | number,
  62. sourceName: null,
  63. searchKey: "",
  64. startTime: "",
  65. endTime: "",
  66. pageSize: 10,
  67. pageNum: 1,
  68. });
  69. // 当前页码统一
  70. useEffect(() => {
  71. pageNumRef.current = tableSelect.pageNum;
  72. }, [tableSelect.pageNum]);
  73. // 顶部状态统一
  74. useEffect(() => {
  75. statusRef.current = tableSelect.status;
  76. }, [tableSelect.status]);
  77. // 防止返回的时候发送了2次请求来对应页码
  78. const getListRef = useRef(-1);
  79. useEffect(() => {
  80. clearTimeout(getListRef.current);
  81. getListRef.current = window.setTimeout(() => {
  82. getList();
  83. }, 100);
  84. // eslint-disable-next-line react-hooks/exhaustive-deps
  85. }, [tableSelect]);
  86. // 藏品来源下拉框筛选
  87. const handleChange = (value: any) => {
  88. setTableSelect({ ...tableSelect, sourceName: value, pageNum: 1 });
  89. };
  90. // 登记人员输入
  91. const nameTime = useRef(-1);
  92. const nameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  93. clearTimeout(nameTime.current);
  94. nameTime.current = window.setTimeout(() => {
  95. setTableSelect({ ...tableSelect, searchKey: e.target.value, pageNum: 1 });
  96. }, 500);
  97. };
  98. // 时间选择器改变
  99. const timeChange = (date: any, dateString: any) => {
  100. let startTime = "";
  101. let endTime = "";
  102. if (dateString[0] && dateString[1]) {
  103. startTime = dateString[0] + " 00:00:00";
  104. endTime = dateString[1] + " 23:59:59";
  105. }
  106. setTableSelect({ ...tableSelect, startTime, endTime, pageNum: 1 });
  107. };
  108. // 点击新增或者编辑按钮
  109. const addObject = (id?: any) => {
  110. // 清空详情页面里面的附件表格信息
  111. dispatch({ type: "login/setGoodsSonList", payload: [] });
  112. if (id)
  113. history.push(
  114. `/object/1/add?k=${pageNumRef.current}&d=${statusRef.current}&id=${id}`
  115. );
  116. else
  117. history.push(
  118. `/object/1/add?k=${pageNumRef.current}&d=${statusRef.current}`
  119. );
  120. };
  121. // 点击删除按钮
  122. const delOne = async (id: number) => {
  123. const res: any = await object1DelAPI(id);
  124. if (res.code === 0) {
  125. MessageFu.success("删除成功!");
  126. getList();
  127. dispatch(getObject1ListNum());
  128. }
  129. };
  130. // ---------关于表格
  131. // 页码变化
  132. const paginationChange = (pageNum: number, pageSize: number) => {
  133. setTableSelect({ ...tableSelect, pageNum, pageSize });
  134. };
  135. const results = useSelector((state: RootState) => state.object1Store.info1);
  136. const columns = useMemo(() => {
  137. return [
  138. {
  139. title: "登记编号",
  140. dataIndex: "num",
  141. },
  142. {
  143. title: "藏品来源",
  144. dataIndex: "sourceName",
  145. },
  146. {
  147. title: "登记人员",
  148. dataIndex: "creatorName",
  149. },
  150. {
  151. title: "创建日期",
  152. dataIndex: "createTime",
  153. },
  154. {
  155. title: "完成日期",
  156. render: (item: any) => (item.day && item.status === 3 ? item.day : "-"),
  157. },
  158. {
  159. title: "状态",
  160. dataIndex: "statusTxt",
  161. },
  162. {
  163. title: "操作",
  164. render: (item: any) => (
  165. <>
  166. <Button
  167. type="text"
  168. danger
  169. onClick={() =>
  170. history.push(
  171. `/object/1/look?k=${pageNumRef.current}&d=${statusRef.current}&id=${item.id}`
  172. )
  173. }
  174. >
  175. 查看
  176. </Button>
  177. {item.status === 0 || item.status === 2 ? (
  178. <AuthButton
  179. id={102}
  180. type="text"
  181. danger
  182. onClick={() => addObject(item.id)}
  183. >
  184. 编辑
  185. </AuthButton>
  186. ) : null}
  187. {item.status === 1 ? (
  188. <AuthButton
  189. id={105}
  190. onClick={() =>
  191. history.push(
  192. `/object/1/audit?k=${pageNumRef.current}&d=${statusRef.current}&id=${item.id}`
  193. )
  194. }
  195. type="text"
  196. danger
  197. >
  198. 审核
  199. </AuthButton>
  200. ) : null}
  201. {item.status === 0 || item.status === 2 ? (
  202. <Popconfirm
  203. title="确定删除吗?"
  204. okText="确定"
  205. cancelText="取消"
  206. onConfirm={() => delOne(item.id)}
  207. >
  208. <AuthButton id={103} type="text" danger>
  209. 删除
  210. </AuthButton>
  211. </Popconfirm>
  212. ) : null}
  213. </>
  214. ),
  215. },
  216. ];
  217. // eslint-disable-next-line react-hooks/exhaustive-deps
  218. }, []);
  219. // 从仓库中获取藏品来源下拉数据
  220. const options = useSelector(
  221. (state: RootState) => state.loginStore.selectAll["文物来源"]
  222. );
  223. return (
  224. <div className={styles.Object1}>
  225. <div className="breadTit">
  226. <BreadTit>
  227. <div className="breadTitRow active">藏品登记</div>
  228. </BreadTit>
  229. </div>
  230. <div className="objectSonMain">
  231. {/* 顶部筛选 */}
  232. <div className="objectSonMainTit">
  233. {dataTit.map((v: any) => (
  234. <div
  235. key={v.id}
  236. onClick={() =>
  237. setTableSelect({ ...tableSelect, status: v.id, pageNum: 1 })
  238. }
  239. className={classNames(
  240. v.id === tableSelect.status ? "active" : ""
  241. )}
  242. >
  243. {v.name}({v.num})
  244. </div>
  245. ))}
  246. </div>
  247. <div className="objectSonMainTable">
  248. {/* 表格数据筛选 */}
  249. <div className="tableSelectBox">
  250. <div className="row">
  251. <span>藏品来源:</span>
  252. <Select
  253. placeholder="请选择"
  254. allowClear
  255. style={{ width: 150 }}
  256. value={tableSelect.sourceName}
  257. onChange={handleChange}
  258. options={options.map((v: any) => ({
  259. label: v.name,
  260. value: v.name,
  261. }))}
  262. />
  263. </div>
  264. <div className="row">
  265. <span>登记人员:</span>
  266. <Input
  267. maxLength={10}
  268. style={{ width: 150 }}
  269. placeholder="请输入"
  270. allowClear
  271. onChange={(e) => nameChange(e)}
  272. />
  273. </div>
  274. <div className="row">
  275. <span>创建日期:</span>
  276. <RangePicker onChange={timeChange} />
  277. </div>
  278. <div className="row">
  279. <AuthButton id={102} type="primary" onClick={() => addObject()}>
  280. 新增
  281. </AuthButton>
  282. </div>
  283. </div>
  284. {/* 表格主体 */}
  285. <div className="tableMain">
  286. <Table
  287. scroll={{ y: 428 }}
  288. dataSource={results.list}
  289. columns={columns}
  290. rowKey="id"
  291. pagination={{
  292. showQuickJumper: true,
  293. position: ["bottomCenter"],
  294. showSizeChanger: true,
  295. current: tableSelect.pageNum,
  296. pageSize: tableSelect.pageSize,
  297. total: results.total,
  298. onChange: paginationChange,
  299. }}
  300. />
  301. </div>
  302. </div>
  303. </div>
  304. </div>
  305. );
  306. }