Upload.vue
7.46 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<script>
import { upload } from '@/api/material'
import Video from 'video.js'
import BenzAMRRecorder from 'benz-amr-recorder'
export default {
name: 'Upload',
components: {},
props: {
fileUrl: {
type: String,
default: ''
},
fileName: {
type: String,
default: ''
},
// 0 图片(image)、1 语音(voice)、2 视频(video),3 普通文件(file)
type: {
type: String,
default: '0'
},
// 上传文件大小不能超过 maxSize MB
maxSize: {
type: Number,
default: 2
},
// 图片的宽高像素限制 [width(number), height(number)],默认null不限制
maxImgPx: {
type: Array,
default: null // () => [100, 100]
},
// 限制允许上传的文件格式 eg:["bmp", "jpg", "png", "jpeg", "gif"]
format: {
type: Array,
default: null
}
// beforeUpload: {
// type: Function,
// default: function() {
// return function() {}
// },
// },
},
data() {
return {
loading: false,
action: process.env.VUE_APP_BASE_API + '/common/uploadFile2Cos',
// (this.type == 0
// ? '/wecom/material/uploadimg'
// : '/common/uploadFile2Cos'),
headers: window.CONFIG.headers,
domain: process.env.VUE_APP_BASE_API
}
},
watch: {},
computed: {
accept() {
return ['image/*', 'amr/*', 'video/*', '*'][this.type]
}
},
created() {},
mounted() {},
methods: {
async handleBeforeUpload(file) {
this.loading = true
let isFormat = true
let isSize = true
if (this.format && this.format.length) {
// 校验格式
let reg = /\.(\w+)$/g
let match = file.name.match(reg)
let fileFormat = match && match[0].replace('.', '').toLowerCase()
if (!(isFormat = this.format.includes(fileFormat))) {
this.$message.error('文件格式不正确,请重新选择')
}
}
if (this.type === '0') {
// 图片
isFormat = file.type === 'image/jpeg' || file.type === 'image/png'
isSize = file.size / 1024 / 1024 < this.maxSize
if (!isFormat) {
this.$message.error('上传文件只能是 JPG 格式!')
}
if (!isSize) {
this.$message.error('上传文件大小不能超过 2MB!')
}
if (this.maxImgPx) {
try {
await new Promise((resolve) => {
let width, height
let image = new Image()
//加载图片获取图片真实宽度和高度
image.onload = () => {
width = image.width
height = image.height
if (width > this.maxImgPx[0]) {
isSize = false
this.$message.error('图片“宽”度超限,请重新选择')
} else if (height > this.maxImgPx[1]) {
this.$message.error('图片“高”度超限,请重新选择')
isSize = false
}
window.URL && window.URL.revokeObjectURL(image.src)
resolve()
}
if (window.URL) {
let url = window.URL.createObjectURL(file)
image.src = url
} else if (window.FileReader) {
let reader = new FileReader()
reader.onload = function (e) {
let data = e.target.result
image.src = data
}
reader.readAsDataURL(file)
}
})
} catch (e) {
console.error(e)
}
}
} else if (this.type === '1') {
// 语音
isFormat = /.amr$/gi.test(file.name)
isSize = file.size / 1024 / 1024 < 2
if (!isFormat) {
this.$message.error('上传文件只能是 amr 格式!')
}
if (!isSize) {
this.$message.error('上传文件大小不能超过 2MB!')
}
let amr = new BenzAMRRecorder()
try {
await amr.initWithBlob(file)
isSize = amr.getDuration() <= 60
if (!isSize) {
this.$message.error('上传文件时长不能超过 60秒!')
}
} catch (error) {
console.log(error)
this.$message.error('文件损坏')
}
} else if (this.type === '2') {
// 视频
isFormat = file.type === 'video/mp4'
isSize = file.size / 1024 / 1024 < 10
if (!isFormat) {
this.$message.error('上传文件只能是 mp4 格式!')
}
if (!isSize) {
this.$message.error('上传文件大小不能超过 10MB!')
}
} else if (this.type === '3') {
// 普通文件
isSize = file.size / 1024 / 1024 < 20
if (!isSize) {
this.$message.error('上传文件大小不能超过 20MB!')
}
}
if (!isFormat || !isSize) {
this.loading = false
}
// if (beforeUpload) {
// return beforeUpload(file)
// }
return (isFormat && isSize) || Promise.reject()
},
onSuccess(res, file) {
if (res.code === 200) {
this.loading = false
// this.$emit('update:fileUrl', res.data.materialUrl)
// this.$emit('update:fileName', res.data.materialName)
// if (this.type == 0) {
// res.url = res.data.url
// res.fileName = res.data.url
// }
this.$emit('update:fileUrl', res.data.url)
this.$emit('update:fileName', res.data.fileName)
this.$emit('update:file', file)
// this.fileUrl = URL.createObjectURL(file.raw)
} else {
this.loading = false
this.$message.error(res.msg)
}
},
onError(err, file, fileList) {
this.loading = false
this.$message.error('上传文件失败')
}
}
}
</script>
<template>
<div>
<el-upload
v-loading="loading"
element-loading-text="正在上传..."
class="uploader"
:accept="accept"
:action="action"
:headers="headers"
:data="{ mediaType: type }"
:show-file-list="false"
:on-success="onSuccess"
:on-error="onError"
:before-upload="handleBeforeUpload"
>
<slot>
<template v-if="fileUrl">
<img v-if="type === '0'" :src="fileUrl" class="upload-img" />
<div v-else-if="type === '2'">
<video
id="myVideo"
class="video-js vjs-default-skin vjs-big-play-centered"
width="100%"
controls
webkit-playsinline="true"
playsinline="true"
:autoplay="false"
preload="auto"
>
<source :src="fileUrl" type="video/mp4" />
</video>
</div>
<div v-else>{{ fileName }}</div>
</template>
<i v-else class="el-icon-plus uploader-icon"></i>
</slot>
</el-upload>
<div class="tip">
<slot name="tip"></slot>
</div>
</div>
</template>
<style lang="scss" scoped>
::v-deep.uploader {
display: inline-block;
.el-upload {
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
&:hover {
border-color: #409eff;
}
}
}
.uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
border-radius: 6px;
border: 1px dashed #d9d9d9;
}
.upload-img {
width: 178px;
height: 178px;
display: block;
}
.tip {
color: #aaa;
font-size: 12px;
}
</style>