upload-button.vue 1.3 KB
Newer Older
wangcong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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
<template>
  <el-upload
    class="upload-demo"
    :on-change="handleFileChange"
    :http-request="handleClick"
    :on-remove="handleRemove"
    :file-list="fileList.map(o => ({name: 'file', url: o}))"
    list-type="picture"
    :limit="1"
  >
    <Button slot="trigger" size="small">上传</Button>
  </el-upload>
</template>

<script>
export default {
  props: {
    fileList: {
      type: Array,
      default: () => [],
    },
    typeCode: {
      type: String,
      default: '0',
    },
  },
  data() {
    return {
      file: null,
    }
  },
  computed: {
  },
  methods: {
    handleRemove() {
      this.$emit('on-remove')
    },
    handleFileChange(file) {
      this.file = file
    },
    handleClick() {
      if (!this.file) return
      const param = new FormData()
      param.append('file', this.file.raw)
      param.append('typeCode', this.typeCode)
      window.GMS.$http.post(
        `/homepage/uploadImage`,
        param,
        {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        }
      ).then((res) => {
        const id = res.data && res.data.data && res.data.data[0]
        if (!id) console.log('upload failed')
        else {
          this.$emit('on-success', id)
        }
      })
      return false
    },
  },
}
</script>

<style>

</style>