index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { useModel } from 'umi'
  2. import { Modal, Button, Form, Select, message, Space, Image as AntImg, Row, Col, Upload, Input } from 'antd'
  3. import { SelectValue } from 'antd/es/select'
  4. import Cropprt from '@/components/Cropper'
  5. import React, { useCallback, useEffect, useState } from "react"
  6. const { Option } = Select
  7. import style from './index.less'
  8. import { request } from 'umi'
  9. import { api } from '@/services/api'
  10. import { DeleteOutlined, InboxOutlined } from '@ant-design/icons'
  11. import { RcFile } from 'antd/lib/upload'
  12. const { Dragger } = Upload
  13. const { TextArea } = Input
  14. import getMD5 from '@/components/MD5'
  15. type Props = {
  16. visible: boolean,
  17. // handleOk: () => void
  18. hideModal: () => void,
  19. editData?: any,
  20. ajax: any,
  21. dataType: string
  22. }
  23. const channelCon = ['朋友圈信息流', '公众平台流量', '广点通', '小程序', '头条', '抖音']
  24. function AddOriginality(props: Props) {
  25. let { visible, hideModal, editData, ajax, dataType: typeData } = props
  26. const [loading, setLoading] = useState<boolean>(false)
  27. const [imgs, setImgs] = useState<string[]>([])
  28. const [video, setVideo] = useState<RcFile>()
  29. const [imgLength, setImgLength] = useState<{ width: number, height: number }>({ width: 800, height: 800 })
  30. const [fileList, setFileList] = useState<any[]>([])
  31. const [queryForm, setQueryForm] = useState<any>({ media: '', title: '', article: '', labelIds: [], type: 1, channel: undefined, dataType: '', id: undefined })
  32. const { addIdeas, getLabels, dataLable } = useModel('useLaunch.useMaterial')
  33. useEffect(() => {
  34. if (editData && typeof editData === 'object' && Object.keys(editData).length > 0) {
  35. let { media, labelIds, type, channel, id, title, article, dataType } = editData
  36. setImgs(media?.split(','))
  37. setQueryForm({ media, labelIds: labelIds || [], type, id, channel, title, article, dataType })
  38. } else {
  39. setQueryForm({ media: '', labelIds: [], type: 1, channel: undefined, id: undefined, title: '', article: '', dataType: '' })
  40. }
  41. }, [editData])
  42. useEffect(() => {
  43. getLabels.run({ pageNum: 1, pageSize: 200 })
  44. }, [])
  45. /** 选择了图片触发 */
  46. const setImgsHandle = useCallback(async (img: any) => {
  47. if (img) {
  48. if (imgs.length > 0) {
  49. let file = await dataURLtoFile(img, Math.round(new Date() as any / 1000))
  50. let md5 = await getMD5(file)
  51. for (let item of imgs) {
  52. let file = await dataURLtoFile(item, Math.round(new Date() as any / 1000))
  53. let md5s = await getMD5(file)
  54. if (md5 === md5s) {
  55. message.error('请选择不同图片!')
  56. return
  57. }
  58. }
  59. let onesCk = await imgMessage([imgs[0]])
  60. let imgCK = await imgMessage([img])
  61. if ((onesCk[0]?.width !== imgCK[0]?.width) || (onesCk[0]?.height !== imgCK[0]?.height)) {
  62. message.error('上传组图,需要图片尺寸相同')
  63. return
  64. }
  65. }
  66. setImgs([...imgs, img])
  67. imgMessage([...imgs, img]).then((res: any) => {
  68. if (res && res?.length > 0) {
  69. setImgLength({ width: res[0]?.width || 800, height: res[0]?.height })
  70. }
  71. })
  72. }
  73. }, [imgs, imgLength])
  74. /** 上传创意 确定 */
  75. const handleOk = useCallback(async () => {
  76. let { labelIds, id } = queryForm
  77. setLoading(true)
  78. if (labelIds.length === 0) { message.error('请选择标签'); return }
  79. // if(!type) { message.error('请选择类型'); return }
  80. // if(!channel) { message.error('请选择投放渠道'); return }
  81. // if(!dataType) { message.error('请选择数据类型'); return }
  82. dataLable.run({ labelIds: labelIds?.toString(), id, name: 'tagIdea' }).then(res => {
  83. setLoading(false)
  84. setQueryForm({ title: '', labelIds: [], type: 1 })
  85. hideModal()
  86. ajax?.refresh()
  87. })
  88. }, [queryForm, imgs, loading])
  89. // 获取图片宽高
  90. const imgMessage = (imgs: string[]): Promise<{ width: number, height: number }[]> => {
  91. return new Promise((resolve, reject) => {
  92. if (imgs.length > 0) {
  93. let imgsAll = imgs?.map((item: string) => {
  94. return new Promise((resolve) => {
  95. let img: any = new Image();
  96. img.onload = function (e: any) {
  97. resolve({ width: this.width, height: this.height })
  98. }
  99. img.src = item;
  100. })
  101. })
  102. Promise.all(imgsAll).then((res: any) => {
  103. resolve(res)
  104. })
  105. } else {
  106. reject([])
  107. }
  108. })
  109. }
  110. // base64转File
  111. const dataURLtoFile = (dataurl: any, filename: any) => {
  112. var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  113. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  114. while (n--) {
  115. u8arr[n] = bstr.charCodeAt(n);
  116. }
  117. return new File([u8arr], filename, { type: mime });
  118. }
  119. const del = useCallback((src: string) => {
  120. let newImgs = imgs.filter((item: string) => item !== src)
  121. setImgs(newImgs)
  122. }, [imgs])
  123. return <Modal visible={visible} title={
  124. <>
  125. <Space>
  126. <span>标签修改</span>
  127. {/* <Select value={queryForm.type} disabled={queryForm?.id} onChange={(value: SelectValue)=>{setQueryForm({...queryForm, type: value})}} placeholder="选择类型">
  128. <Option value={1}>图片</Option>
  129. <Option value={2}>视频</Option>
  130. </Select> */}
  131. </Space>
  132. </>
  133. }
  134. onOk={handleOk} onCancel={hideModal}
  135. footer={
  136. <>
  137. <Space>
  138. <Button onClick={hideModal}>取消</Button>
  139. <Button type="primary" onClick={handleOk} loading={addIdeas?.loading || dataLable?.loading || loading}>{editData && typeof editData === 'object' && Object.keys(editData).length > 0 ? '修改' : '新增'}</Button>
  140. </Space>
  141. </>
  142. }
  143. >
  144. <Form labelCol={{ span: 4 }}>
  145. {/* {
  146. queryForm?.type === 1 ? <Form.Item >
  147. {
  148. !queryForm?.id && <Cropprt isChangeCropperSize={imgs.length > 0} {...imgLength} callback={(img) => { setImgsHandle(img) }} />
  149. }
  150. {
  151. imgs.length > 0 && <div className={style.imgs}>
  152. <Space>
  153. {imgs?.map((item: string, index: number) => (
  154. <div className={style.imgCon} key={index}>
  155. <AntImg width={60} src={item} preview={false}/>
  156. {!queryForm?.id && <div className={style.handle}>
  157. <Space><div className={style.bt} onClick={()=>{del(item)}}><DeleteOutlined style={{color: 'red'}}/></div></Space>
  158. </div>}
  159. </div>
  160. ))}
  161. </Space>
  162. </div>
  163. }
  164. </Form.Item> :
  165. queryForm?.type === 2 ? <Form.Item>
  166. {
  167. !queryForm?.id ? <Dragger
  168. accept="video/*"
  169. action=""
  170. customRequest={()=>{}}
  171. maxCount={1}
  172. fileList={fileList}
  173. multiple={false}
  174. beforeUpload={(file: RcFile, FileList: RcFile[]) : any=>{
  175. const isLt2M = file.size / 1024 / 1024 < 500;
  176. if (!isLt2M) {
  177. message.error('请上传视频小于 500MB!');
  178. return isLt2M;
  179. }
  180. setFileList(FileList)
  181. setVideo(file)
  182. }}
  183. >
  184. <p className="ant-upload-drag-icon">
  185. <InboxOutlined />
  186. </p>
  187. <p className="ant-upload-text">单击或拖动文件到此区域以上载</p>
  188. <p className="ant-upload-hint">
  189. 支持MP4....
  190. </p>
  191. </Dragger> :
  192. <video style={{width: '100%'}} height={160} src={queryForm?.media} loop={true} autoPlay={false} controls></video>
  193. }
  194. </Form.Item> : null
  195. }
  196. <Form.Item label="标题">
  197. <Input disabled={queryForm?.id} value={queryForm.title} onChange={(e)=>{setQueryForm({...queryForm, title: e.target.value})}} placeholder="请输入标题"/>
  198. </Form.Item>
  199. <Form.Item label="文案">
  200. <TextArea
  201. disabled={queryForm?.id}
  202. value={queryForm.article}
  203. placeholder="请输入文案"
  204. autoSize={{ minRows: 2 }}
  205. onChange={(e)=>{setQueryForm({...queryForm, article: e.target.value})}}
  206. />
  207. </Form.Item> */}
  208. <Form.Item label="标签">
  209. <Select value={queryForm.labelIds} onChange={(value: SelectValue) => { setQueryForm({ ...queryForm, labelIds: value }) }} placeholder="选择标签" mode="tags">
  210. {getLabels?.data?.records.map((item: any) => (<Option value={item?.id} key={item?.id}>{item?.label}</Option>))}
  211. </Select>
  212. </Form.Item>
  213. {/* <Form.Item label="渠道">
  214. <Select value={queryForm.channel} disabled={queryForm?.id} onChange={(value: SelectValue)=>{setQueryForm({...queryForm, channel: value})}} placeholder="选择投放渠道">
  215. {channelCon?.map((item: string, index: number) => <Option value={item} key={index}>{item}</Option>)}
  216. </Select>
  217. </Form.Item>
  218. <Form.Item label="数据类型">
  219. <Select value={queryForm.dataType} disabled={queryForm?.id && typeData === 'all'} onChange={(value: SelectValue)=>{setQueryForm({...queryForm, dataType: value})}} placeholder="选择数据类型">
  220. <Option value='personal' disabled={queryForm?.id && typeData === 'group'}>个人</Option>
  221. <Option value='group'>组内共享</Option>
  222. <Option value='all'>公司全员共享</Option>
  223. </Select>
  224. </Form.Item> */}
  225. </Form>
  226. </Modal>
  227. }
  228. export default React.memo(AddOriginality)