fujianList copy 2.vue
7.86 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
<template>
<el-upload
:auto-upload="false" multiple :action="uploadFileUrl" :accept="fileType" :on-change="handleUpload":on-success="handleUploadSuccess"
:limit="limit" :on-error="handleUploadError" :on-exceed="handleExceed" :on-remove="handleRemove" :headers="headers" :file-list="fileList"
:on-preview="handlePictureCardPreview" :class="{hide: this.fileList.length >= this.limit}">
<div style="display: flex;align-items: center;">
<el-button v-if="fileList.length < limit" style="background-color: #fff;color: #000;border: 1px solid #dcdfe6;" class="buttonHover">
<div style="display: flex;align-items: center;">
<img src="@/assets/images/upload.png" style="width: 16px;height: 16px;margin-right: 4px;" alt="">
<span style="">上传附件</span>
</div>
</el-button>
<div v-if="fileList.length < limit" slot="tip" class="tips" style="margin-left: 10px;">不超过{{ fileSize }}MB</div>
</div>
</el-upload>
</template>
<script>
import {
miniioupload
} from '@/api/commodityLease.js'
// import { getAccessToken } from '@/utils/auth'
import {
uploadUrl
} from '@/utils/request'
export default {
props: {
value: [String, Object, Array],
// 图片数量限制
limit: {
type: Number,
default: 1
},
filePath: {
type: String,
default: 'other'
},
// 大小限制(MB)
fileSize: {
type: Number,
default: 5
},
// 文件类型, 例如['png', 'jpg', 'jpeg']
fileType: {
type: String,
default:'.bmp,.jpg,.jpeg,.png',
},
// 是否显示提示
isShowTip: {
type: Boolean,
default: true
}
},
data() {
return {
host: '',
number: 0,
uploadList: [],
dialogImageUrl: '',
dialogVisible: false,
hideUpload: false,
uploadFileUrl: uploadUrl, // 请求地址
headers: {
Authorization: ''
}, // 设置上传的请求头部
fileList: [],
}
},
computed: {
// 是否显示提示
showTip() {
return this.isShowTip && (this.fileType || this.fileSize)
}
},
watch: {
value: {
handler(val) {
if (val) {
// 首先将值转为数组
this.uploadList = []
const list = Array.isArray(val) ? val : this.value.split(',')
// 然后将数组转为对象数组
for (const item of list) {
if (typeof item === 'string') {
let info = {
name: this.$baseURL+item,
url: this.$baseURL+item
}
this.uploadList.push(item)
this.fileList.push(info)
}
}
} else {
this.uploadList = []
this.fileList = []
return []
}
},
deep: true,
immediate: true
}
},
methods: {
checkFileType(file) {
console.error(file)
let {
accept
} = this.options;
if (!accept || accept === '*') {
return true;
}
accept = accept.toLowerCase().replace(/\s/g, '');
const acceptTypeList = accept.split(',');
const fileType = file.name.match(/\.\w*$/)?.shift() ?? '';
return acceptTypeList.includes(fileType.toLowerCase());
},
checkSize({
size
}) {
return size <= this.options.limitSize * 1024 * 1024;
},
filesCheck(files) {
const limit = this.limit;
const limitSize = this.fileSize;
const accept = this.fileType;
if (this.uploadList.length + files.length > limit) {
this.$message.error(`最多上传${limit}个文件`);
return false;
}
for (let index = 0; index < files.length; index++) {
const file = files[index];
if (!this.checkFileType(file)) {
this.$message.error(`请上传${accept}等格式`);
return false;
}
if (!this.checkSize(file)) {
this.$message.error(`文件大小不能超过 ${limitSize}MB!`);
return false;
}
}
return true;
},
handleUpload(response) {
// if (!this.filesCheck(response)) {
// return;
// }
// console.error(response)
// return
let fd = new FormData()
fd.append('file', response.raw)
fd.append('filePath',this.filePath)
miniioupload(fd).then(res => {
console.error(res)
const fileMsg = {
name: response.name,
url: this.$baseURL + res.data,
}
this.fileList.push(fileMsg)
this.uploadList.push(res.data)
this.$emit('changimg',this.uploadList.join(','))
})
},
// 删除图片
handleRemove(file, fileList) {
this.fileList = []
this.uploadList = []
this.$emit('changimg',this.uploadList.join(','))
// const findex = this.fileList.map(f => f.name).indexOf(file.name)
// if (findex > -1) {
// this.fileList.splice(findex, 1)
// this.uploadList.splice(findex, 1)
// }
},
// 上传成功回调
handleUploadSuccess(res) {
this.uploadList.push({
name: res.data.url,
url: res.data.url
})
console.log(this.uploadList)
if (this.uploadList.length === this.number) {
this.fileList = this.fileList.concat(this.uploadList)
this.uploadList = []
this.number = 0
this.$emit('input', this.listToString(this.fileList))
// this.$modal.closeLoading()
}
},
// 上传前loading加载
handleBeforeUpload(file) {
// let isImg = false
// if (this.fileType.length) {
// let fileExtension = ''
// if (file.name.lastIndexOf('.') > -1) {
// fileExtension = file.name.slice(file.name.lastIndexOf('.') + 1)
// }
// isImg = this.fileType.some(type => {
// if (file.type.indexOf(type) > -1) return true
// if (fileExtension && fileExtension.indexOf(type) > -1) return true
// return false
// })
// } else {
// isImg = file.type.indexOf('image') > -1
// }
// if (!isImg) {
// this.$message.error(`文件格式不正确, 请上传${this.fileType.join('/')}图片格式文件!`)
// return false
// }
// if (this.fileSize) {
// const isLt = file.size / 1024 / 1024 < this.fileSize
// if (!isLt) {
// this.$message.error(`上传头像图片大小不能超过 ${this.fileSize} MB!`)
// return false
// }
// }
// this.$message.warning('正在上传图片,请稍候...')
// this.number++
},
// 文件个数超出
handleExceed() {
this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`)
},
// 上传失败
handleUploadError() {
this.$message.error('上传图片失败,请重试')
// this.$modal.closeLoading()
},
// 预览
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url
this.dialogVisible = true
},
// 对象转成指定字符串分隔
listToString(list, separator) {
let strs = ''
separator = separator || ','
for (const i in list) {
strs += list[i].url.replace(this.baseUrl, '') + separator
}
return strs !== '' ? strs.substr(0, strs.length - 1) : ''
}
}
}
</script>
<style lang="scss" scoped>
::v-deep .el-upload {
text-align: left !important;
}
.tips {
color: #0006;
font: 14px 'Alibaba PuHuiTi 2.0-55 Regular';
line-height: 38px;
}
</style>