index.tsx 8.6 KB

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