index.tsx 8.2 KB

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