quanyawei
2023-09-27 ea9f9a9f6dc6dc88f3916d1d25a0cb3be436d1ba
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<template>
  <div>
    <el-upload
      :limit="limit"
      :action="uploadUrl"
      accept="image/*,video/*"
      :data="{sysCode}"
      :file-list="UrlList"
      :headers="{ token: token }"
      list-type="picture-card"
      :before-upload="before_upload"
      :on-change="handleImgChange"
      :on-success="handleUploadSuccess"
    >
      <i slot="default" class="el-icon-plus" />
      <div slot="file" slot-scope="{ file }">
        <img
          v-if="file.type == 1"
          class="el-upload-list__item-thumbnail"
          :src="file.url"
          alt=""
        >
        <video v-if="file.type == 2" :src="file.url" style="width: 100%">
          您的浏览器不支持 video 标签。
        </video>
        <audio
          v-if="file.type == 3"
          controls
          :src="file.url"
          style="width: 100%"
        >
          您的浏览器不支持该音频格式。
        </audio>
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in" />
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleImgRemove(file)"
          >
            <i class="el-icon-delete" />
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" width="600px" :modal-append-to-body="false" :destroy-on-close="true" @close="handleCancel">
      <div style="text-align: center;">
        <img
          v-if="dialogType == 1"
          class="el-upload-list__item-thumbnail"
          style="width: 300px;height: 500px"
          :src="dialogImageUrl"
          alt=""
        >
        <video
          v-if="dialogType == 2"
          ref="video"
          style="width: 300px;height: 500px"
          :src="dialogImageUrl"
          controls
          autoplay
        />
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
import { getToken } from '@/utils/auth'
import bus from '@/Bus'
export default {
  name: 'UploadExcel',
  props: {
    uploadUrl: {
      type: String,
      required: true
    },
    sysCode: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      token: getToken(),
      limit: 9, // 限制上传数量
      dialogImageUrl: null,
      dialogVisible: false,
      UrlList: [],
      disabled: false,
      dialogType: null,
      fileBaseList: [],
      fileChangeList: []
    }
  },
 
  methods: {
    handleCancel(e) {
      if (this.$refs.video) {
        this.$refs.video.pause()
      }
      this.dialogImageUrl = ''
      this.dialogVisible = false
    },
    // 上传前
    before_upload(file) {
      const fileSize = file.size
      const FIVE_M = 100 * 1024 * 1024
      // 不允许上传大于2M
      if (fileSize > FIVE_M) {
        this.$message.error('最大上传1002M')
        return false
      }
      const name = file.name.substring(file.name.lastIndexOf('.') + 1)
      const nameList = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'jpe', 'mp4', 'm2v', 'mkv']
      if (!nameList.includes(name)) {
        this.$message.error('只能上传图片和视频!')
        return false
      }
      return true
    },
 
    handlePictureCardPreview(file) {
      console.log('file', file)
      // const baseUrl = `${requestObj.baseUrl}/static/img/`
      this.dialogImageUrl = file.url
      console.log('this.dialogImageUrl', file)
      this.dialogType = file.type
      this.dialogVisible = true
    },
    // 图片改变
    handleImgChange(file, fileList) {
      bus.$emit('changeFileAfterList', 'change', fileList)
 
      this.UrlList = fileList
    },
    // 图片移除
    handleImgRemove(file, fileList) {
      fileList = this.UrlList
      const index = fileList.indexOf(file)
      fileList.splice(index, 1)
      bus.$emit('changeFileAfterList', 'detail', fileList)
    },
    // 图片上传成功
    handleUploadSuccess(response, file, fileList) {
      console.log(file)
      this.UrlList.push({
        url: file.url
      })
      bus.$emit('changeFileAfterList', 'success', fileList)
      file.type = this.matchType(file.response.data.fileName)
    },
 
    // 根据文件名后缀区分 文件类型
    /*
     * @param: fileName - 文件名称
     * @param: 数据返回 1) 无后缀匹配 - false
     * @param: 数据返回 2) 匹配图片 - image
     * @param: 数据返回 8) 匹配 视频 - video
     * @param: 数据返回 9) 匹配 音频 - radio
     * @param: 数据返回 10) 其他匹配项 - other
     */
    matchType(fileName) {
      // 后缀获取
      var suffix = ''
      // 获取类型结果
      var result = ''
      try {
        var flieArr = fileName.split('.')
        suffix = flieArr[flieArr.length - 1]
      } catch (err) {
        suffix = ''
      }
      // fileName无后缀返回 false
      if (!suffix) {
        result = false
        return result
      }
      // 图片格式
      var imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'jpe']
      // 进行图片匹配
      result = imglist.some(function(item) {
        return item === suffix
      })
      if (result) {
        result = '1'
        return result
      }
      // 匹配 视频
      var videolist = ['mp4', 'm2v', 'mkv']
      result = videolist.some(function(item) {
        return item === suffix
      })
      if (result) {
        result = '2'
        return result
      }
      // 匹配 音频
      var radiolist = ['mp3', 'wav', 'wmv', 'flac']
      result = radiolist.some(function(item) {
        return item === suffix
      })
      if (result) {
        result = '3'
        return result
      }
      // 其他 文件类型
      result = 'other'
      return result
    }
 
  }
}
</script>
<style lang="scss" scope>
.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: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  max-height: 148px;
  display: block;
}
.avatar-video {
  width: 200px;
  max-height: 200px;
  display: block;
}
</style>