A7tab2M.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import React, { useCallback, useEffect, useRef, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import {
  4. Button,
  5. DatePicker,
  6. Form,
  7. FormInstance,
  8. Input,
  9. Modal,
  10. Radio,
  11. } from "antd";
  12. import MyPopconfirm from "@/components/MyPopconfirm";
  13. import ZupOne from "@/components/ZupOne";
  14. import { MessageFu } from "@/utils/message";
  15. import { A7_APIgetInfoBook, A7_APIsaveBook } from "@/store/action/A7school";
  16. import dayjs from "dayjs";
  17. import TextArea from "antd/es/input/TextArea";
  18. type Props = {
  19. fId: number;
  20. closeFu: () => void;
  21. addTableFu: () => void;
  22. upTableFu: () => void;
  23. };
  24. function A7tab2M({ fId, closeFu, addTableFu, upTableFu }: Props) {
  25. // 设置表单初始数据(区分编辑和新增)
  26. const FormBoxRef = useRef<FormInstance>(null);
  27. const getInfoFu = useCallback(async (id: number) => {
  28. const res = await A7_APIgetInfoBook(id);
  29. if (res.code === 0) {
  30. const data = res.data;
  31. if (data.type === "link") setIsLink(true);
  32. FormBoxRef.current?.setFieldsValue({
  33. ...data,
  34. myTime: dayjs(data.publishDate),
  35. });
  36. // 设置封面图
  37. ZupOneRef1.current?.setFileComFileFu({
  38. fileName: "",
  39. filePath: data.thumb,
  40. });
  41. // 设置附件
  42. ZupOneRef2.current?.setFileComFileFu({
  43. fileName: data.fileName,
  44. filePath: data.filePath,
  45. });
  46. }
  47. }, []);
  48. useEffect(() => {
  49. if (fId > 0) getInfoFu(fId);
  50. else {
  51. FormBoxRef.current?.setFieldsValue({
  52. myTime: dayjs(Date.now()),
  53. });
  54. }
  55. }, [fId, getInfoFu]);
  56. // 附件的ref
  57. const ZupOneRef1 = useRef<any>(null);
  58. const ZupOneRef2 = useRef<any>(null);
  59. // 附件 是否 已经点击过确定
  60. const [fileCheck, setFileCheck] = useState(false);
  61. // 附件和链接的选择
  62. const [isLink, setIsLink] = useState(false);
  63. // 没有通过校验
  64. const onFinishFailed = useCallback(() => {
  65. setFileCheck(true);
  66. }, []);
  67. // 通过校验点击确定
  68. const onFinish = useCallback(
  69. async (values: any) => {
  70. setFileCheck(true);
  71. // 没有传 封面图 或者 附件
  72. const coverUrl1 = ZupOneRef1.current?.fileComFileResFu();
  73. const coverUrl2 = ZupOneRef2.current?.fileComFileResFu();
  74. if (!coverUrl1.filePath) return;
  75. if (!isLink) {
  76. if (!coverUrl2.filePath) return;
  77. }
  78. // 发布日期
  79. const publishDate = dayjs(values.myTime).format("YYYY-MM-DD");
  80. const obj = {
  81. ...values,
  82. id: fId > 0 ? fId : null,
  83. fileName: coverUrl2?.fileName || "",
  84. filePath: coverUrl2?.filePath || "",
  85. thumb: coverUrl1.filePath,
  86. publishDate,
  87. type: isLink ? "link" : "file",
  88. };
  89. const res = await A7_APIsaveBook(obj);
  90. if (res.code === 0) {
  91. MessageFu.success(fId > 0 ? "编辑成功!" : "新增成功!");
  92. fId > 0 ? addTableFu() : upTableFu();
  93. closeFu();
  94. }
  95. },
  96. [addTableFu, closeFu, fId, isLink, upTableFu]
  97. );
  98. return (
  99. <Modal
  100. // 和 A7tab1M 共用样式
  101. wrapClassName={styles.A7tab1M}
  102. open={true}
  103. title={`${fId > 0 ? "编辑" : "新增"}刊物`}
  104. footer={
  105. [] // 设置footer为空,去掉 取消 确定默认按钮
  106. }
  107. >
  108. <div className="A7mMaiin">
  109. <Form
  110. scrollToFirstError={true}
  111. ref={FormBoxRef}
  112. name="basic"
  113. labelCol={{ span: 3 }}
  114. onFinish={onFinish}
  115. onFinishFailed={onFinishFailed}
  116. autoComplete="off"
  117. >
  118. <Form.Item
  119. label="刊物标题"
  120. name="name"
  121. rules={[{ required: true, message: "请输入刊物标题!" }]}
  122. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  123. >
  124. <Input maxLength={30} showCount placeholder="请输入内容" />
  125. </Form.Item>
  126. {/* 刊物封面 */}
  127. <div className="formRow">
  128. <div className="formLeft">
  129. <span>* </span>
  130. 刊物封面:
  131. </div>
  132. <div className="formRight">
  133. <ZupOne
  134. ref={ZupOneRef1}
  135. isLook={false}
  136. fileCheck={fileCheck}
  137. size={2}
  138. dirCode="schoolThumb2"
  139. myUrl="cms/periodical/upload"
  140. format={["image/jpeg", "image/png"]}
  141. formatTxt="png、jpg和jpeg"
  142. checkTxt="请上传刊物封面!"
  143. upTxt="最多1张"
  144. myType="thumb"
  145. />
  146. </div>
  147. </div>
  148. <div className="formRow">
  149. <div className="formLeft">
  150. <span>* </span>
  151. 刊物内容:
  152. </div>
  153. <div className="formRight">
  154. <div className="A7radio">
  155. <Radio
  156. value={!isLink}
  157. checked={!isLink}
  158. onClick={() => setIsLink(false)}
  159. >
  160. 附件
  161. </Radio>
  162. <Radio
  163. value={isLink}
  164. checked={isLink}
  165. onClick={() => setIsLink(true)}
  166. >
  167. 链接
  168. </Radio>
  169. </div>
  170. </div>
  171. </div>
  172. {/* 刊物附件 | 链接 */}
  173. {isLink ? (
  174. <Form.Item
  175. label="链接"
  176. name="link"
  177. rules={[{ required: true, message: "请输入刊物链接!" }]}
  178. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  179. >
  180. <TextArea maxLength={200} showCount placeholder="请输入内容" />
  181. </Form.Item>
  182. ) : null}
  183. <div className="formRow" hidden={isLink}>
  184. <div className="formLeft"></div>
  185. <div className="formRight">
  186. <ZupOne
  187. ref={ZupOneRef2}
  188. isLook={false}
  189. fileCheck={fileCheck}
  190. size={30}
  191. dirCode="schoolPdf"
  192. myUrl="cms/periodical/upload"
  193. format={["application/pdf"]}
  194. formatTxt="pdf"
  195. checkTxt="请上传刊物附件!"
  196. upTxt="最多1个"
  197. myType="pdf"
  198. />
  199. </div>
  200. </div>
  201. <Form.Item
  202. label="发布日期"
  203. name="myTime"
  204. rules={[{ required: true, message: "请选择发布日期!" }]}
  205. >
  206. <DatePicker />
  207. </Form.Item>
  208. {/* 确定和取消按钮 */}
  209. <Form.Item className="A7mbtn">
  210. <Button type="primary" htmlType="submit">
  211. 提交
  212. </Button>
  213. <br />
  214. <br />
  215. <MyPopconfirm txtK="取消" onConfirm={closeFu} />
  216. </Form.Item>
  217. </Form>
  218. </div>
  219. </Modal>
  220. );
  221. }
  222. const MemoA7tab2M = React.memo(A7tab2M);
  223. export default MemoA7tab2M;