yaozc
2023-03-20 d18c0bde988f8501ba84600f9d3d02f676a881e8
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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<template>
  <div>
    <el-upload
      ref="upload"
      class="upload-demo"
      :headers="headers"
      :action="fileUploadApi + '?name=' + new Date().getTime()"
      :file-list="fileList"
      :auto-upload="false"
      drag
      multiple
      :limit="limit"
      :on-change="handleChange"
      :on-exceed="handleExceed"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
      :on-error="handleError"
      :on-preview="handlePreview"
      :disabled="disabled"
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
      <div slot="tip" class="el-upload__tip" />
    </el-upload>
  </div>
</template>
 
<script>
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
export default {
  name: 'FileUpload',
  props: {
    fileUrl: {
      type: String,
      default() {
        return null
      }
    },
    limit: {
      type: Number,
      default() {
        return 1
      }
    },
    disabled: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      // 请求头
      headers: { 'Authorization': getToken() },
      // 文件列表
      fileList: []
    }
  },
  computed: {
    ...mapGetters([
      'fileUploadApi',
      'baseApi'
    ])
  },
  mounted() {
    // 判断文件路径参数是否为空
    if (this.fileUrl !== null) {
      // 文件路径数组
      const fileUrlList = this.fileUrl.split(',')
      // 循环文件路径数组
      for (let i = 0; i < fileUrlList.length; i++) {
        // 临时集合
        const tempOption = {}
        // 临时集合添加文件路径参数
        tempOption.url = this.baseApi + fileUrlList[i]
        tempOption.name = fileUrlList[i].substring(fileUrlList[i].lastIndexOf('/') + 1, fileUrlList[i].lastIndexOf('-')) + fileUrlList[i].substring(fileUrlList[i].lastIndexOf('.'), fileUrlList[i].length)
        // 文件列表添加临时集合
        this.fileList.push(tempOption)
      }
    }
  },
  methods: {
    // 改变事件
    handleChange(file, fileList) {
      // 文件列表赋值
      this.fileList = fileList
    },
    // 个数限制事件
    handleExceed(files, fileList) {
      this.$message.warning('文件个数超出上限,请先移除文件再上传!')
    },
    // 移除事件
    handleRemove(file, fileList) {
      // 文件列表赋值
      this.fileList = fileList
    },
    // 成功事件
    handleSuccess(response, file, fileList) {
      // 循环文件列表
      for (let i = 0; i < fileList.length; i++) {
        // 判断文件状态
        if (fileList[i].status === 'ready' || fileList[i].status === 'uploading') {
          // 终止
          return
        }
        // 判断循环是否结束
        if (i === fileList.length - 1) {
          // 向父页面发送参数:“fileInfo”是参数名,“fileList”是参数值。
          this.$emit('fileInfo', fileList)
        }
      }
    },
    // 失败事件
    handleError(err) {
      console.log(err)
    },
    // 点击事件
    handlePreview(file) {
      var a = document.createElement('a')
      var event = new MouseEvent('click')
      a.download = file.name
      a.href = file.url
      a.target = '_blank'
      a.dispatchEvent(event)
    },
    // 上传文件
    submit() {
      // 判断文件列表长度
      if (this.fileList.length === 0) {
        // 向父页面发送参数:“fileInfo”是参数名,“this.fileList”是参数值。
        this.$emit('fileInfo', this.fileList)
        // 终止
        return
      }
      // 循环文件列表
      for (let i = 0; i < this.fileList.length; i++) {
        // 判断文件状态
        if (this.fileList[i].status === 'ready') {
          // 将指定组件别名进行提交
          this.$refs.upload.submit()
          // 终止
          return
        }
        // 判断循环是否结束
        if (i === this.fileList.length - 1) {
          // 向父页面发送参数:“fileInfo”是参数名,“this.fileList”是参数值。
          this.$emit('fileInfo', this.fileList)
        }
      }
    }
  }
}
</script>
 
<style>
 
</style>