index.vue
3.63 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
<template>
<el-upload
ref="uploader"
class="avatar-uploader"
:action="url"
:show-file-list="false"
:headers="headers"
:on-success="handleSuccess"
:before-upload="beforeUpload"
:disabled="disabled"
>
<div v-if="value" :style="{ 'background-image': `url(${value})` }" class="avatar">
<div class="avatar-uploader-mask" @click.stop>
<div class="avatar-uploader-mask-btns">
<i v-if="!disabled" class="iconfont icon-plus" @click="handleAdd"></i>
<i class="iconfont icon-search" @click="handlePreview" style="margin: 0px 15px;"></i>
<i v-if="!disabled" class="iconfont icon-delete" @click="handleDelete"></i>
</div>
</div>
</div>
<i v-else class="avatar-uploader-icon" :class="disabled ? 'el-icon-picture-outline' : 'el-icon-plus'"></i>
<el-dialog class="photoPreviewer" :visible.sync="dialogVisible" append-to-body>
<img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
</el-upload>
</template>
<style lang="scss">
.avatar-uploader {
position: relative;
.el-upload {
border: 1px dashed #d9d9d9 !important;
border-radius: 6px !important;
cursor: pointer !important;
position: relative !important;
overflow: hidden !important;
}
.el-upload:hover {
border-color: #1890ff !important;
}
.avatar-uploader-icon {
font-size: 28px !important;
color: #8c939d !important;
width: 178px !important;
height: 178px !important;
line-height: 178px !important;
text-align: center !important;
}
.avatar {
background-position: center;
background-repeat: no-repeat;
background-size: 178px auto;
width: 178px !important;
height: 178px !important;
display: block !important;
&:hover {
.avatar-uploader-mask {
display: block;
}
}
.avatar-uploader-mask {
position: absolute;
display: none;
top: 0;
left: 0;
height: 100%;
width: 100%;
color: rgba(255, 255, 255, 0.9);
background-color: rgba(0, 0, 0, 0.5);
.avatar-uploader-mask-btns {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
i {
font-size: 32px;
&:hover {
color: #1890ff;
}
}
}
}
}
}
.photoPreviewer {
.el-dialog__header {
border-bottom: 0;
}
}
</style>
<script>
export default {
props: {
headers: {
type: Object,
default() {
return {}
}
},
url: {
type: String,
required: true
},
value: String,
// 选择框禁用状态
disabled: {
type: Boolean,
default: false
},
},
name: 'ImageUpload',
data() {
return {
dialogImageUrl: '',
dialogVisible: false
};
},
methods: {
handleSuccess(response = {}, file, fileList) {
const { result = [] } = response;
const url = result[0];
this.$emit('input', url);
},
beforeUpload(file) {
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 2MB!');
}
return isLt2M;
},
handleAdd() {
if (this.$refs.uploader && this.$refs.uploader.$el.children && this.$refs.uploader.$el.children[0]) {
this.$refs.uploader.$el.children[0].click();
}
},
handlePreview() {
this.dialogImageUrl = this.value;
this.dialogVisible = true;
},
handleDelete() {
this.$emit('input', undefined);
}
}
}
</script>