提交 | 用户 | 时间
|
0ca254
|
1 |
<template> |
A |
2 |
<div class="component-upload-image"> |
|
3 |
<el-upload |
|
4 |
multiple |
|
5 |
:action="uploadImgUrl" |
|
6 |
list-type="picture-card" |
|
7 |
:on-success="handleUploadSuccess" |
|
8 |
:before-upload="handleBeforeUpload" |
|
9 |
:limit="limit" |
|
10 |
:on-error="handleUploadError" |
|
11 |
:on-exceed="handleExceed" |
|
12 |
ref="imageUpload" |
|
13 |
:on-remove="handleDelete" |
|
14 |
:show-file-list="true" |
|
15 |
:headers="headers" |
|
16 |
:file-list="fileList" |
|
17 |
:on-preview="handlePictureCardPreview" |
|
18 |
:class="{hide: this.fileList.length >= this.limit}" |
|
19 |
> |
|
20 |
<i class="el-icon-plus"></i> |
|
21 |
</el-upload> |
|
22 |
|
|
23 |
<!-- 上传提示 --> |
|
24 |
<div class="el-upload__tip" slot="tip" v-if="showTip"> |
|
25 |
请上传 |
|
26 |
<template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template> |
|
27 |
<template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template> |
|
28 |
的文件 |
|
29 |
</div> |
|
30 |
|
|
31 |
<el-dialog |
|
32 |
:visible.sync="dialogVisible" |
|
33 |
title="预览" |
|
34 |
width="800" |
|
35 |
append-to-body |
|
36 |
> |
|
37 |
<img |
|
38 |
:src="dialogImageUrl" |
|
39 |
style="display: block; max-width: 100%; margin: 0 auto" |
|
40 |
/> |
|
41 |
</el-dialog> |
|
42 |
</div> |
|
43 |
</template> |
|
44 |
|
|
45 |
<script> |
|
46 |
import { getToken } from "@/utils/auth"; |
|
47 |
|
|
48 |
export default { |
|
49 |
props: { |
|
50 |
value: [String, Object, Array], |
|
51 |
// 图片数量限制 |
|
52 |
limit: { |
|
53 |
type: Number, |
|
54 |
default: 5, |
|
55 |
}, |
|
56 |
// 大小限制(MB) |
|
57 |
fileSize: { |
|
58 |
type: Number, |
|
59 |
default: 5, |
|
60 |
}, |
|
61 |
// 文件类型, 例如['png', 'jpg', 'jpeg'] |
|
62 |
fileType: { |
|
63 |
type: Array, |
|
64 |
default: () => ["png", "jpg", "jpeg"], |
|
65 |
}, |
|
66 |
// 是否显示提示 |
|
67 |
isShowTip: { |
|
68 |
type: Boolean, |
|
69 |
default: true |
|
70 |
} |
|
71 |
}, |
|
72 |
data() { |
|
73 |
return { |
|
74 |
number: 0, |
|
75 |
uploadList: [], |
|
76 |
dialogImageUrl: "", |
|
77 |
dialogVisible: false, |
|
78 |
hideUpload: false, |
|
79 |
baseUrl: process.env.VUE_APP_BASE_API, |
|
80 |
uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 |
|
81 |
headers: { |
|
82 |
Authorization: "Bearer " + getToken(), |
|
83 |
}, |
|
84 |
fileList: [] |
|
85 |
}; |
|
86 |
}, |
|
87 |
watch: { |
|
88 |
value: { |
|
89 |
handler(val) { |
|
90 |
if (val) { |
|
91 |
// 首先将值转为数组 |
|
92 |
const list = Array.isArray(val) ? val : this.value.split(','); |
|
93 |
// 然后将数组转为对象数组 |
|
94 |
this.fileList = list.map(item => { |
|
95 |
if (typeof item === "string") { |
|
96 |
if (item.indexOf(this.baseUrl) === -1) { |
|
97 |
item = { name: this.baseUrl + item, url: this.baseUrl + item }; |
|
98 |
} else { |
|
99 |
item = { name: item, url: item }; |
|
100 |
} |
|
101 |
} |
|
102 |
return item; |
|
103 |
}); |
|
104 |
} else { |
|
105 |
this.fileList = []; |
|
106 |
return []; |
|
107 |
} |
|
108 |
}, |
|
109 |
deep: true, |
|
110 |
immediate: true |
|
111 |
} |
|
112 |
}, |
|
113 |
computed: { |
|
114 |
// 是否显示提示 |
|
115 |
showTip() { |
|
116 |
return this.isShowTip && (this.fileType || this.fileSize); |
|
117 |
}, |
|
118 |
}, |
|
119 |
methods: { |
|
120 |
// 上传前loading加载 |
|
121 |
handleBeforeUpload(file) { |
|
122 |
let isImg = false; |
|
123 |
if (this.fileType.length) { |
|
124 |
let fileExtension = ""; |
|
125 |
if (file.name.lastIndexOf(".") > -1) { |
|
126 |
fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1); |
|
127 |
} |
|
128 |
isImg = this.fileType.some(type => { |
|
129 |
if (file.type.indexOf(type) > -1) return true; |
|
130 |
if (fileExtension && fileExtension.indexOf(type) > -1) return true; |
|
131 |
return false; |
|
132 |
}); |
|
133 |
} else { |
|
134 |
isImg = file.type.indexOf("image") > -1; |
|
135 |
} |
|
136 |
|
|
137 |
if (!isImg) { |
|
138 |
this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`); |
|
139 |
return false; |
|
140 |
} |
|
141 |
if (this.fileSize) { |
|
142 |
const isLt = file.size / 1024 / 1024 < this.fileSize; |
|
143 |
if (!isLt) { |
|
144 |
this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`); |
|
145 |
return false; |
|
146 |
} |
|
147 |
} |
|
148 |
this.$modal.loading("正在上传图片,请稍候..."); |
|
149 |
this.number++; |
|
150 |
}, |
|
151 |
// 文件个数超出 |
|
152 |
handleExceed() { |
|
153 |
this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`); |
|
154 |
}, |
|
155 |
// 上传成功回调 |
|
156 |
handleUploadSuccess(res, file) { |
|
157 |
if (res.code === 200) { |
|
158 |
this.uploadList.push({ name: res.fileName, url: res.fileName }); |
|
159 |
this.uploadedSuccessfully(); |
|
160 |
} else { |
|
161 |
this.number--; |
|
162 |
this.$modal.closeLoading(); |
|
163 |
this.$modal.msgError(res.msg); |
|
164 |
this.$refs.imageUpload.handleRemove(file); |
|
165 |
this.uploadedSuccessfully(); |
|
166 |
} |
|
167 |
}, |
|
168 |
// 删除图片 |
|
169 |
handleDelete(file) { |
|
170 |
const findex = this.fileList.map(f => f.name).indexOf(file.name); |
|
171 |
if (findex > -1) { |
|
172 |
this.fileList.splice(findex, 1); |
|
173 |
this.$emit("input", this.listToString(this.fileList)); |
|
174 |
} |
|
175 |
}, |
|
176 |
// 上传失败 |
|
177 |
handleUploadError() { |
|
178 |
this.$modal.msgError("上传图片失败,请重试"); |
|
179 |
this.$modal.closeLoading(); |
|
180 |
}, |
|
181 |
// 上传结束处理 |
|
182 |
uploadedSuccessfully() { |
|
183 |
if (this.number > 0 && this.uploadList.length === this.number) { |
|
184 |
this.fileList = this.fileList.concat(this.uploadList); |
|
185 |
this.uploadList = []; |
|
186 |
this.number = 0; |
|
187 |
this.$emit("input", this.listToString(this.fileList)); |
|
188 |
this.$modal.closeLoading(); |
|
189 |
} |
|
190 |
}, |
|
191 |
// 预览 |
|
192 |
handlePictureCardPreview(file) { |
|
193 |
this.dialogImageUrl = file.url; |
|
194 |
this.dialogVisible = true; |
|
195 |
}, |
|
196 |
// 对象转成指定字符串分隔 |
|
197 |
listToString(list, separator) { |
|
198 |
let strs = ""; |
|
199 |
separator = separator || ","; |
|
200 |
for (let i in list) { |
|
201 |
if (list[i].url) { |
|
202 |
strs += list[i].url.replace(this.baseUrl, "") + separator; |
|
203 |
} |
|
204 |
} |
|
205 |
return strs != '' ? strs.substr(0, strs.length - 1) : ''; |
|
206 |
} |
|
207 |
} |
|
208 |
}; |
|
209 |
</script> |
|
210 |
<style scoped lang="scss"> |
|
211 |
// .el-upload--picture-card 控制加号部分 |
|
212 |
::v-deep.hide .el-upload--picture-card { |
|
213 |
display: none; |
|
214 |
} |
|
215 |
// 去掉动画效果 |
|
216 |
::v-deep .el-list-enter-active, |
|
217 |
::v-deep .el-list-leave-active { |
|
218 |
transition: all 0s; |
|
219 |
} |
|
220 |
|
|
221 |
::v-deep .el-list-enter, .el-list-leave-active { |
|
222 |
opacity: 0; |
|
223 |
transform: translateY(0); |
|
224 |
} |
|
225 |
</style> |
|
226 |
|