Blame view

src/components/publicPicture/index.vue 2.65 KB
1 2 3 4 5
<!--
 * @Description: 
 * @Autor: renchao
 * @LastEditTime: 2023-07-25 16:08:48
-->
任超 committed
6
<template>
任超 committed
7
  <el-image-viewer :on-close="closeViewer" :url-list="urlList">
任超 committed
8 9 10
  </el-image-viewer>
</template>
<script>
11 12 13 14 15 16 17 18 19 20 21
  import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
  export default {
    components: {
      ElImageViewer,
    },
    props: {
      urlList: {
        type: Array,
        default: function () {
          return []
        }
任超 committed
22 23
      }
    },
24 25 26
    data () {
      return {
        wrapperElem: null
任超 committed
27
      }
任超 committed
28
    },
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    mounted () {
      this.$nextTick(() => {
        let wrapper = document.getElementsByClassName(
          'el-image-viewer__actions__inner'
        )
        let downImg = document.createElement('i')
        downImg.setAttribute('class', 'el-icon-download')
        wrapper[0].appendChild(downImg)
        if (wrapper.length > 0) {
          this.wrapperElem = wrapper[0]
          this.wrapperElem.addEventListener('click', this.hideCusBtn)
        }
      })
    },
    methods: {
      /**
       * @description: closeViewer
       * @author: renchao
       */
      closeViewer () {
        this.$emit('close-viewer')
      },
      /**
       * @description: hideCusBtn
       * @param {*} e
       * @author: renchao
       */
      hideCusBtn (e) {
        let className = e.target.className
        if (className === 'el-icon-download') {
          let imgUrl = document.getElementsByClassName(
            'el-image-viewer__canvas'
          )[0].children[0].src
          this.downloadImage(imgUrl)
        }
      },
      /**
       * @description: downloadImage
       * @param {*} imgUrl
       * @author: renchao
       */
      downloadImage (imgUrl) {
        let tmpArr = imgUrl.split('/')
        let fileName = tmpArr[tmpArr.length - 1]
        window.URL = window.URL || window.webkitURL
        let xhr = new XMLHttpRequest()
        xhr.open('get', imgUrl, true)
        xhr.responseType = 'blob'
        xhr.onload = function () {
          if (this.status == 200) {
            //得到一个blob对象
            let blob = this.response
            let fileUrl = window.URL.createObjectURL(blob)
            let a = document.createElement('a')
              ; (document.body || document.documentElement).appendChild(a)
            a.href = fileUrl
            if ('download' in a) {
              a.download = fileName
            } else {
              a.setAttribute('download', fileName)
            }
            a.target = '_self'
            a.click()
            a.remove()
任超 committed
93 94
          }
        }
95 96
        xhr.send()
      },
任超 committed
97
    },
98
  }
任超 committed
99 100 101
</script>

<style lang="scss" scoped>
102 103 104
  /deep/ .el-image-viewer__close {
    color: #ffffff;
  }
yuanbo committed
105
</style>