uni-im-img.vue 3.2 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
  <image @load="load" :src="url" :mode="mode" :style='{"width":viewWidth,"height":viewHeight}' @click="handleClick"></image>
DCloud_JSON's avatar
DCloud_JSON 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
</template>

<script>
import uniIm from '@/uni_modules/uni-im/sdk/index.js';
  export default {
    emits: ['click'],
    props: {
      src: {
        type: String,
        default: ''
      },
      mode: {
        type: String,
        default: ''
      },
      maxWidth: {
19
        type: [String,Number],
DCloud_JSON's avatar
DCloud_JSON 已提交
20 21
        default: false
      },
22 23 24 25 26 27 28 29 30 31 32 33
      maxHeight: {
        type: [String,Number],
        default: ''
      },
      width: {
        type: [String,Number],
        default: ''
      },
      height: {
        type: [String,Number],
        default: ''
      }
DCloud_JSON's avatar
DCloud_JSON 已提交
34 35 36
    },
    data() {
      return {
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
        url: '',
        viewWidth: '',
        viewHeight: ''
      }
    },
    mounted() {
      // console.log('beforeCreate')
      if( this.width && this.height){
        // 根据maxWidth 和 maxWidth,在保持宽高比例不变的情况下 计算viewWidth 和 viewHeight
        const maxWidth = this.toPx(this.maxWidth)
        const maxHeight = this.toPx(this.maxHeight)
        let width = this.toPx(this.width)
        let height = this.toPx(this.height)
        
        // 如果宽超过最大宽度,高度按比例缩小
        if(width > maxWidth){
          height = height * maxWidth / width
          width = maxWidth
        }
        // 如果高超过最大高度,宽度按比例缩小
        if(height > maxHeight){
          width = width * maxHeight / height
          height = maxHeight
        }
        this.viewWidth = width + 'px'
        this.viewHeight = height + 'px'
DCloud_JSON's avatar
DCloud_JSON 已提交
63 64 65 66 67 68 69 70 71 72 73 74
      }
    },
    watch: {
       src:{
        async handler(src) {
          if(src){
            this.url = await uniIm.utils.getTempFileURL(src)
            // src 是cloud://开头的云存储地址,是腾讯云,否则是阿里云
            if(src.indexOf('cloud://') === 0){
              this.url += '?imageMogr2/thumbnail/400x400>'
            }else if(src.indexOf('http') === 0){
              // 因为还可能是 base64 blob 的本地图片,所以这里判断是不是http开头的
75
              this.url += '?x-oss-process=image/resize,w_200/quality,q_80'
DCloud_JSON's avatar
DCloud_JSON 已提交
76 77 78 79 80 81 82 83 84
            }
          }
        },
        immediate: true
      }
    },
    methods: {
      load(e){
        // console.log('img load',e)
85 86 87 88 89 90 91 92 93 94 95 96
        // TODO:渲染后再设置宽高,为兼容旧版系统,图片消息中没有带宽高的情况
        if(!this.width || !this.height){
          let width = e.detail.width
          let maxWidth = this.toPx(this.maxWidth)
          if(maxWidth && width > maxWidth){
            this.viewWidth = maxWidth + 'px'
            this.viewHeight = maxWidth * e.detail.height / e.detail.width + 'px'
            // console.error('超了',this.width,this.height)
          }else{
            this.viewWidth = e.detail.width + 'px'
            this.viewHeight = e.detail.height + 'px'
          }
DCloud_JSON's avatar
DCloud_JSON 已提交
97 98 99 100
        }
      },
      handleClick(){
        this.$emit('click')
101 102 103 104 105 106 107
      },
      // 如果是rpx单位,转换为px
      toPx(val){
        if(typeof val === 'string' && val.indexOf('rpx') > -1){
          return parseInt(val) * uniIm.systemInfo.windowWidth / 750
        }
        return parseInt(val)
DCloud_JSON's avatar
DCloud_JSON 已提交
108 109 110 111 112 113 114
      }
    }
  }
</script>

<style>
</style>