index.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import React, { useCallback, useEffect, useRef, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import classNames from 'classnames'
  4. import { A1EditInfoType } from '../data'
  5. import { Button, Form, FormInstance, Input, DatePicker } from 'antd'
  6. import TextArea from 'antd/es/input/TextArea'
  7. import { A4_APIgetInfo, A4_APIadd, A4_APIedit } from '@/store/action/A4news'
  8. import { MessageFu } from '@/utils/message'
  9. import ZupOne from '@/components/ZupOne'
  10. import MyPopconfirm from '@/components/MyPopconfirm'
  11. import dayjs from 'dayjs'
  12. import ZRichTexts from '@/components/ZRichTexts'
  13. import { A4AddType, A4tableType } from '@/types/api/A4news'
  14. type Props = {
  15. editInfo: A1EditInfoType
  16. closeFu: () => void
  17. addTableFu: () => void
  18. editTableFu: () => void
  19. }
  20. function A4add({ editInfo, closeFu, addTableFu, editTableFu }: Props) {
  21. // 表单的ref
  22. const FormBoxRef = useRef<FormInstance>(null)
  23. // 封面图的ref
  24. const ZupThumbRef1 = useRef<any>(null)
  25. const ZupThumbRef2 = useRef<any>(null)
  26. // 正文的ref
  27. const ZRichTextRef = useRef<any>(null)
  28. // 回显数据的方法调用
  29. const dataShow = useCallback((info: A4tableType) => {
  30. let editObj = null
  31. if (info.status === 0) {
  32. editObj = {
  33. ...info,
  34. title: info.titleB,
  35. publish: info.publishB,
  36. remark: info.remarkB,
  37. context: info.contextB,
  38. indexImg: info.indexImgB,
  39. indexImgTh: info.indexImgThB,
  40. infoImg: info.infoImgB,
  41. infoImgTh: info.infoImgThB
  42. }
  43. } else {
  44. editObj = { ...info }
  45. }
  46. FormBoxRef.current?.setFieldsValue({ ...editObj, publish: dayjs(editObj.publish) })
  47. // 设置封面图
  48. ZupThumbRef1.current?.setFileComFileFu({
  49. fileName: '',
  50. thUrl: editObj.indexImgTh,
  51. url: editObj.indexImg
  52. })
  53. ZupThumbRef2.current?.setFileComFileFu({
  54. fileName: '',
  55. thUrl: editObj.infoImgTh,
  56. url: editObj.infoImg
  57. })
  58. // 设置正文
  59. ZRichTextRef.current?.ritxtShowFu({
  60. txtArr: [
  61. {
  62. id: editObj.informationId,
  63. name: '正文',
  64. txt: editObj.context,
  65. fileInfo: { fileName: '', filePath: '' }
  66. }
  67. ]
  68. })
  69. }, [])
  70. // 编辑 进入页面 获取信息
  71. const getInfoFu = useCallback(
  72. async (id: number) => {
  73. if (editInfo.info) dataShow(editInfo.info)
  74. else {
  75. const res = await A4_APIgetInfo(id)
  76. if (res.code === 0) {
  77. dataShow(res.data)
  78. }
  79. }
  80. },
  81. [dataShow, editInfo.info]
  82. )
  83. // 附件 是否 已经点击过确定
  84. const [fileCheck, setFileCheck] = useState(false)
  85. // 编辑填入数据
  86. useEffect(() => {
  87. if (editInfo.id > 0) {
  88. getInfoFu(editInfo.id)
  89. }
  90. }, [editInfo.id, getInfoFu])
  91. // 没有通过校验
  92. const onFinishFailed = useCallback(() => {
  93. setFileCheck(true)
  94. }, [])
  95. // 通过校验点击确定
  96. const onFinish = useCallback(
  97. async (values: any) => {
  98. setFileCheck(true)
  99. const coverUrl1 = ZupThumbRef1.current?.fileComFileResFu()
  100. const coverUrl2 = ZupThumbRef2.current?.fileComFileResFu()
  101. const context = ZRichTextRef.current?.fatherBtnOkFu()
  102. // 没有传 封面图
  103. if (!coverUrl1.url) return MessageFu.warning('请上传首页封面图!')
  104. if (!coverUrl2.url) return MessageFu.warning('请上传详情封面图!')
  105. if (!context.val.txtArr[0].txt) return MessageFu.warning('请输入正文!')
  106. // 发布
  107. const obj1: A4AddType = {
  108. ...values,
  109. indexImg: coverUrl1.url,
  110. indexImgTh: coverUrl1.thUrl,
  111. infoImg: coverUrl2.url,
  112. infoImgTh: coverUrl2.thUrl,
  113. context: context.val.txtArr[0].txt,
  114. publish: values.publish?.format('YYYY-MM-DD') || '',
  115. publishB: '',
  116. status: 1,
  117. titleB: '',
  118. urlB: ''
  119. }
  120. // 预发布
  121. const obj2: A4AddType = {
  122. indexImg: '',
  123. indexImgB: coverUrl1.url,
  124. indexImgTh: '',
  125. indexImgThB: coverUrl1.thUrl,
  126. infoImg: '',
  127. infoImgB: coverUrl2.url,
  128. infoImgTh: '',
  129. infoImgThB: coverUrl2.thUrl,
  130. publish: '',
  131. publishB: values.publish?.format('YYYY-MM-DD') || '',
  132. status: 0,
  133. title: '',
  134. titleB: values.title,
  135. remark: '',
  136. remarkB: values.remark,
  137. context: '',
  138. contextB: context.val.txtArr[0].txt
  139. }
  140. let res: any
  141. if (btnRef.current === '1') {
  142. res =
  143. editInfo.txt === '新增'
  144. ? await A4_APIadd(obj1)
  145. : await A4_APIedit({ ...obj1, informationId: editInfo.id })
  146. } else {
  147. res =
  148. editInfo.txt === '新增'
  149. ? await A4_APIadd(obj2)
  150. : await A4_APIedit({ ...obj2, informationId: editInfo.id })
  151. }
  152. if (res.code === 0) {
  153. MessageFu.success(`${editInfo.txt}成功!`)
  154. editInfo.id > 0 ? editTableFu() : addTableFu()
  155. closeFu()
  156. }
  157. },
  158. [addTableFu, closeFu, editInfo.id, editInfo.txt, editTableFu]
  159. )
  160. const formOkBtnRef = useRef<any>(null)
  161. const btnRef = useRef('')
  162. return (
  163. <div className={styles.A4add}>
  164. <div className={classNames('A4aMain')}>
  165. <Form
  166. ref={FormBoxRef}
  167. name='basic'
  168. labelCol={{ span: 3 }}
  169. onFinish={onFinish}
  170. onFinishFailed={onFinishFailed}
  171. autoComplete='off'
  172. scrollToFirstError
  173. >
  174. <Form.Item
  175. label='标题'
  176. name='title'
  177. rules={[{ required: true, message: '请输入标题!' }]}
  178. >
  179. <Input placeholder='请输入内容,不超过30个字' maxLength={30} showCount />
  180. </Form.Item>
  181. <Form.Item
  182. label='发布日期'
  183. name='publish'
  184. rules={[{ required: true, message: '请选择日期!' }]}
  185. initialValue={dayjs()}
  186. >
  187. <DatePicker
  188. defaultValue={dayjs()}
  189. allowClear
  190. placeholder='请选择日期'
  191. style={{ width: 200 }}
  192. />
  193. </Form.Item>
  194. <Form.Item label='摘要' name='remark'>
  195. <TextArea maxLength={200} showCount placeholder='请输入内容,不超过200个字' />
  196. </Form.Item>
  197. <Form.Item label='正文' name='context'>
  198. <ZRichTexts
  199. check={true}
  200. dirCode={'A4news'}
  201. isLook={false}
  202. ref={ZRichTextRef}
  203. myUrl='museum/upload/upload'
  204. isOne={true}
  205. upAudioBtnNone={true}
  206. video={false}
  207. />
  208. </Form.Item>
  209. {/* 封面 */}
  210. <div className='formRow'>
  211. <div className='formLeft'>
  212. <span>* </span>
  213. 首页封面:
  214. </div>
  215. <div className='formRight'>
  216. <ZupOne
  217. ref={ZupThumbRef1}
  218. fileCheck={fileCheck}
  219. size={5}
  220. dirCode={'A1banner'}
  221. myUrl='museum/upload/uploadImg'
  222. format={['image/jpg', 'image/png']}
  223. inchTxt='7:2'
  224. formatTxt='png、jpg'
  225. checkTxt='请上传封面图!'
  226. upTxt='最多1张'
  227. myType='thumb'
  228. />
  229. </div>
  230. </div>
  231. <div className='formRow'>
  232. <div className='formLeft'>
  233. <span>* </span>
  234. 详情封面:
  235. </div>
  236. <div className='formRight'>
  237. <ZupOne
  238. ref={ZupThumbRef2}
  239. fileCheck={fileCheck}
  240. size={5}
  241. dirCode={'A1banner'}
  242. myUrl='museum/upload/uploadImg'
  243. format={['image/jpg', 'image/png']}
  244. inchTxt='5:7'
  245. formatTxt='png、jpg'
  246. checkTxt='请上传封面图!'
  247. upTxt='最多1张'
  248. myType='thumb'
  249. />
  250. </div>
  251. </div>
  252. {/* 确定和取消按钮 */}
  253. <Form.Item className='A4abtn'>
  254. <div className='A4abtnBox'>
  255. <Button
  256. type='primary'
  257. htmlType='submit'
  258. onClick={() => {
  259. btnRef.current = '1'
  260. formOkBtnRef.current?.click()
  261. }}
  262. >
  263. 发布
  264. </Button>
  265. <Button
  266. type='primary'
  267. htmlType='submit'
  268. onClick={() => {
  269. btnRef.current = '0'
  270. formOkBtnRef.current?.click()
  271. }}
  272. >
  273. 预发布
  274. </Button>
  275. <MyPopconfirm txtK='取消' onConfirm={closeFu} />
  276. </div>
  277. </Form.Item>
  278. </Form>
  279. </div>
  280. </div>
  281. )
  282. }
  283. const MemoA4add = React.memo(A4add)
  284. export default MemoA4add