提交 | 用户 | age
|
ccad61
|
1 |
<template> |
C |
2 |
<div> |
|
3 |
<!-- 上传图片组件(多图) --> |
|
4 |
<el-upload |
30a172
|
5 |
id="UploadMultipleImg" |
ccad61
|
6 |
ref="refUploadImg" |
C |
7 |
:accept="accept" |
|
8 |
:auto-upload="false" |
|
9 |
list-type="picture-card" |
|
10 |
:class="{disabled:uploadDisabled}" |
|
11 |
action="#" |
|
12 |
:limit="limit" |
30a172
|
13 |
drag |
bf9798
|
14 |
multiple |
ccad61
|
15 |
:file-list="uploadImgs" |
C |
16 |
:on-change="addUploadImg" |
|
17 |
:on-remove="delUploadImg" |
|
18 |
:http-request="uploadImg" |
|
19 |
:on-preview="uploadPreview" |
|
20 |
:before-upload="beforeUploadImg" |
|
21 |
size="mini" |
|
22 |
> |
|
23 |
<i class="el-icon-plus" /> |
|
24 |
</el-upload> |
|
25 |
<!-- upload放大图片 --> |
|
26 |
<el-dialog :visible.sync="uploadPreviewVisible" append-to-body style="text-align:center"> |
|
27 |
<img style="max-width:100%" :src="uploadPreviewUrl" alt=""> |
|
28 |
</el-dialog> |
|
29 |
</div> |
|
30 |
</template> |
|
31 |
|
|
32 |
<script> |
|
33 |
import mixin_upload from '@/mixins/upload.js' // 通用上传图片预览 |
|
34 |
export default { |
|
35 |
mixins: [mixin_upload], |
|
36 |
model: { |
|
37 |
prop: 'uploadImgs', |
|
38 |
event: 'change' |
|
39 |
}, |
|
40 |
props: { |
|
41 |
uploadImgs: { |
|
42 |
type: Array, |
|
43 |
default: () => { |
|
44 |
return [] |
|
45 |
} |
|
46 |
}, |
|
47 |
accept: { |
|
48 |
type: String, |
|
49 |
default: () => { |
|
50 |
return 'image/jpeg,image/jpg,image/gif,image/png' |
|
51 |
} |
|
52 |
}, |
|
53 |
limit: { |
|
54 |
type: Number, |
|
55 |
default: 9 |
|
56 |
} |
|
57 |
}, |
|
58 |
|
|
59 |
data() { |
|
60 |
return { |
|
61 |
callback: null |
|
62 |
} |
|
63 |
}, |
|
64 |
computed: { |
|
65 |
uploadDisabled() { |
|
66 |
return this.uploadImgs.length >= this.limit |
|
67 |
} |
|
68 |
}, |
|
69 |
|
|
70 |
methods: { |
|
71 |
// 上传图片 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ |
|
72 |
// 上传前确认格式 |
|
73 |
beforeUploadImg(file) { |
|
74 |
const isImg = file.type.indexOf('image/') > -1 |
|
75 |
const isTooLarge = file.size / 1024 / 1024 > 4 |
|
76 |
let flag = true |
|
77 |
if (!isImg) { |
|
78 |
this.$message.error('必须是选择图片文件') |
|
79 |
flag = false |
|
80 |
} else if (isTooLarge) { |
|
81 |
this.$message.error('请勿上传大于4MB的图片') |
|
82 |
flag = false |
|
83 |
} |
|
84 |
return flag |
|
85 |
}, |
|
86 |
// 增加图片 |
|
87 |
addUploadImg(file, fileList) { |
|
88 |
this.$emit('change', fileList) |
|
89 |
}, |
|
90 |
// 删除图片 |
|
91 |
delUploadImg(file, fileList) { |
|
92 |
this.$emit('change', fileList) |
|
93 |
}, |
|
94 |
// 执行上传商品图 |
|
95 |
runUploadImg(suc_cb) { |
|
96 |
console.log('runUploadImg!!!!!') |
|
97 |
if (this.checkNeedUpload(this.uploadImgs)) { |
|
98 |
this.callback = suc_cb |
|
99 |
this.$refs.refUploadImg.submit() |
|
100 |
} else { |
d4d2d4
|
101 |
suc_cb && suc_cb(this.uploadImgs) |
ccad61
|
102 |
} |
C |
103 |
}, |
|
104 |
// 上传商品图 |
|
105 |
uploadImg(res) { |
|
106 |
console.log(res) |
|
107 |
const file = res.file |
|
108 |
const formData = new FormData() |
|
109 |
formData.append('file', file) |
|
110 |
console.log(formData) |
|
111 |
this.postFN({ |
|
112 |
url: 'admin/image/upload?folderCode=IMG', |
|
113 |
header: { 'Content-Type': 'multipart/form-data' }, |
|
114 |
params: formData, |
|
115 |
mockData: { |
|
116 |
code: 100, |
|
117 |
msg: '', |
|
118 |
data: { |
|
119 |
imgUrl: 'xxxx' |
|
120 |
} |
|
121 |
} |
|
122 |
}, (inf) => { |
|
123 |
console.log('上传图片成功') |
|
124 |
|
|
125 |
// 根据uid 替换掉未上传的图片 |
0e0365
|
126 |
var uploadImgs = JSON.parse(JSON.stringify(this.uploadImgs)) |
ccad61
|
127 |
uploadImgs.forEach((o, i) => { |
74c210
|
128 |
if (o.uid === res.file.uid) { |
L |
129 |
uploadImgs[i].url = inf.imgUrl |
|
130 |
uploadImgs[i].status = 'success' |
|
131 |
} |
ccad61
|
132 |
}) |
C |
133 |
|
|
134 |
// 触发change时间激活v-model |
|
135 |
this.$emit('change', uploadImgs) |
|
136 |
|
|
137 |
console.log('上传图片结束') |
|
138 |
|
|
139 |
// 上传结束 |
0e0365
|
140 |
if (!this.checkNeedUpload(uploadImgs)) { |
ccad61
|
141 |
console.log('上传轮播图结束') |
0e0365
|
142 |
this.callback && this.callback(uploadImgs) |
ccad61
|
143 |
} |
C |
144 |
}) |
|
145 |
} |
|
146 |
// 上传图片 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ |
|
147 |
} |
|
148 |
} |
|
149 |
</script> |
|
150 |
|
|
151 |
<style> |
|
152 |
.disabled .el-upload--picture-card { |
|
153 |
display: none; |
|
154 |
} |
|
155 |
.el-upload-list--picture-card{ |
|
156 |
line-height: 1; |
|
157 |
} |
|
158 |
.el-upload-list--picture-card .el-upload-list__item{ |
|
159 |
margin-bottom: 0 |
|
160 |
} |
30a172
|
161 |
#UploadMultipleImg .el-upload-dragger{ |
L |
162 |
width: auto; |
|
163 |
height: auto; |
|
164 |
} |
ccad61
|
165 |
</style> |