newText.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import TextAideInput from "@/pages/launchSystemV3/components/TextAideInput"
  2. import { txtLength } from "@/utils/utils"
  3. import { Button, Card, Form, Modal, Popconfirm, Radio, Space, message } from "antd"
  4. import React, { useEffect, useState } from "react"
  5. import { TextTypeList } from "../../const"
  6. import { DeleteOutlined, PlusOutlined } from "@ant-design/icons"
  7. import AddTextS from "./addTextS"
  8. import style from '../index.less'
  9. import styles from '../Material/index.less'
  10. import VideoNews from "@/pages/launchSystemNew/components/newsModal/videoNews"
  11. interface Props {
  12. textData: any,
  13. dynamicMaterialDTos: any,
  14. mediaType: 0 | 1 | 2,
  15. deliveryMode?: string,
  16. value?: any,
  17. visible?: boolean
  18. onClose?: () => void
  19. onChange?: (value: any) => void
  20. }
  21. /**
  22. * 创意文案
  23. * @param param0
  24. * @returns
  25. */
  26. const NewText: React.FC<Props> = ({ visible, onClose, onChange, value, textData, dynamicMaterialDTos, mediaType, deliveryMode }) => {
  27. /*************************************/
  28. const [form] = Form.useForm();
  29. const type = Form.useWatch('type', form)
  30. const textDto = Form.useWatch('textDto', form)
  31. const [textList, setTextList] = useState<PULLIN.TextDtoProps[]>([])
  32. const [desVisible, setDesVisible] = useState<boolean>(false)
  33. const [addSTitle, setAddStitle] = useState<string>()
  34. /*************************************/
  35. const handleOk = (values: any) => {
  36. const { type, textDto } = values
  37. onChange?.({
  38. type,
  39. dynamicCreativesTextDetailDTOList: textDto.map((item: any) => {
  40. let data: any = {}
  41. Object.keys(item).forEach(key => {
  42. data[key] = [item[key]]
  43. })
  44. return data
  45. })
  46. })
  47. }
  48. useEffect(() => {
  49. if (value && Object.keys(value).length) {
  50. const { type, dynamicCreativesTextDetailDTOList } = value
  51. form.setFieldsValue({
  52. type, textDto: dynamicCreativesTextDetailDTOList.map((item: any) => {
  53. let data: any = {}
  54. Object.keys(item).forEach(key => {
  55. data[key] = item[key]?.[0]
  56. })
  57. return data
  58. })
  59. })
  60. }
  61. }, [value])
  62. useEffect(() => {
  63. if (textData && Object.keys(textData)) {
  64. let data = Object.values(textData).map((item: any) => {
  65. let content = item.children.content
  66. return { label: content.description, restriction: content.restriction, value: item.name, required: item.required }
  67. })
  68. setTextList(data)
  69. }
  70. }, [textData])
  71. // 一一对应显示素材
  72. const showMaterial = (index: number) => {
  73. const dynamicGroup = dynamicMaterialDTos?.dynamicGroup
  74. if (dynamicGroup && Object.keys(dynamicGroup).length) {
  75. let dynamic = dynamicGroup[index]
  76. const keys = Object.keys(dynamic)
  77. if (deliveryMode === "DELIVERY_MODE_CUSTOMIZE") {
  78. return <div className={style.detail_body_m}>
  79. {(keys.includes('video_id') || keys.includes('short_video1')) ? <>
  80. <div className={style.video}>
  81. <VideoNews src={dynamic?.video_id?.url || dynamic?.short_video1?.url} style={{ width: 40, height: 30 }} />
  82. {dynamic?.cover_id && <div className={style.cover_image} style={{ marginLeft: 4, width: 40, height: 30, minWidth: 42 }}>
  83. <img src={dynamic?.cover_id?.url} style={{ maxWidth: '96%', maxHeight: '96%' }} />
  84. </div>}
  85. </div>
  86. </> : keys.includes('image_id') ? <>
  87. <div className={style.cover_image} style={{ width: 40, height: 30, minWidth: 42 }}>
  88. <img src={dynamic?.image_id?.url} />
  89. </div>
  90. </> : (keys.includes('image_list') || keys.includes('element_story')) ? <>
  91. {dynamic?.[keys.includes('image_list') ? 'image_list' : 'element_story']?.map((item: { url: string | undefined; }, index: undefined) => <div className={style.cover_image} key={index} style={{ width: 30, height: 24, minWidth: 32 }}>
  92. <img src={item?.url} />
  93. </div>)}
  94. </> : null}
  95. </div>
  96. } else {
  97. return <div style={{ display: 'flex', gap: 5, flexWrap: 'wrap' }}>
  98. {dynamic?.list?.map((item: any, index: number) => {
  99. if (Array.isArray(item)) {
  100. let length = item.length
  101. return <div className={styles.boxList_body_item} key={index} style={{ width: 30, height: 30 }}>
  102. <div className={styles.content} style={{ width: 30, height: 30 }}>
  103. {item.map((l, i) => <img src={l?.url} key={i} style={{ width: length === 6 ? 9.999 : 14.999 }} />)}
  104. </div>
  105. </div>
  106. } else if (item?.url?.includes('mp4')) {
  107. return <div className={styles.boxList_body_item} key={index} style={{ width: 30, height: 30 }}>
  108. <div className={styles.content} style={{ width: 30, height: 30 }}>
  109. <VideoNews src={item?.url} style={{ width: 30, height: 30 }} maskBodyStyle={{ backgroundColor: "rgba(242, 246, 254, 0.1)" }} />
  110. </div>
  111. </div>
  112. } else {
  113. return <div className={styles.boxList_body_item} key={index} style={{ width: 30, height: 30 }}>
  114. <div className={styles.content} style={{ width: 30, height: 30 }}><img src={item?.url} /></div>
  115. </div>
  116. }
  117. })}
  118. </div>
  119. }
  120. }
  121. return null
  122. }
  123. return <Modal
  124. title={<Space>
  125. <strong style={{ fontSize: 20 }}>创意文案</strong>
  126. {type !== 0 && <>
  127. {textList.some(item => item.value === "description") && <a style={{ fontSize: 12 }} onClick={() => { setDesVisible(true); setAddStitle(textList.find(item => item.value === "description")?.label) }}>批量添加{textList.find(item => item.value === "description")?.label}</a>}
  128. {textList.some(item => item.value === "title") && <a style={{ fontSize: 12 }} onClick={() => { setDesVisible(true); setAddStitle(textList.find(item => item.value === "description")?.label) }}>批量添加{textList.find(item => item.value === "description")?.label}</a>}
  129. </>}
  130. </Space>}
  131. visible={visible}
  132. onCancel={onClose}
  133. footer={null}
  134. width={800}
  135. className={`modalResetCss`}
  136. bodyStyle={{ padding: '0 0 40px', position: 'relative', borderRadius: '0 0 8px 8px' }}
  137. maskClosable={false}
  138. >
  139. <Form
  140. form={form}
  141. name="newText"
  142. labelAlign='left'
  143. layout="vertical"
  144. colon={false}
  145. style={{ backgroundColor: '#f1f4fc', maxHeight: 650, overflow: 'hidden', overflowY: 'auto', padding: '0 10px 10px', borderRadius: '0 0 8px 8px' }}
  146. scrollToFirstError={{
  147. behavior: 'smooth',
  148. block: 'center'
  149. }}
  150. onFinishFailed={({ errorFields }) => {
  151. message.error(errorFields?.[0]?.errors?.[0])
  152. }}
  153. initialValues={{
  154. type: 0,
  155. textDto: [{}]
  156. }}
  157. onFinish={handleOk}
  158. >
  159. <Card className="cardResetCss" style={{ marginTop: 10 }}>
  160. <Form.Item name="type" label={<strong>文案分配规则</strong>} style={{ marginBottom: 0 }} rules={[{ required: true, message: '请选择文案分配规则!' }]}>
  161. <Radio.Group onChange={(e) => {
  162. let value = e.target.value
  163. let count = dynamicMaterialDTos.dynamicGroup.length
  164. let oldtextDto: PULLIN.TextDtoProps[] = JSON.parse(JSON.stringify(textDto))
  165. let length = oldtextDto.length
  166. if (value === 0) {
  167. oldtextDto = [textDto[0] || {}]
  168. } else if (value === 1) {
  169. if (count > length) {
  170. oldtextDto = oldtextDto.concat(Array(count - length).fill({}))
  171. } else {
  172. oldtextDto = oldtextDto.slice(0, count)
  173. }
  174. } else if (value === 2) {
  175. if (count < length) {
  176. oldtextDto = oldtextDto.slice(0, count)
  177. }
  178. }
  179. form.setFieldsValue({ textDto: oldtextDto })
  180. }}>
  181. {TextTypeList.filter(item => mediaType !== 1 ? item.value !== 4 : true).map(item => <Radio value={item.value} key={item.value}>{item.label}</Radio>)}
  182. </Radio.Group>
  183. </Form.Item>
  184. </Card>
  185. <Form.List name="textDto">
  186. {(fields, { add, remove }) => (<>
  187. {fields.map(({ key, name, ...restField }, num) => (
  188. <Card
  189. title={type === 0 ? null : <div style={{ display: 'flex', gap: 5, alignItems: 'center' }}>
  190. {type === 1 ? <strong style={{ fontSize: 14 }}>创意组{num + 1}</strong> : type === 0 ? null : <strong style={{ fontSize: 14 }}>文案组{num + 1}</strong>}
  191. {type === 1 && showMaterial(num)}
  192. </div>}
  193. className="cardResetCss"
  194. style={{ marginTop: 10, width: '100%' }}
  195. key={key}
  196. extra={([3, 2, 4].includes(type) && textDto?.length > 1) && <Popconfirm
  197. title="你确定删除当前文案组?"
  198. onConfirm={() => remove(name)}
  199. >
  200. <DeleteOutlined style={{ color: 'red' }} />
  201. </Popconfirm>}
  202. >
  203. <div style={{ display: 'flex', flexWrap: 'wrap', gap: 10, width: '100%', flexDirection: 'column' }}>
  204. {textList.map(item => {
  205. return <Form.Item
  206. {...restField}
  207. label={<strong>{item.label}</strong>}
  208. name={[name, item.value]}
  209. key={key}
  210. style={{ marginBottom: 0, width: '100%' }}
  211. rules={[{
  212. required: item.required, message: '请输入正确的' + item.label, validator: (rule, value) => {
  213. if (!value) {
  214. return Promise.reject()
  215. } else if (!value.match(RegExp(item.restriction.textRestriction.textPattern))) {
  216. return Promise.reject()
  217. } else if (txtLength(value) > item.restriction.textRestriction.maxLength) {
  218. return Promise.reject()
  219. }
  220. return Promise.resolve()
  221. }
  222. }]}
  223. >
  224. <TextAideInput placeholder={'请输入' + item.label} style={{ width: 580 }} maxTextLength={item.restriction.textRestriction.maxLength} />
  225. </Form.Item>
  226. })}
  227. </div>
  228. </Card>
  229. ))}
  230. {[3, 2, 4].includes(type) && !(type === 2 && textDto.length >= dynamicMaterialDTos.dynamicGroup.length) && <Form.Item style={{ marginTop: 10, marginBottom: 0 }}>
  231. <Button type="dashed" onClick={() => add({})} block icon={<PlusOutlined />} disabled={type === 3 && textDto.length >= 30}>
  232. 新增
  233. </Button>
  234. </Form.Item>}
  235. </>)}
  236. </Form.List>
  237. <Form.Item className="submit_pull">
  238. <Space>
  239. <Button onClick={onClose}>取消</Button>
  240. <Button type="primary" htmlType="submit" className="modalResetCss">
  241. 确定
  242. </Button>
  243. </Space>
  244. </Form.Item>
  245. </Form>
  246. {/* 批量添加 */}
  247. {desVisible && <AddTextS
  248. visible={desVisible}
  249. title={addSTitle}
  250. onClose={() => {
  251. setDesVisible(false)
  252. setAddStitle(undefined)
  253. }}
  254. onChange={(value) => {
  255. if (value) {
  256. let valList = value
  257. .split(/\r?\n/) // 按换行符分割字符串
  258. .map(line => line.trim()) // 去除每行的首尾空白
  259. .filter(line => line !== ''); // 过滤掉空行
  260. let fieldData = textList.find(item => item.label === addSTitle)
  261. let count = dynamicMaterialDTos.dynamicGroup.length
  262. if (type === 1) {
  263. let len = 0
  264. const newTextDto = textDto.map((item: { [x: string]: any }) => {
  265. if (fieldData?.value && !item?.[fieldData?.value] && valList.length >= len + 1) {
  266. return { ...item, [fieldData.value]: valList[len++] }
  267. }
  268. return item
  269. })
  270. form.setFieldsValue({
  271. textDto: newTextDto
  272. })
  273. } else if (type === 2) {
  274. let len = 0
  275. const newTextDto = textDto.map((item: { [x: string]: any }) => {
  276. if (fieldData?.value && !item?.[fieldData?.value] && valList.length >= len + 1) {
  277. return { ...item, [fieldData.value]: valList[len++] }
  278. }
  279. return item
  280. })
  281. let diffTextDto: any[] = []
  282. if (newTextDto.length < count && len < valList.length) {
  283. let diffCount = count - newTextDto.length
  284. let diffTextCount = valList.length - len
  285. let diff = 0
  286. if (diffCount >= diffTextCount) {
  287. diff = diffTextCount
  288. } else {
  289. diff = diffCount
  290. }
  291. diffTextDto = Array(diff).fill('').map((item: { [x: string]: any }) => {
  292. if (fieldData?.value) {
  293. return { ...item, [fieldData.value]: valList[len++] }
  294. }
  295. return item
  296. })
  297. }
  298. form.setFieldsValue({
  299. textDto: [...newTextDto, ...diffTextDto]
  300. })
  301. } else if ([3, 4].includes(type)) {
  302. let len = 0
  303. const newTextDto = textDto.map((item: { [x: string]: any }) => {
  304. if (fieldData?.value && !item?.[fieldData?.value] && valList.length >= len + 1) {
  305. return { ...item, [fieldData.value]: valList[len++] }
  306. }
  307. return item
  308. })
  309. let diffTextDto: any[] = []
  310. if (len < valList.length) {
  311. let diff = valList.length - len
  312. diffTextDto = Array(diff).fill('').map((item: { [x: string]: any }) => {
  313. if (fieldData?.value) {
  314. return { ...item, [fieldData.value]: valList[len++] }
  315. }
  316. return item
  317. })
  318. }
  319. form.setFieldsValue({
  320. textDto: [...newTextDto, ...diffTextDto]
  321. })
  322. }
  323. }
  324. setDesVisible(false)
  325. setAddStitle(undefined)
  326. }}
  327. />}
  328. </Modal>
  329. }
  330. export default React.memo(NewText)