Blame view

antis-ncc-admin/src/components/NCC-uploadBtn/index.vue 2.66 KB
96009bc9   hexiaodong   hxd
1
2
3
  <template>
    <el-upload :action="define.comUrl+url" :headers="{ Authorization: $store.getters.token}"
      :on-success="handleSuccess" :before-upload="beforeUpload" :show-file-list="false"
7606a6ad   “wangming”   fix: update produ...
4
      class="upload-btn" :http-request="customUpload">
96009bc9   hexiaodong   hxd
5
6
7
8
9
10
      <el-button :type="buttonType" icon="el-icon-upload2" :loading="loading">{{buttonText}}
      </el-button>
    </el-upload>
  </template>
  
  <script>
7606a6ad   “wangming”   fix: update produ...
11
  import { ossUploadFile } from '@/utils/oss-upload'
96009bc9   hexiaodong   hxd
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  export default {
    name: 'NCC-uploadBtn',
    data() {
      return {
        loading: false
      }
    },
    props: {
      url: {
        type: String,
        default: ""
      },
      buttonText: {
        type: String,
        default: '导入'
      },
      buttonType: {
        type: String,
        default: 'text'
      }
    },
    methods: {
7606a6ad   “wangming”   fix: update produ...
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
      customUpload(options) {
        var file = options.file
        this.loading = true
        if (this.url.indexOf('annexpic') > -1 || this.url.indexOf('attendancepic') > -1) {
          var ossType = this.url.indexOf('attendancepic') > -1 ? 'attendancepic' : 'annexpic'
          ossUploadFile(file, ossType).then(data => {
            this.loading = false
            this.$message({ message: '上传成功', type: 'success', duration: 1000 })
            this.$emit('on-success')
          }).catch(() => {
            this.loading = false
            this.$message({ message: '上传失败,请重试', type: 'error', duration: 1000 })
          })
        } else {
          var formData = new FormData()
          formData.append('file', file)
          var xhr = new XMLHttpRequest()
          xhr.open('POST', this.define.comUrl + this.url, true)
          xhr.setRequestHeader('Authorization', this.$store.getters.token)
          xhr.onload = () => {
            if (xhr.status === 200) {
              var res = JSON.parse(xhr.responseText)
              this.handleSuccess(res)
            } else {
              this.loading = false
              this.$message({ message: '上传失败,请重试', type: 'error', duration: 1000 })
            }
          }
          xhr.onerror = () => {
            this.loading = false
            this.$message({ message: '上传失败,请重试', type: 'error', duration: 1000 })
          }
          xhr.send(formData)
        }
      },
96009bc9   hexiaodong   hxd
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
      beforeUpload() {
        this.loading = true
      },
      handleSuccess(res) {
        this.loading = false
        if (res.code == 200) {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1000
          })
          this.$emit('on-success')
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            duration: 1000
          })
        }
      }
    }
  };
  </script>
  <style lang="scss" scoped>
  .upload-btn {
    display: inline-block;
    margin: 0 10px;
  }
  </style>