import { useAjax } from "@/Hook/useAjax" import SelectCloud from "@/pages/launchSystemNew/components/selectCloud" import { get_tools_video_capture } from "@/services/launchAdq/global" import { MinusCircleOutlined, PlusOutlined } from "@ant-design/icons" import { Button, Form, message, Modal, Radio, Space, Switch, Image, Popconfirm } from "antd" import React, { useEffect, useState } from "react" import { useModel } from "umi" import styles from './index.less' import { getVideoImgUrl } from "@/utils/utils" interface Props { value?: any[] material?: { label: string, name: string, restriction: any, arrayProperty?: any }[], sysAdcreative: any onChange?: (data: any) => void } /** * 选择素材组 * @returns */ const Material: React.FC = (props) => { /**************************/ const { onChange, material = [], sysAdcreative, value } = props const { adcreativeTemplateId } = sysAdcreative const [visible, setVisible] = useState(false) const [selectVideoVisible, set_selectVideoVisible] = useState(false) const [videoImgsVisbile, set_videoImgsVisbile] = useState(false) const [videoUploads, setVideoUploads] = useState() const [imgUploads, setImgUploads] = useState() const { init } = useModel('useLaunchAdq.useBdMediaPup') const [materialConfig, setMaterialConfig] = useState<{ adcreativeTemplateId?: number, type: string, cloudSize: { relation: string, width: number, height: number }[], list: any[], index: number, max: number, sliderImgContent: any }>({ type: '',//类型 cloudSize: [],//素材搜索条件 list: [],//素材 index: 0, // 素材组下标 max: 1,//素材数量 sliderImgContent: undefined })//图片素材配置 const [videoImgs, set_videoImgs] = useState<{//视频封面图设置 activeUrl: string,//选中的视频封面图地址 preview: boolean,//是否开启图片点击预览 urlList: any[],//生成的视频封面列表 }>({ activeUrl: '', preview: false, urlList: [ 'https://test-adq-media.oss-cn-hangzhou.aliyuncs.com/image/21D8D51AD98C4FF8BF41F1C2D28EA39F.jpg', 'https://test-adq-media.oss-cn-hangzhou.aliyuncs.com/image/80DBE1AB3EDE4E85ABAE5F1670D9FED0.jpg', 'https://test-adq-media.oss-cn-hangzhou.aliyuncs.com/image/BCB2DAB86BDB4549BCB8E493C4F29E82.jpg', 'https://test-adq-media.oss-cn-hangzhou.aliyuncs.com/image/545A4C2A5B874C82A9D1C0C063624AE5.jpg' ] }) const [form] = Form.useForm(); let materials = Form.useWatch('materials', form) const getVideoCapture = useAjax((params) => get_tools_video_capture(params)) /**************************/ // 回填 useEffect(() => { if (visible) { if (value && value?.length > 0) { let data = value?.map((item: any) => { let data: any = {} for (const key in item) { switch (key) { case 'imageUrl'://图素材 data.image = item[key] break; case 'videoUrl'://视频素材 data.video = item[key] break; case 'imageUrlList'://图素材 data.image_list = item[key] break; case 'shortVideo1Url'://视频素材 data.short_video1 = item[key] break; case 'elementStory': data.element_story = item[key].map((item: { imageUrl: string }) => item.imageUrl) break; } } return data }) form.setFieldsValue({ materials: data }) } else { form.setFieldsValue({ materials: [undefined] }) } } }, [value, visible]) // 处理判断有哪些素材 展示按钮 useEffect(() => { if (material?.length > 0) { setVideoUploads(material?.find((item: { name: string }) => item.name === 'short_video1' || item.name === 'video')) setImgUploads(material?.find((item: { name: string }) => item.name === 'image' || item.name === 'image_list' || item.name === 'element_story')) } else { setVideoUploads({}) setImgUploads({}) } }, [material]) // 确定 const handleOk = () => { form.validateFields().then(values => { let data = values?.materials?.map((item: any) => { let data: any = {} for (const key in item) { switch (key) { case 'image'://图素材 data.imageUrl = item[key] break; case 'video'://视频素材 data.videoUrl = item[key] break; case 'image_list'://图素材 data.imageUrlList = item[key] break; case 'short_video1'://视频素材 data.shortVideo1Url = item[key] break; case 'element_story': data.elementStory = item[key].map((url: string) => ({ imageUrl: url })) break; } } return data }) onChange && onChange(data) setVisible(false) }) } return <> { setVisible(true) }}>{value && value?.length > 0 ? '编辑' : '添加'} {visible && setVisible(false)} title={<> 创意素材 {videoUploads && Object.keys(videoUploads)?.length > 0 && } {imgUploads && Object.keys(imgUploads)?.length > 0 && } {(materials && materials?.length > 1) && form.setFieldsValue({ materials: [undefined] })} okText="是" cancelText="否" > } } width={930} bodyStyle={{ maxHeight: 650, overflowY: 'scroll' }} onOk={handleOk} >
{(fields, { add, remove }) => (<> {fields.map((field, num) => (
{material?.map((item, index) => { if (item.name === 'short_video1' || item.name === 'video') { return {item.label}} rules={[{ required: true, message: '请选择素材!' }]} name={[field.name, item.name]} key={index} >
{ init({ mediaType: 'VIDEO', cloudSize: adcreativeTemplateId === 1708 ? [[{ relation: '=', width: 1280, height: 720 }]] : [[{ relation: '=', width: item.restriction.videoRestriction.minWidth, height: item.restriction.videoRestriction.minHeight }]], maxSize: item.restriction.videoRestriction.fileSize * 1024 }) setMaterialConfig({ ...materialConfig, type: item.name, max: 1, index: num, adcreativeTemplateId }) setTimeout(() => { set_selectVideoVisible(true) }, 100) }}>

{materials?.length > 0 && materials[num] && Object.keys(materials[num])?.includes(item.name) ?

} if (item.name === 'image') { return {item.label}} rules={[{ required: true, message: '请选择素材!' }]} name={[field.name, item.name]} key={index} >
{ init({ mediaType: 'IMG', cloudSize: [[{ relation: '=', width: item.restriction.imageRestriction.width, height: item.restriction.imageRestriction.height }]], maxSize: item.restriction.imageRestriction.fileSize * 1024 }) setMaterialConfig({ ...materialConfig, type: item.name, max: 1, index: num, adcreativeTemplateId }) setTimeout(() => { set_selectVideoVisible(true) }, 100) }}>

{materials?.length > 0 && materials[num] && Object.keys(materials[num])?.includes(item.name) ? : <> {`推荐尺寸(${item.restriction.imageRestriction.width} x ${item.restriction.imageRestriction.height})`} {`${item.restriction.imageRestriction.fileFormat?.map((str: any) => str?.replace('IMAGE_TYPE_', ''))};小于 ${item.restriction.imageRestriction.fileSize}KB`} }

} if (item.name === 'image_list') { return {item.label}} rules={[{ required: true, message: '请选择素材!' }]} name={[field.name, item.name]} key={index} >
= 3 ? styles.image_list : styles.imageMater}`} style={item.arrayProperty.maxNumber >= 3 ? { flexFlow: 'row', width: '100%' } : {}} onClick={() => { init({ mediaType: 'IMG', num: item.arrayProperty.maxNumber, cloudSize: [[{ relation: '=', width: item.restriction.imageRestriction.width, height: item.restriction.imageRestriction.height }]], maxSize: item.restriction.imageRestriction.fileSize * 1024 }) setMaterialConfig({ ...materialConfig, type: item.name, max: item.arrayProperty.maxNumber, index: num, adcreativeTemplateId }) setTimeout(() => { set_selectVideoVisible(true) }, 100) }}> {Array(item.arrayProperty.maxNumber).fill('').map((arr, index1) => { return

= 3 ? { width: 130, height: 130 } : { width: 130, height: 130, justifyContent: 'center' }}> {materials?.length > 0 && materials[num] && Object.keys(materials[num])?.includes(item.name) && materials[num][item.name][index1] ? : <> {`推荐尺寸(${item.restriction.imageRestriction.width} x ${item.restriction.imageRestriction.height})`} {`${item.restriction.imageRestriction.fileFormat?.map((str: any) => str?.replace('IMAGE_TYPE_', ''))};小于 ${item.restriction.imageRestriction.fileSize}KB`} }

})}
} if (item.name === 'element_story') { return {item.label}} rules={[{ required: true, message: '请选择素材!' }]} name={[field.name, item.name]} key={index} >
= 3 ? styles.image_list : styles.imageMater}`} style={item.arrayProperty.maxNumber >= 3 ? { flexFlow: 'row', width: '100%' } : {}} onClick={() => { init({ mediaType: 'IMG', num: item.arrayProperty.maxNumber, cloudSize: [[{ relation: '=', width: item.restriction.imageRestriction.width, height: item.restriction.imageRestriction.height }]], maxSize: item.restriction.imageRestriction.fileSize * 1024 }) setMaterialConfig({ ...materialConfig, type: item.name, max: item.arrayProperty.maxNumber, index: num, adcreativeTemplateId }) setTimeout(() => { set_selectVideoVisible(true) }, 100) }}> {Array(item.arrayProperty.maxNumber).fill('').map((arr, index1) => { return

= 3 ? { width: 130, height: 130 } : { width: 130, height: 130, justifyContent: 'center' }}> {materials?.length > 0 && materials[num] && Object.keys(materials[num])?.includes(item.name) && materials[num][item.name][index1] ? item?.restriction.imageRestriction?.height ? { width: '100%', height: 'auto' } : {}} src={materials[num][item.name][index1]} /> : <> {`推荐尺寸(${item.restriction.imageRestriction.width} x ${item.restriction.imageRestriction.height})`} {`${item.restriction.imageRestriction.fileFormat?.map((str: any) => str?.replace('IMAGE_TYPE_', ''))};小于 ${item.restriction.imageRestriction.fileSize}KB`} }

})}
} return null })} {fields?.length > 1 && remove(field.name)} style={{ marginBottom: 24, color: 'red' }} />}
))} )}
} {/* 选择视频素材 */} {selectVideoVisible && set_selectVideoVisible(false)} sliderImgContent={materialConfig.index === 99999 ? undefined : materialConfig.type === 'element_story' ? (materials[materialConfig.index] && Object.keys(materials[materialConfig.index])?.includes('element_story')) ? materials[materialConfig.index]['element_story']?.map((item: string) => ({ url: item })) : undefined : materialConfig.type === 'image_list' ? (materials[materialConfig.index] && Object.keys(materials[materialConfig.index])?.includes('image_list')) ? materials[materialConfig.index]['image_list']?.map((item: string) => ({ url: item })) : undefined : (materials[materialConfig.index] && Object.keys(materials[materialConfig.index])?.includes(materialConfig.type)) ? [{ url: materials[materialConfig.index][materialConfig.type] }] : undefined } onChange={(content: any) => { if (content.length > 0) { if (materialConfig.index === 99999) { if (materialConfig.type === 'image_list' || materialConfig.type === 'element_story') { let urls = content?.map((item: any) => item?.url) let max = materialConfig.max let materialsNew = materials.map((item: any) => { let newItem = item || {} // 判断是否有字段,是否设置了值 if (Object.keys(newItem).includes(materialConfig.type) && newItem[materialConfig.type]) { if (max > newItem[materialConfig.type].length && urls.length > 0) { let difference = max - newItem[materialConfig.type].length let material: any[] = [] if (urls.length >= difference) { material = urls.splice(0, difference) } else { material = urls.splice(0, urls.length) } newItem[materialConfig.type] = [...newItem[materialConfig.type], ...material] return newItem } else { return newItem } } else { if (urls.length >= max) { let material = urls.splice(0, max) return { ...newItem, [materialConfig.type]: material } } else if (urls.length > 0) { let material = urls.splice(0, urls.length) return { ...newItem, [materialConfig.type]: material } } else { return newItem } } }) if (urls.length > 0) { let data = Array(Math.ceil(urls.length / max)).fill(undefined).map(item => { if (urls.length >= max) { let material = urls.splice(0, max) return { [materialConfig.type]: material } } else { let material = urls.splice(0, urls.length) return { [materialConfig.type]: material } } }) materialsNew = [...materialsNew, ...data] } form.setFieldsValue({ materials: materialsNew }) } else { let newMaterials = content?.map((item: any) => { if (materialConfig.type === 'video' && material.length === 2) { return { [materialConfig.type]: item?.url, image: getVideoImgUrl(item?.url) } } return { [materialConfig.type]: item?.url } }) if (newMaterials.length > 0) { if (materials?.every((item: any) => !item)) { // 没设置过 form.setFieldsValue({ materials: newMaterials }) } else { // 设置过 let materialsNew = materials.map((item: any) => { let newItem = item || {} if (Object.keys(newItem).includes(materialConfig.type) && newItem[materialConfig.type]) { return item } else { if (newMaterials.length > 0) { let material = newMaterials.splice(0, 1) return { ...newItem, ...material[0] } } else { return item } } }) if (newMaterials.length > 0) { materialsNew = [...materialsNew, ...newMaterials] } form.setFieldsValue({ materials: materialsNew }) } } } } else { materials = materials?.map((item: any, index: number) => { if (materialConfig.index === index) { if (materialConfig.type === 'image_list' || materialConfig.type === 'element_story') { if (item) { item[materialConfig.type] = content?.map((item: any) => item?.url) return { ...item } } else { return { [materialConfig.type]: content?.map((item: any) => item?.url) } } } else { if (item) { item[materialConfig.type] = content[0]?.url return { ...item } } else { if (materialConfig.type === 'video' && material.length === 2) { return { [materialConfig.type]: content[0]?.url, image: getVideoImgUrl(content[0]?.url) } } return { [materialConfig.type]: content[0]?.url } } } } return item }) form.setFieldsValue({ materials }) } } set_selectVideoVisible(false) }} />} {/* 视频封面图弹窗 */} {videoImgsVisbile && 生成封面图 { set_videoImgs({ ...videoImgs, preview: checked }) }} />} onOk={() => { if (videoImgs.activeUrl) { // setImgMaterialConfig({ ...imgMaterialConfig, list: [{ url: videoImgs.activeUrl }] }) set_videoImgsVisbile(false) } else { message.error('请选择图片,获取使用取消按钮关闭弹窗!') } }} onCancel={() => { set_videoImgsVisbile(false) }} confirmLoading={getVideoCapture.loading} width={600} > { let url = e.target.value set_videoImgs({ ...videoImgs, activeUrl: url }) }}> { videoImgs?.urlList?.map((item: any, index: number) => { return }) } } } export default React.memo(Material)