index.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import React, { useCallback, useEffect, useRef, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import { A1EditInfoType } from '@/pages/A1goods/data'
  4. import { Button, Form, FormInstance, Input, InputNumber } from 'antd'
  5. import { A4_APIgetInfo, A4_APIsave } from '@/store/action/A4video'
  6. import { MessageFu } from '@/utils/message'
  7. import classNames from 'classnames'
  8. import ZupOne from '@/components/ZupOne'
  9. import TextArea from 'antd/es/input/TextArea'
  10. import MyPopconfirm from '@/components/MyPopconfirm'
  11. type Props = {
  12. editInfo: A1EditInfoType
  13. closeFu: () => void
  14. addTableFu: () => void
  15. editTableFu: () => void
  16. }
  17. function A4add({ editInfo, closeFu, addTableFu, editTableFu }: Props) {
  18. // 表单的ref
  19. const FormBoxRef = useRef<FormInstance>(null)
  20. // 封面图的ref
  21. const ZupThumbRef = useRef<any>(null)
  22. // 附件的ref
  23. const ZupVideoRef = useRef<any>(null)
  24. // 编辑/查看 进入页面 获取信息
  25. const getInfoFu = useCallback(async (id: number) => {
  26. const res = await A4_APIgetInfo(id)
  27. if (res.code === 0) {
  28. const info = res.data
  29. const obj = {
  30. ...info
  31. }
  32. FormBoxRef.current?.setFieldsValue(obj)
  33. // 设置封面图
  34. ZupThumbRef.current?.setFileComFileFu({
  35. fileName: '',
  36. filePath: info.thumb
  37. })
  38. // 设置附件
  39. ZupVideoRef.current?.setFileComFileFu({
  40. fileName: info.fileName,
  41. filePath: info.filePath
  42. })
  43. }
  44. }, [])
  45. // 附件 是否 已经点击过确定
  46. const [fileCheck, setFileCheck] = useState(false)
  47. useEffect(() => {
  48. if (editInfo.id > 0) {
  49. getInfoFu(editInfo.id)
  50. } else {
  51. FormBoxRef.current?.setFieldsValue({
  52. sort: 999
  53. })
  54. }
  55. }, [editInfo.id, getInfoFu])
  56. // 没有通过校验
  57. const onFinishFailed = useCallback(() => {
  58. setFileCheck(true)
  59. }, [])
  60. // 通过校验点击确定
  61. const onFinish = useCallback(
  62. async (values: any) => {
  63. setFileCheck(true)
  64. const coverUrl1 = ZupThumbRef.current?.fileComFileResFu()
  65. // 没有传 封面图
  66. if (!coverUrl1.filePath) return
  67. // 没有传视频附件
  68. let fileName = ''
  69. let filePath = ''
  70. // txt附件
  71. const ZupVideoRefObj = ZupVideoRef.current?.fileComFileResFu()
  72. fileName = ZupVideoRefObj.fileName
  73. filePath = ZupVideoRefObj.filePath
  74. if (!filePath) return MessageFu.warning('请上传附件!')
  75. const obj = {
  76. ...values,
  77. id: editInfo.id > 0 ? editInfo.id : null,
  78. thumb: coverUrl1.filePath,
  79. fileName,
  80. filePath
  81. }
  82. const res = await A4_APIsave(obj)
  83. if (res.code === 0) {
  84. MessageFu.success(editInfo.txt + '成功!')
  85. editInfo.id > 0 ? editTableFu() : addTableFu()
  86. closeFu()
  87. }
  88. },
  89. [addTableFu, closeFu, editInfo.id, editInfo.txt, editTableFu]
  90. )
  91. return (
  92. <div className={styles.A4add}>
  93. <div className={classNames('A4aMain', editInfo.txt === '查看' ? 'A4aMainLook' : '')}>
  94. <Form
  95. ref={FormBoxRef}
  96. name='basic'
  97. labelCol={{ span: 3 }}
  98. onFinish={onFinish}
  99. onFinishFailed={onFinishFailed}
  100. autoComplete='off'
  101. scrollToFirstError
  102. >
  103. <Form.Item label='名称' name='name' rules={[{ required: true, message: '请输入名称!' }]}>
  104. <Input
  105. readOnly={editInfo.txt === '查看'}
  106. placeholder='请输入内容'
  107. maxLength={20}
  108. showCount
  109. />
  110. </Form.Item>
  111. {/* 封面 */}
  112. <div className='formRow'>
  113. <div className='formLeft'>
  114. <span>* </span>
  115. 封面:
  116. </div>
  117. <div className='formRight'>
  118. <ZupOne
  119. ref={ZupThumbRef}
  120. isLook={editInfo.txt === '查看'}
  121. fileCheck={fileCheck}
  122. size={5}
  123. dirCode={'A2video'}
  124. myUrl='cms/video/upload'
  125. format={['image/jpeg', 'image/png']}
  126. formatTxt='png、jpg和jpeg'
  127. checkTxt='请上传封面图!'
  128. upTxt='最多1张'
  129. myType='thumb'
  130. />
  131. </div>
  132. </div>
  133. {editInfo.txt === '查看' ? <br /> : null}
  134. {/* 附件 */}
  135. <div className='formRow'>
  136. <div className='formLeft'>
  137. <span>* </span>
  138. 附件:
  139. </div>
  140. <div className='formRight'>
  141. <ZupOne
  142. ref={ZupVideoRef}
  143. isLook={editInfo.txt === '查看'}
  144. fileCheck={fileCheck}
  145. size={200}
  146. dirCode='A2video'
  147. myUrl='cms/video/upload'
  148. format={['video/mp4']}
  149. formatTxt='mp4'
  150. checkTxt='请上传mp4附件!'
  151. upTxt='最多1个'
  152. myType='video'
  153. />
  154. </div>
  155. </div>
  156. {editInfo.txt === '查看' ? <br /> : null}
  157. <Form.Item label='简介' name='remark'>
  158. <TextArea
  159. readOnly={editInfo.txt === '查看'}
  160. maxLength={200}
  161. showCount
  162. placeholder='请输入内容'
  163. />
  164. </Form.Item>
  165. <div className='A4fromRow'>
  166. <Form.Item
  167. label='排序值'
  168. name='sort'
  169. rules={[{ required: true, message: '请输入排序值!' }]}
  170. >
  171. <InputNumber min={1} max={999} precision={0} placeholder='请输入' />
  172. </Form.Item>
  173. <div className='A4_6Frow' hidden={editInfo.txt === '查看'}>
  174. 请输入1~999的数字。数字越小,排序越靠前。数字相同时,更新发布的内容排在前面
  175. </div>
  176. </div>
  177. {/* 确定和取消按钮 */}
  178. <Form.Item className='A4abtn'>
  179. {editInfo.txt === '查看' ? (
  180. <Button onClick={closeFu}>返回</Button>
  181. ) : (
  182. <>
  183. <Button type='primary' htmlType='submit'>
  184. 提交
  185. </Button>
  186. <br />
  187. <br />
  188. <MyPopconfirm txtK='取消' onConfirm={closeFu} />
  189. </>
  190. )}
  191. </Form.Item>
  192. </Form>
  193. </div>
  194. </div>
  195. )
  196. }
  197. const MemoA4add = React.memo(A4add)
  198. export default MemoA4add