index.vue
3.65 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
<template>
<div class="upload_img_wrap">
<el-upload
ref="pictures"
v-if="pictures.length<maxNum"
list-type="picture-card"
class="image-upload image-header"
:action='WEBURL+"/carservice/publicservice/common/uploadFile"'
header='Access-Control-Allow-Origin'
:show-file-list="false"
:on-success="uploadHeaderImageSuccess"
:on-error=" (value) => uploadError(value,'附件')"
:before-upload=" (value) => uploadImageChange(value,'附件')"
>
<span>
<i class="el-icon-plus image-upload-icon"></i>
</span>
</el-upload>
<template v-for="(item,index) in pictures">
<span
style="position:relative;display: inline-block;"
:key="index">
<i class="el-icon-remove red-cl" style="position:absolute;top:1px;right:5px;cursor:pointer;" @click="deleteImage(index)"></i>
<el-image
class="image-header-item"
:src="item"
:preview-src-list="pictures"
fit="contain">
</el-image>
</span>
</template>
</div>
</template>
<script>
import {
getBussinessApi,
getDictDataListByKey,
} from '@/api/system'
export default {
props: {
maxNum: Number,
beforeUpload: Function, // eslint-disable-line
onSuccess: Function// eslint-disable-line
},
data() {
return {
WEBURL:process.env.BASE_API,
pictures: [],
}
},
methods: {
uploadError(file,imageName){
this.$message.error(imageName+'上传失败!');
},
uploadImageChange(file,imageName) {
const regx = ["image/png","image/jpeg"]
const isJPG = regx.includes(file.type);
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上传的'+imageName+'只能是 JPG/PNG 格式!');
return false
}
if (!isLt2M) {
this.$message.error('上传的'+imageName+'大小不能超过 2MB!');
return false
}
return isJPG && isLt2M;
},
//业务
uploadHeaderImageSuccess(res, file) {
this.pictures.push(res.data);
this.$emit('update', this.pictures)
},
deleteImage(index){
getBussinessApi({'fileUrl':this.pictures[index]},'deleteFile','post').then(response => {
if(response&&response.code == 200){ //添加成功
this.pictures.splice(index,1)
this.$emit('update', this.pictures)
}else{
this.$message.error('照片删除失败,请稍后重试');
}
}).catch(error => {})
},
reset(){
this.pictures = []
}
}
}
</script>
<style lang="scss" scoped>
.upload_img_wrap{
.image-upload {
.el-upload {
border: 1px dashed #d9d9d9;
border-radius: 5px;
cursor: pointer;
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
.el-upload:hover {
border-color: #409EFF;
}
.image-upload-icon {
font-size: 28px;
color: #8c939d;
width: 100%;
height: 100%;
text-align: center;
}
.image {
width: 100%;
height: 100%;
display: block;
}
p{
position: absolute;
top: 0px;
width: 100%;
color: #999;
margin: 0;
padding-top: 30px;
}
}
.image-header {
padding: 10px 0 0 0;
float: left;
margin-right: 10px;
.el-upload {
border-radius: 5px;
width: 100px;
height: 100px;
line-height: 100px;
}
}
.image-header-item{
border:1px dashed rgba(0,0,0,.1);
padding: 5px;
border-radius: 5px;
width: 100px;
height: 100px;
margin:10px 10px 0 0;
}
}
</style>
<style lang="scss" >
.upload_img_wrap{
.el-upload--picture-card{
width: 100px;
height: 100px;
line-height: 102px;
}
}
</style>