index.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import BreadTit from "@/components/BreadTit";
  2. import history, { urlParameter } from "@/utils/history";
  3. import { Input, Select, Table, Popconfirm, Button } from "antd";
  4. import TextArea from "antd/es/input/TextArea";
  5. import React, {
  6. useCallback,
  7. useEffect,
  8. useMemo,
  9. useRef,
  10. useState,
  11. } from "react";
  12. import { useLocation } from "react-router-dom";
  13. import styles from "./index.module.scss";
  14. import ObjectAdd from "@/components/ObjectAdd";
  15. import ImageLazy from "@/components/ImageLazy/index";
  16. import { useDispatch, useSelector } from "react-redux";
  17. import { RootState } from "@/store";
  18. import {
  19. delInTablesAPI,
  20. getObj1InfoTableAPI,
  21. object1AddAPI,
  22. object1infoOutAPI,
  23. } from "@/store/action/object1";
  24. import _ from "lodash";
  25. import { MessageFu } from "@/utils/message";
  26. function AddObject1() {
  27. const dispatch = useDispatch();
  28. // 从仓库中获取藏品来源下拉数据
  29. const options = useSelector(
  30. (state: RootState) => state.loginStore.selectAll["文物来源"]
  31. );
  32. // 顶部数据
  33. const [addInfoTop, setAddInfoTop] = useState<any>({});
  34. // 进入页面新增请求函数
  35. const object1AddAPIFu = useCallback(async () => {
  36. const res = await object1AddAPI();
  37. setAddInfoTop({
  38. ...res.data,
  39. sourceName:null
  40. // sourceName: options[0].name ? options[0].name : "",
  41. });
  42. }, []);
  43. // 通过id获取详情函数
  44. const object1infoOutAPIFu = useCallback(
  45. async (id: number) => {
  46. const res = await object1infoOutAPI(id);
  47. setAddInfoTop(res.data);
  48. // 获取表格详情信息
  49. dispatch(getObj1InfoTableAPI(id));
  50. },
  51. [dispatch]
  52. );
  53. // 获取地址栏参数
  54. const location = useLocation();
  55. const [urlParam, setUrlParam] = useState<any>({});
  56. useEffect(() => {
  57. const obj = urlParameter(location.search);
  58. setUrlParam(obj);
  59. if (obj.id) {
  60. // 如果是编辑
  61. object1infoOutAPIFu(obj.id);
  62. } else object1AddAPIFu();
  63. }, [location, object1AddAPIFu, object1infoOutAPIFu]);
  64. // 藏品来源下拉框
  65. const handleChange = (value: string) => {
  66. setAddInfoTop({ ...addInfoTop, sourceName: value });
  67. };
  68. // 表格数据
  69. // 选中的表格数据
  70. const [tableSelectList, setTableSelectList] = useState([]);
  71. // 从仓库拿表格信息
  72. const results = useSelector(
  73. (state: RootState) => state.loginStore.goodsTableList
  74. );
  75. // 前端删除成功的id集合,用于点击存入草稿或者提交成功之后 传入后端删除
  76. const delIds = useRef<any>([]);
  77. // 点击删除
  78. const delTableListFu = useCallback(() => {
  79. console.log("多个删除", tableSelectList);
  80. const data = _.differenceBy(results, tableSelectList, "id");
  81. dispatch({ type: "login/setGoodsSonList", payload: data });
  82. setTableSelectList(data);
  83. // 删除的id存起来
  84. tableSelectList.forEach((v: any) => {
  85. delIds.current.push(v.id);
  86. });
  87. }, [dispatch, results, tableSelectList]);
  88. const delOne = useCallback(
  89. (id: number) => {
  90. const data = results.filter((v: any) => v.id !== id);
  91. dispatch({ type: "login/setGoodsSonList", payload: data });
  92. console.log("单个删除", id);
  93. // 删除的id存起来
  94. delIds.current.push(id);
  95. },
  96. [dispatch, results]
  97. );
  98. const rowSelection = {
  99. onChange: (selectedRowKeys: any, selectedRows: any) => {
  100. setTableSelectList(selectedRows);
  101. },
  102. };
  103. // 点击添加或者编辑
  104. const addId = useRef<any>(null);
  105. const addPageFu = useCallback((id?: any) => {
  106. addId.current = id;
  107. setAddPage(true);
  108. }, []);
  109. const columns = useMemo(() => {
  110. return [
  111. {
  112. title: "缩略图",
  113. render: (item: any) => (
  114. <ImageLazy width={120} height={70} src={item.thumb} />
  115. ),
  116. },
  117. {
  118. title: "藏品编号名称",
  119. dataIndex: "dictNum",
  120. },
  121. {
  122. title: "藏品编号",
  123. render: (item: any) => (item.num ? item.num : "-"),
  124. },
  125. {
  126. title: "藏品名称",
  127. dataIndex: "name",
  128. },
  129. {
  130. title: "类别",
  131. dataIndex: "dictGoodType",
  132. },
  133. {
  134. title: "完残程度",
  135. dataIndex: "complete",
  136. },
  137. {
  138. title: "年代",
  139. dataIndex: "dictAge",
  140. },
  141. {
  142. title: "操作",
  143. render: (item: any) => (
  144. <>
  145. <Button type="text" danger onClick={() => addPageFu(item.id)}>
  146. 编辑
  147. </Button>
  148. <Popconfirm
  149. title="确定删除吗?"
  150. okText="确定"
  151. cancelText="取消"
  152. onConfirm={() => delOne(item.id)}
  153. >
  154. <Button type="text" danger>
  155. 删除
  156. </Button>
  157. </Popconfirm>
  158. </>
  159. ),
  160. },
  161. ];
  162. }, [addPageFu, delOne]);
  163. // 点击返回
  164. const cancelFu = useCallback(() => {
  165. history.push({
  166. pathname: `/object`,
  167. state: { k: urlParam.k ? urlParam.k : "1", d: urlParam.d },
  168. });
  169. }, [urlParam.d, urlParam.k]);
  170. // 点击提交
  171. const submitFu = useCallback(
  172. async (val: number) => {
  173. if (!addInfoTop.sourceName) return MessageFu.warning("请选择藏品来源!");
  174. if (results.length === 0)
  175. return MessageFu.warning("至少需要添加一条藏品信息!");
  176. const obj = {
  177. ...addInfoTop,
  178. status: val,
  179. };
  180. const res: any = await object1AddAPI(obj);
  181. if (res.code === 0) {
  182. // 真正进行删除
  183. if (delIds.current.length) {
  184. const res2: any = await delInTablesAPI(delIds.current.join(","));
  185. if (res2.code === 0) {
  186. MessageFu.success("操作成功!");
  187. cancelFu();
  188. }
  189. } else {
  190. MessageFu.success("操作成功!");
  191. cancelFu();
  192. }
  193. }
  194. },
  195. [addInfoTop, cancelFu, results.length]
  196. );
  197. // 点击添加或者编辑出来页面
  198. const [addPage, setAddPage] = useState(false);
  199. return (
  200. <div className={styles.AddObject1}>
  201. <div className="breadTit">
  202. <BreadTit>
  203. <div className="breadTitRow">藏品登记</div>
  204. <div className="splitStr">/</div>
  205. <div className="breadTitRow active">
  206. {urlParam.id ? "编辑" : "新增"}
  207. </div>
  208. </BreadTit>
  209. </div>
  210. <div className="objectSonMain">
  211. {/* 上面的信息展示 */}
  212. <div className="addInfoTop">
  213. <div className="row">
  214. <div>
  215. <span className="bs">登记编号:</span>
  216. <Input style={{ width: 400 }} value={addInfoTop.num} disabled />
  217. </div>
  218. <div>
  219. <span className="bs">登记人员:</span>
  220. <Input
  221. style={{ width: 400 }}
  222. value={addInfoTop.creatorName}
  223. disabled
  224. />
  225. </div>
  226. </div>
  227. <div className="row">
  228. <div>
  229. <span className="bs">藏品来源:</span>
  230. <Select
  231. placeholder="请选择"
  232. style={{ width: 400 }}
  233. value={addInfoTop.sourceName}
  234. onChange={handleChange}
  235. options={options.map((v: any) => ({
  236. label: v.name,
  237. value: v.name,
  238. }))}
  239. />
  240. </div>
  241. </div>
  242. <div className="rowAll">
  243. <span>登记说明:</span>
  244. <TextArea
  245. value={addInfoTop.description}
  246. onChange={(e) =>
  247. setAddInfoTop({ ...addInfoTop, description: e.target.value })
  248. }
  249. rows={3}
  250. placeholder="请输入"
  251. showCount
  252. maxLength={255}
  253. />
  254. </div>
  255. </div>
  256. {/* 下面的表格 */}
  257. <div className="addTableBox">
  258. <div className="addTableBox_Tit">
  259. <div className="addTableBox_TitL">藏品信息</div>
  260. <div className="addTableBox_TitR">
  261. <Button onClick={() => addPageFu(null)}>添加</Button>
  262. &emsp;
  263. <Popconfirm
  264. disabled={tableSelectList.length === 0}
  265. title="确定删除吗?"
  266. okText="确定"
  267. cancelText="取消"
  268. onConfirm={delTableListFu}
  269. >
  270. <Button disabled={tableSelectList.length === 0}>删除</Button>
  271. </Popconfirm>
  272. </div>
  273. </div>
  274. {/* 表格主体 */}
  275. <div className="addTableBox_table">
  276. <Table
  277. size="small"
  278. scroll={{ y: 300 }}
  279. rowSelection={{
  280. type: "checkbox",
  281. ...rowSelection,
  282. }}
  283. dataSource={results}
  284. columns={columns}
  285. rowKey="id"
  286. pagination={false}
  287. />
  288. </div>
  289. <div className="addTableBox_btn">
  290. <Button onClick={() => submitFu(0)}>存入草稿</Button>
  291. &emsp;
  292. <Button type="primary" onClick={() => submitFu(1)}>
  293. 提交
  294. </Button>
  295. &emsp;
  296. <Button onClick={cancelFu}>返回</Button>
  297. </div>
  298. </div>
  299. </div>
  300. {/* 点击添加或者编辑出来的页面 */}
  301. {addPage ? (
  302. <ObjectAdd
  303. id={addId.current}
  304. colsePage={() => setAddPage(false)}
  305. dirCode={addInfoTop.id}
  306. />
  307. ) : null}
  308. </div>
  309. );
  310. }
  311. const MemoAddObject1 = React.memo(AddObject1);
  312. export default MemoAddObject1;