index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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, Select } from 'antd'
  6. import TextArea from 'antd/es/input/TextArea'
  7. import { A7_APIgetInfo, A7_APIadd, A7_APIedit } from '@/store/action/A7collection'
  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 { A7AddType, A7tableType } from '@/types/api/A7collection'
  13. import ZupTypes from '@/components/ZupTypes'
  14. type Props = {
  15. editInfo: A1EditInfoType
  16. closeFu: () => void
  17. addTableFu: () => void
  18. editTableFu: () => void
  19. }
  20. function A7add({ editInfo, closeFu, addTableFu, editTableFu }: Props) {
  21. // 表单的ref
  22. const FormBoxRef = useRef<FormInstance>(null)
  23. // 封面图的ref
  24. const ZupThumbRef1 = useRef<any>(null)
  25. // 附件的ref
  26. const ZupTypesRef = useRef<any>(null)
  27. // 回显数据的方法调用
  28. const dataShow = useCallback((info: A7tableType) => {
  29. let editObj = null
  30. if (info.status === 0) {
  31. editObj = {
  32. ...info,
  33. title: info.titleB,
  34. type: info.typeB,
  35. era: info.eraB,
  36. texture: info.textureB,
  37. size: info.sizeB,
  38. publish: info.publishB,
  39. remark: info.remarkB,
  40. img: info.imgB,
  41. imgTh: info.imgThB,
  42. typeModel: info.typeModelB,
  43. typeImage: info.typeImageB,
  44. typeAudio: info.typeAudioB,
  45. typeVideo: info.typeVideoB,
  46. modelFile: info.modelFileB,
  47. imageFiles: info.imageFilesB,
  48. videoFile: info.videoFileB,
  49. audioFile: info.audioFileB,
  50. webSite: info.webSiteB,
  51. level: info.levelB
  52. }
  53. } else {
  54. editObj = { ...info }
  55. }
  56. FormBoxRef.current?.setFieldsValue({ ...editObj, publish: dayjs(editObj.publish) })
  57. // 设置封面图
  58. ZupThumbRef1.current?.setFileComFileFu({
  59. fileName: '',
  60. thUrl: editObj.imgTh,
  61. url: editObj.img
  62. })
  63. // 设置附件
  64. const imgFileList = editObj.typeImage
  65. ? editObj.imageFiles.map((v: any) => ({
  66. fileName: v.split('/').pop(),
  67. filePath: v,
  68. id: v,
  69. type: 'img'
  70. }))
  71. : []
  72. ZupTypesRef.current?.setFileComFileFu({
  73. fileList: [
  74. ...imgFileList,
  75. editObj.typeModel && {
  76. fileName: editObj.modelFile.split('/').pop(),
  77. filePath: editObj.modelFile,
  78. id: editObj.modelFile,
  79. type: 'model'
  80. },
  81. editObj.typeAudio && {
  82. fileName: editObj.audioFile.split('/').pop(),
  83. filePath: editObj.audioFile,
  84. id: editObj.audioFile,
  85. type: 'audio'
  86. },
  87. editObj.typeVideo && {
  88. fileName: editObj.videoFile.split('/').pop(),
  89. filePath: editObj.videoFile,
  90. id: editObj.videoFile,
  91. type: 'video'
  92. }
  93. ],
  94. type: [
  95. editObj.typeImage && 'img',
  96. editObj.typeModel && 'model',
  97. editObj.typeAudio && 'audio',
  98. editObj.typeVideo && 'video'
  99. ]
  100. })
  101. }, [])
  102. // 编辑 进入页面 获取信息
  103. const getInfoFu = useCallback(
  104. async (id: number) => {
  105. if (editInfo.info) dataShow(editInfo.info)
  106. else {
  107. const res = await A7_APIgetInfo(id)
  108. if (res.code === 0) {
  109. dataShow(res.data)
  110. }
  111. }
  112. },
  113. [dataShow, editInfo.info]
  114. )
  115. // 附件 是否 已经点击过确定
  116. const [fileCheck, setFileCheck] = useState(false)
  117. // 编辑填入数据
  118. useEffect(() => {
  119. if (editInfo.id > 0) {
  120. getInfoFu(editInfo.id)
  121. }
  122. }, [editInfo.id, getInfoFu])
  123. // 没有通过校验
  124. const onFinishFailed = useCallback(() => {
  125. setFileCheck(true)
  126. }, [])
  127. // 通过校验点击确定
  128. const onFinish = useCallback(
  129. async (values: any) => {
  130. setFileCheck(true)
  131. const coverUrl1 = ZupThumbRef1.current?.fileComFileResFu()
  132. const { fileList, sonType, sonIsOk } = ZupTypesRef.current?.fileComFileResFu()
  133. // 没有传 封面图
  134. if (!coverUrl1.url) return MessageFu.warning('请上传首页封面图!')
  135. if (sonIsOk) return MessageFu.warning('请上传附件!')
  136. // 发布
  137. const obj1: A7AddType = {
  138. ...values,
  139. publish: values.publish?.format('YYYY-MM-DD') || '',
  140. img: coverUrl1.url,
  141. imgTh: coverUrl1.thUrl,
  142. status: 1,
  143. modelFile: fileList.model.filePath || '',
  144. imageFiles: fileList.img.map((v: any) => v.filePath) || [],
  145. videoFile: fileList.video.filePath || '',
  146. audioFile: fileList.audio.filePath || '',
  147. typeModel: sonType.includes('model') || false,
  148. typeImage: sonType.includes('img') || false,
  149. typeAudio: sonType.includes('audio') || false,
  150. typeVideo: sonType.includes('video') || false
  151. }
  152. // 预发布
  153. const obj2: A7AddType = {
  154. ...values,
  155. audioFileB: fileList.audio.filePath || '',
  156. eraB: values.era,
  157. imageFilesB: fileList.img.map((v: any) => v.filePath) || [],
  158. imgB: coverUrl1.url,
  159. imgThB: coverUrl1.thUrl,
  160. modelFileB: fileList.model.filePath || '',
  161. publishB: values.publish?.format('YYYY-MM-DD') || '',
  162. remarkB: values.remark,
  163. sizeB: values.size,
  164. status: 0,
  165. textureB: values.texture,
  166. titleB: values.title,
  167. typeAudioB: sonType.includes('audio') || false,
  168. typeB: values.type,
  169. typeImageB: sonType.includes('img') || false,
  170. typeModelB: sonType.includes('model') || false,
  171. typeVideoB: sonType.includes('video') || false,
  172. videoFileB: fileList.video.filePath || '',
  173. webSiteB: '',
  174. levelB: values.level
  175. }
  176. let res: any
  177. if (btnRef.current === '1') {
  178. res =
  179. editInfo.txt === '新增'
  180. ? await A7_APIadd(obj1)
  181. : await A7_APIedit({ ...obj1, artifactId: editInfo.id })
  182. } else {
  183. res =
  184. editInfo.txt === '新增'
  185. ? await A7_APIadd(obj2)
  186. : await A7_APIedit({ ...obj2, artifactId: editInfo.id })
  187. }
  188. if (res.code === 0) {
  189. MessageFu.success(`${editInfo.txt}成功!`)
  190. editInfo.id > 0 ? editTableFu() : addTableFu()
  191. closeFu()
  192. }
  193. },
  194. [addTableFu, closeFu, editInfo.id, editInfo.txt, editTableFu]
  195. )
  196. const formOkBtnRef = useRef<any>(null)
  197. const btnRef = useRef('')
  198. return (
  199. <div className={styles.A7add}>
  200. <div className={classNames('A7aMain')}>
  201. <Form
  202. ref={FormBoxRef}
  203. name='basic'
  204. labelCol={{ span: 3 }}
  205. onFinish={onFinish}
  206. onFinishFailed={onFinishFailed}
  207. autoComplete='off'
  208. scrollToFirstError
  209. >
  210. <Form.Item
  211. label='标题'
  212. name='title'
  213. rules={[{ required: true, message: '请输入标题!' }]}
  214. >
  215. <Input placeholder='请输入内容,最多20字' maxLength={20} showCount />
  216. </Form.Item>
  217. <Form.Item label='类别' name='type' initialValue={4}>
  218. <Select
  219. defaultValue={0}
  220. placeholder='请选择类别'
  221. options={[
  222. { label: '平面纸质类', value: 1 },
  223. { label: '棉麻丝绸类', value: 2 },
  224. { label: '专业器物类', value: 3 },
  225. { label: '其他', value: 4 }
  226. ]}
  227. />
  228. </Form.Item>
  229. <Form.Item label='级别' name='level'>
  230. <Input placeholder='请输入内容,最多10字' maxLength={10} showCount />
  231. </Form.Item>
  232. <Form.Item label='年代' name='era'>
  233. <Input placeholder='请输入内容,最多10字' maxLength={10} showCount />
  234. </Form.Item>
  235. <Form.Item label='质地' name='texture'>
  236. <Input placeholder='请输入内容,最多10字' maxLength={10} showCount />
  237. </Form.Item>
  238. <Form.Item label='尺寸' name='size'>
  239. <Input placeholder='请输入内容,最多30字' maxLength={30} showCount />
  240. </Form.Item>
  241. <Form.Item label='简介' name='remark'>
  242. <TextArea maxLength={200} showCount placeholder='请输入内容,不超过200个字' />
  243. </Form.Item>
  244. {/* 封面 */}
  245. <div className='formRow'>
  246. <div className='formLeft'>
  247. <span>* </span>
  248. 封面图:
  249. </div>
  250. <div className='formRight'>
  251. <ZupOne
  252. ref={ZupThumbRef1}
  253. fileCheck={fileCheck}
  254. size={20}
  255. dirCode={'A7collection'}
  256. myUrl='museum/upload/uploadImg'
  257. format={['image/jpeg', 'image/jpg', 'image/png']}
  258. inchTxt='5:4'
  259. formatTxt='png、jpg和jpeg'
  260. checkTxt='请上传封面图!'
  261. upTxt='最多1张'
  262. myType='thumb'
  263. />
  264. </div>
  265. </div>
  266. <Form.Item
  267. label={
  268. <>
  269. <span style={{ color: 'red' }}>*</span> 文件类型
  270. </>
  271. }
  272. name='file'
  273. >
  274. <ZupTypes
  275. ref={ZupTypesRef}
  276. selecFlag='模型/图片/音频/视频'
  277. fileCheck={fileCheck}
  278. dirCode='A7collection'
  279. myUrl='museum/upload/upload'
  280. imgSize={20}
  281. />
  282. </Form.Item>
  283. <Form.Item
  284. label='发布日期'
  285. name='publish'
  286. initialValue={dayjs()}
  287. rules={[{ required: true, message: '请选择日期!' }]}
  288. >
  289. <DatePicker
  290. defaultValue={dayjs()}
  291. allowClear
  292. placeholder='请选择日期'
  293. style={{ width: 200 }}
  294. />
  295. </Form.Item>
  296. {/* 确定和取消按钮 */}
  297. <Form.Item className='A7abtn'>
  298. <div className='A7abtnBox'>
  299. <Button
  300. type='primary'
  301. htmlType='submit'
  302. onClick={() => {
  303. btnRef.current = '1'
  304. formOkBtnRef.current?.click()
  305. }}
  306. >
  307. 发布
  308. </Button>
  309. <Button
  310. type='primary'
  311. htmlType='submit'
  312. onClick={() => {
  313. btnRef.current = '0'
  314. formOkBtnRef.current?.click()
  315. }}
  316. >
  317. 预发布
  318. </Button>
  319. <MyPopconfirm txtK='取消' onConfirm={closeFu} />
  320. </div>
  321. </Form.Item>
  322. </Form>
  323. </div>
  324. </div>
  325. )
  326. }
  327. const MemoA7add = React.memo(A7add)
  328. export default MemoA7add