index.vue
4.49 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
<template>
<div class="upload-ctnr">
<label v-if="name" :class="['label',{'required-field': required}]" for="">{{name}}</label>
<div class="upload-wrap" :style="defaultStyle">
<img v-if="url" class="image" :src="url" alt="" />
<div v-else class="add-img">
<input type="file" class="file" id="file" ref="input" @change="upload" accept="image/png, image/jpeg, image/gif, image/jpg" />
<i class="el-icon-plus add"></i>
<p>上传照片</p>
</div>
<div class="operate" v-if="url">
<div class="btn-wrap">
<i class="el-icon-view" @click="handleLook"></i>
<i v-if="!disabled" class="el-icon-delete del-icon" @click="del"></i>
</div>
</div>
</div>
<el-dialog class="image-dialog" :visible.sync="dialogVisibleImg" width="30%">
<big-view ref="viewForm" v-if="dialogVisibleImg" :dialogSrc="url" dialogType="img"></big-view>
</el-dialog>
</div>
</template>
<script>
import bigView from '@/components/big-view'
export default {
name: 'Upload',
components: {
bigView
},
props: {
required: {
type: Boolean,
default: false
},
name: {
type: String,
default: ''
},
disabled: {
type: Boolean,
default: false
},
url: {
type: String,
default: ''
},
attr: {
type: String,
default: ''
},
defaultStyle: {
type: Object,
default: ()=>{
return {}
}
},
checkObj: { // 校验规则
type: Object,
default: () => {
return {}
}
}
},
data() {
return {
dialogVisibleImg: false
}
},
methods: {
handleLook() {
this.dialogVisibleImg = true
},
upload(e) {
const file = e.target.files[0]
if (this.checkObj && Object.keys(this.checkObj).length) {
const AllImgExt = this.checkObj.fileTypes
let extName = file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
if (AllImgExt.indexOf(extName) === -1) {
return this.$message.error('上传格式不正确')
}
const size = file.size / 1024 / 1024
const maxSize = this.checkObj.maxSize
if (size > maxSize) {
return this.$message.error(`上传文件不可超过${maxSize}M`)
}
}
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = event => {
const imgFile = event.target.result
const arrBase64 = imgFile.split(',')
const base64 = file.name.split('.').pop() + '@' + arrBase64[1]
this.$apis.claims.uploadImgApi({ file: base64, fileType: 'image' }).then(res => {
this.$emit('uploadSuccess', { attr: this.attr, url: res })
})
}
},
del() {
this.$emit('delImage', { attr: this.attr })
}
}
}
</script>
<style lang="scss" scoped>
.upload-ctnr {
display: flex;
.label {
display: inline-block;
vertical-align: top;
width: 154px;
height: 20px;
font-size: 14px;
color: #333333;
text-align: right;
line-height: 20px;
box-sizing: border-box;
}
.upload-wrap {
display: inline-flex;
border: 1px dashed #00000026;
box-sizing: border-box;
width: 104px;
height: 104px;
background: #0000000a;
border-radius: 4px;
position: relative;
&:hover {
.operate {
display: block;
}
}
.image {
width: 100%;
height: 100%;
z-index: 2;
position: relative;
}
.add-img {
width: 100%;
height: 100%;
cursor: pointer;
display: inline-flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: #666666;
position: relative;
.file {
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
position: absolute;
opacity: 0;
}
.add {
font-size: 18px;
}
p {
height: 32px;
line-height: 32px;
}
}
.operate {
display: none;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.4);
border-radius: 4px;
z-index: 5;
.btn-wrap {
color: #fff;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
.del-icon {
margin-left: 12px;
}
i {
font-size: 16px;
cursor: pointer;
}
}
}
}
}
</style>