antd 对话框、上传图片、轮播图结合在一起

antd 对话框、上传图片、轮播图结合在一起

import React, { useState, useEffect, useRef } from 'react'
import { Button, Upload, message, Modal, Carousel } from 'antd'
import Icon from './Icon'
import urls from '../../api/urls'
import {
  imageUrlFormat,
  addUploadToken,
  addUploadExtraData,
} from '../../utils/tools'

let orderType = 'up'
export default function UploadImgPlusLightWithOrder({
  value = [],
  msg,
  onChange,
  ctype = 'company',
}) {
  const [fileList, setFileList] = useState([])
  const [isModalVisible, setIsModalVisible] = useState(false)
  const carouselEl = useRef(null)

  const getUrl = (file) => {
    let url = ''
    if (file.url) {
      url = file.url
    } else if (file.response && file.response.data) {
      url = file.response.data.fileImg
    }
    return url
  }

  const handlePreview = (file) => {
    const currentIndex = fileList.findIndex(item => {
      return getUrl(item) === getUrl(file)
    })
    carouselEl.current.goTo(currentIndex, false)
    setIsModalVisible(true)
  }

  const uploadProps = {
    name: 'file',
    action: urls.light.uploadFile,
    data: {
      ctype,
      ...addUploadExtraData(),
    },
    headers: {
      ...addUploadToken(),
    },
    //maxCount: 1,
    listType: 'picture',
    //listType: "picture-card",
    fileList: [...fileList],
    accept: '.jpg',
    className: 'm-upload-order',
    showUploadList: {
      showDownloadIcon: true,
      downloadIcon: (
        <div>
          <Icon
            name="top-arrow"
            title="置顶"
            className="m-upload-img-icon"
            onClick={() => {
              orderType = 'top'
            }}
          ></Icon>
          <Icon
            name="order-arrow"
            title="上移"
            className="m-upload-img-icon"
            onClick={() => {
              orderType = 'up'
            }}
          ></Icon>
          <Icon
            name="order-arrow"
            title="下移"
            className="m-upload-img-icon rotate"
            onClick={() => {
              orderType = 'down'
            }}
          ></Icon>
          <Icon
            name="top-arrow"
            title="置底"
            className="m-upload-img-icon rotate"
            onClick={() => {
              orderType = 'bottom'
            }}
          ></Icon>
        </div>
      ),
      showRemoveIcon: true,
    },
    onChange(info) {
      let fileList = [...info.fileList]
      setFileList(fileList)
      if (info.file.status !== 'uploading') {
        const result = info.fileList.map((item) => {
          if (item.historyUrl) {
            return item.historyUrl
          } else {
            return item.response.data.filePath
          }
        })
        console.log(result)
        if (result.length === 0) {
          //handleFileList([])
          onChange(undefined)
        } else {
          //handleFileList(result)
          onChange(result)
        }
      }
      if (info.file.status === 'done') {
        message.success(`${info.file.name} 上传成功`)
        if (info.file.response.state === 1) {
          const result = info.fileList.map((item) => {
            if (item.historyUrl) {
              return item.historyUrl
            } else {
              return item.response.data.filePath
            }
          })
          console.log(result)
          //handleFileList(result)
          onChange(result)
        }
      } else if (info.file.status === 'error') {
        message.error(`${info.file.name} 上传失败`)
      }
    },
    onDownload(file) {
      let url = ''
      if (file.url) {
        url = file.url
      } else if (file.response && file.response.data) {
        url = file.response.data.fileImg
      }
      console.log(value)
      console.log(url)
      if (Array.isArray(value) && value.length > 0) {
        let index = value.findIndex((item) => {
          return url.includes(item)
        })
        if (orderType === 'top') {
          if (index > 0) {
            const temp = value[index]
            value.splice(index, 1)
            value.unshift(temp)
            handleFileList(value)
            onChange(value)
          }
        } else if (orderType === 'up') {
          // 利用ES6的解构赋值能更加便捷的进行元素交换
          if (index > 0) {
            ;[value[index - 1], value[index]] = [value[index], value[index - 1]]
            handleFileList(value)
            onChange(value)
          }
        } else if (orderType === 'down') {
          if (index < value.length - 1) {
            ;[value[index + 1], value[index]] = [value[index], value[index + 1]]
            handleFileList(value)
            onChange(value)
          }
        } else if (orderType === 'bottom') {
          if (index < value.length - 1) {
            const temp = value[index]
            value.splice(index, 1)
            value.push(temp)
            handleFileList(value)
            onChange(value)
          }
        }
      }
    },
    onPreview: handlePreview,
  }

  const handleFileList = (value) => {
    if (Array.isArray(value)) {
      const tempFileList = value.map((item, index) => {
        return {
          uid: index,
          name: imageUrlFormat(item),
          status: 'done',
          url: imageUrlFormat(item),
          historyUrl: item,
        }
      })
      setFileList(tempFileList)
    }
  }

  useEffect(() => {
    handleFileList(value)
    // eslint-disable-next-line
  }, [])

  return (
    <span>
      <Upload {...uploadProps}>
        <Button>上传图片</Button>
        <span className="m-upload-text">{msg}</span>
      </Upload>
      <Modal
        title="预览"
        visible={isModalVisible}
        onCancel={() => setIsModalVisible(false)}
        className="m-modal-full-screen m-carousel-modal"
        footer={null}
        forceRender
      >
        <Carousel className="m-carousel" dots={ {className: "m-dots"} } ref={carouselEl} >
          {fileList.map((file) =>  {
            let url = ''
            if (file.url) {
              url = file.url
            } else if (file.response && file.response.data) {
              url = file.response.data.fileImg
            }
            return (
              <div className="m-carousel-img-wrap" key={url}>
                <img
                  src={url}
                  className="m-carousel-img"
                  alt="img"
                />
              </div>
            )
          })}
        </Carousel>
        <div className="m-modal-img-footer">
          <Button className="m-space" onClick={() => setIsModalVisible(false)}>
            取消
          </Button>
        </div>
      </Modal>
    </span>
  )
}
.m-carousel-modal .ant-carousel{height: 100%;}
.m-carousel-modal .slick-list{height: 100%;}
.m-carousel-modal .slick-track{height: 100%;}
.m-carousel-modal .slick-slide{height: 100%;}
.m-carousel-modal .slick-slide>div{height: 100%;}
.m-carousel{height: 100%;}
.m-carousel-img-wrap{display: flex; height: 100%;}
.m-carousel-img{margin: auto;height: 100%;}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值