index.vue 2.6 KB
Newer Older
Q
qiang 已提交
1 2 3
<template>
  <uni-cover-image
    :src="src"
4
    :style="imageInfo"
fxy060608's avatar
fxy060608 已提交
5 6
    v-on="$listeners"
  >
Q
qiang 已提交
7 8
    <div
      ref="container"
fxy060608's avatar
fxy060608 已提交
9 10
      class="uni-cover-image"
    />
Q
qiang 已提交
11 12 13 14 15
  </uni-cover-image>
</template>
<script>
import native from '../../mixins/native'
import cover from '../../mixins/cover'
16 17 18 19 20 21
import {
  plusReady
} from 'uni-shared'
import {
  TEMP_PATH
} from '../../../service/api/constants'
Q
qiang 已提交
22 23 24 25 26 27 28 29

export default {
  name: 'CoverImage',
  mixins: [native, cover],
  props: {
    src: {
      type: String,
      default: ''
30 31 32 33
    },
    autoSize: {
      type: [Boolean, String],
      default: false
Q
qiang 已提交
34 35 36 37
    }
  },
  data () {
    return {
38 39 40
      coverType: 'image',
      coverContent: '',
      imageInfo: {}
Q
qiang 已提交
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 93 94 95 96 97 98 99 100 101 102 103
  watch: {
    src () {
      this.loadImage()
    }
  },
  created () {
    this.loadImage()
  },
  beforeDestroy () {
    const downloaTask = this.downloaTask
    if (downloaTask && downloaTask.state < 4) {
      downloaTask.abort()
    }
  },
  methods: {
    loadImage () {
      this.coverContent = ''
      this.imageInfo = this.autoSize ? { width: 0, height: 0 } : {}
      const realPath = this.src ? this.$getRealPath(this.src) : ''
      if (realPath.indexOf('http://') === 0 || realPath.indexOf('https://') === 0) {
        plusReady(() => {
          this.downloaTask = plus.downloader.createDownload(realPath, {
            filename: TEMP_PATH + '/download/'
          }, (task, status) => {
            if (status === 200) {
              this.getImageInfo(task.filename)
            } else {
              this.$trigger('error', {}, {
                errMsg: 'error'
              })
            }
          }).start()
        })
      } else if (realPath) {
        this.getImageInfo(realPath)
      }
    },
    getImageInfo (src) {
      this.coverContent = src
      plusReady(() => {
        plus.io.getImageInfo({
          src,
          success: ({ width, height }) => {
            if (this.autoSize) {
              this.imageInfo = {
                width: `${width}px`,
                height: `${height}px`
              }
              if (this._isMounted) {
                this._requestPositionUpdate()
              }
            }
            this.$trigger('load', {}, { width, height })
          },
          fail: () => {
            this.$trigger('error', {}, {
              errMsg: 'error'
            })
          }
        })
      })
Q
qiang 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    }
  }
}
</script>

<style>
uni-cover-image {
  display: block;
  line-height: 1.2;
  overflow: hidden;
  height: 100%;
  width: 100%;
  pointer-events: auto;
}

uni-cover-image[hidden] {
  display: none;
}

uni-cover-image .uni-cover-image {
  width: 100%;
  height: 100%;
}
</style>