Blame view

antis-ncc-admin/src/components/Upload/SingleImg.vue 2.49 KB
96009bc9   hexiaodong   hxd
1
2
3
  <template>
    <div class="SingleImg-container">
      <el-upload class="avatar-uploader" :action="define.comUploadUrl+'/'+type"
7606a6ad   “wangming”   fix: update produ...
4
5
        :show-file-list="false" :on-success="handleSuccess" :headers="uploadHeaders" accept="image/*"
        :http-request="customUpload">
96009bc9   hexiaodong   hxd
6
7
8
9
10
11
12
13
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </div>
  </template>
  
  <script>
  import { getDownloadUrl } from '@/api/common'
7606a6ad   “wangming”   fix: update produ...
14
  import { ossUploadFile } from '@/utils/oss-upload'
96009bc9   hexiaodong   hxd
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  export default {
    name: 'SingleImg',
    props: {
      value: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: 'workFlow'
      },
    },
    data() {
      return {
        imageUrl: '',
        uploadHeaders: { Authorization: this.$store.getters.token }
      }
    },
    watch: {
      value(val) { if (!val) this.imageUrl = '' }
    },
    methods: {
7606a6ad   “wangming”   fix: update produ...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
      customUpload(options) {
        var file = options.file
        if (this.type === 'annexpic' || this.type === 'attendancepic') {
          ossUploadFile(file, this.type).then(data => {
            this.imageUrl = URL.createObjectURL(file)
            this.$emit('input', data.name)
          }).catch(() => {
            this.$message({ message: '上传失败,请重试', type: 'error', duration: 1500 })
          })
        } else {
          var formData = new FormData()
          formData.append('file', file)
          var xhr = new XMLHttpRequest()
          xhr.open('POST', this.define.comUploadUrl + '/' + this.type, true)
          xhr.setRequestHeader('Authorization', this.$store.getters.token)
          xhr.onload = () => {
            if (xhr.status === 200) {
              var res = JSON.parse(xhr.responseText)
              this.handleSuccess(res, { raw: file })
            }
          }
          xhr.send(formData)
        }
      },
96009bc9   hexiaodong   hxd
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
      handleSuccess(res, file) {
        if (res.code == 200) {
          this.imageUrl = URL.createObjectURL(file.raw);
          this.$emit('input', res.data.name)
        } else {
          this.$message({ message: res.msg, type: 'error', duration: 1500 })
        }
      }
    }
  }
  </script>
  <style lang="scss" scoped>
  .avatar-uploader >>> .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
  </style>