<template>
  <img ref="img" :style="{'width': width +'px','height':height +'px'}" style="border-radius: 4px;"></img>
</template>
<script>
import { getToken } from '@/utils/auth'
export default {
  name: "AuthImg",
  props: {
    authSrc: {
      type: String,
      default: "",
    },
    width: {
      type: Number,
      default: 80,
    },
    height: {
      type: Number,
      default: 78,
    },
  },
  mounted () {
    let token = getToken()
    Object.defineProperty(Image.prototype, 'authsrc', {
      writable: true,
      enumerable: true,
      configurable: true
    })
    let img = this.$refs.img
    let request = new XMLHttpRequest();
    request.responseType = 'blob';
    request.open('get', this.authSrc, true);
    request.setRequestHeader('Authorization', 'Bearer '+token);  //根据后端需求添加的权限标志
    request.onreadystatechange = e => {
      if (request.readyState == XMLHttpRequest.DONE && request.status == 200) {
        img.src = URL.createObjectURL(request.response);
        img.onload = () => {
          URL.revokeObjectURL(img.src);
        }
      }
    };
    request.send(null);
  }
}
</script>
