index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import React, { useCallback, useEffect, useRef, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import { Button, Form, FormInstance, Input, Select } from "antd";
  4. import ZupOne from "@/components/ZupOne";
  5. import { A1_APIgetInfo, A1_APIsave } from "@/store/action/A1video";
  6. import { MessageFu } from "@/utils/message";
  7. import MyPopconfirm from "@/components/MyPopconfirm";
  8. type Props = {
  9. addId: number;
  10. closeFu: () => void;
  11. addTableFu: () => void;
  12. type: "video" | "poster";
  13. };
  14. function A1add({ addId, closeFu, addTableFu, type }: Props) {
  15. // 表单的ref
  16. const FormBoxRef = useRef<FormInstance>(null);
  17. // 没有通过校验
  18. const onFinishFailed = useCallback(() => {
  19. setFileCheck(true);
  20. }, []);
  21. // 通过校验点击确定
  22. const onFinish = useCallback(
  23. async (value: any) => {
  24. setFileCheck(true);
  25. const coverUrl1 = ZupOneRef1.current?.fileComFileResFu();
  26. // 没有传 封面图
  27. if (!coverUrl1.filePath) return;
  28. let coverUrl2 = { filePath: "", fileName: "" };
  29. if (type === "video") {
  30. coverUrl2 = ZupOneRef2.current?.fileComFileResFu();
  31. // 没有传 视频
  32. if (!coverUrl2.filePath) return;
  33. }
  34. const obj = {
  35. ...value,
  36. id: addId > 0 ? addId : null,
  37. thumb: coverUrl1.filePath,
  38. type,
  39. filePath: type === "video" ? coverUrl2.filePath : null,
  40. fileName: type === "video" ? coverUrl2.fileName : null,
  41. };
  42. const res = await A1_APIsave(obj);
  43. if (res.code === 0) {
  44. MessageFu.success(addId > 0 ? "编辑成功!" : "新增成功!");
  45. addTableFu();
  46. closeFu();
  47. }
  48. },
  49. [addId, addTableFu, closeFu, type]
  50. );
  51. // 附件 是否 已经点击过确定
  52. const [fileCheck, setFileCheck] = useState(false);
  53. // 上传附件的ref
  54. const ZupOneRef1 = useRef<any>(null);
  55. const ZupOneRef2 = useRef<any>(null);
  56. // 通过id获取详情
  57. const getInfoFu = useCallback(async (id: number) => {
  58. const res = await A1_APIgetInfo(id);
  59. if (res.code === 0) {
  60. const data = res.data;
  61. FormBoxRef.current?.setFieldsValue({
  62. name: data.name,
  63. display: data.display,
  64. });
  65. ZupOneRef1.current?.setFileComFileFu({
  66. fileName: "",
  67. filePath: data.thumb,
  68. });
  69. ZupOneRef2.current?.setFileComFileFu({
  70. fileName: data.fileName,
  71. filePath: data.filePath,
  72. });
  73. }
  74. }, []);
  75. useEffect(() => {
  76. if (addId > 0) {
  77. // 编辑
  78. getInfoFu(addId);
  79. } else {
  80. // 新增
  81. FormBoxRef.current?.setFieldsValue({
  82. display: 1,
  83. });
  84. }
  85. }, [addId, getInfoFu]);
  86. return (
  87. <div className={styles.A1add}>
  88. <div className="A1Amain">
  89. <Form
  90. ref={FormBoxRef}
  91. name="basic"
  92. labelCol={{ span: 3 }}
  93. onFinish={onFinish}
  94. onFinishFailed={onFinishFailed}
  95. autoComplete="off"
  96. >
  97. <Form.Item
  98. label="标题"
  99. name="name"
  100. rules={[{ required: true, message: "请输入标题!" }]}
  101. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  102. >
  103. <Input maxLength={50} showCount placeholder="请输入内容" />
  104. </Form.Item>
  105. {/* 封面 */}
  106. <div className="formRow">
  107. <div className="formLeft">
  108. <span>* </span>
  109. {type === "video" ? "封面" : "海报"}:
  110. </div>
  111. <div className="formRight">
  112. <ZupOne
  113. ref={ZupOneRef1}
  114. isLook={false}
  115. fileCheck={fileCheck}
  116. size={type === "video" ? 5 : 10}
  117. dirCode={type === "video" ? "tab1Video" : "tab2Poster"}
  118. myUrl="cms/poster/upload"
  119. format={["image/jpeg", "image/png"]}
  120. formatTxt="png、jpg和jpeg"
  121. checkTxt="请上传封面图"
  122. upTxt="最多1张"
  123. myType="thumb"
  124. />
  125. </div>
  126. </div>
  127. {/* 视频 */}
  128. {type === "video" ? (
  129. <div className="formRow">
  130. <div className="formLeft">
  131. <span>* </span>视频:
  132. </div>
  133. <div className="formRight">
  134. <ZupOne
  135. ref={ZupOneRef2}
  136. isLook={false}
  137. fileCheck={fileCheck}
  138. size={500}
  139. dirCode={type === "video" ? "tab1Video" : "tab2Poster"}
  140. myUrl="cms/poster/upload"
  141. format={["video/mp4"]}
  142. formatTxt="mp4"
  143. checkTxt="请上传视频"
  144. upTxt="最多1个"
  145. myType="video"
  146. />
  147. </div>
  148. </div>
  149. ) : null}
  150. <Form.Item
  151. label="自动播放"
  152. name="display"
  153. rules={[{ required: true, message: "请选择自动播放!" }]}
  154. >
  155. <Select
  156. placeholder="请选择"
  157. style={{ width: 200 }}
  158. options={[
  159. { value: 1, label: "是" },
  160. { value: 0, label: "否" },
  161. ]}
  162. />
  163. </Form.Item>
  164. <div className="formLastTit">当设为“是”时,该内容将参与自动播放</div>
  165. {/* 确定和取消按钮 */}
  166. <br />
  167. <Form.Item wrapperCol={{ offset: 9, span: 16 }}>
  168. <Button type="primary" htmlType="submit">
  169. 提交
  170. </Button>
  171. &emsp;
  172. <MyPopconfirm txtK="取消" onConfirm={closeFu} />
  173. </Form.Item>
  174. </Form>
  175. </div>
  176. </div>
  177. );
  178. }
  179. const MemoA1add = React.memo(A1add);
  180. export default MemoA1add;