drag-file-upload.vue
4.17 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
<template>
<div v-if="show">
<image-purview
v-if="uploadList[0]"
:status="uploadList[0].status"
:url="uploadList[0].url"
:progress="uploadList[0].showProgress"
:percentage="uploadList[0].percentage"
:remove="true"
@remove-image="handleRemove"
></image-purview>
<div v-show="!uploadList[0]" class="upload-box">
<Upload
ref="upload"
:show-upload-list="false"
:data="{ bucket: bucket }"
:on-success="handleSuccess"
:on-error="handleError"
:default-file-list="defaultList"
:format="format"
:max-size="maxSize"
:on-format-error="handleFormatError"
:on-exceeded-size="handleMaxSize"
action="/Api/upload/image"
>
<Icon type="ios-cloud-upload-outline" :title="uploadIconTitle"></Icon>
</Upload>
<Icon v-if="skn" type="ios-cloud-outline" title="调用图片服务" @click.native="browseOnline"></Icon>
</div>
<modal-skn-image v-model="sknImageModel" :skn="skn" @selected="selected"></modal-skn-image>
</div>
</template>
<script>
import _ from 'lodash';
export default {
name: 'DragFileUpload',
props: {
id: {
type: Object,
},
defaultFile: {
type: String,
},
skn: {
type: [String, Number],
},
bucket: {
type: String,
default() {
return 'goodsimg';
},
},
maxSize: {
type: Number,
default: 2048,
},
formatMaxSizeMsg: {
type: Function,
default() {
return file => '文件 ' + file.name + ' 太大,不能超过 2M。';
},
},
format: {
type: Array,
default() {
return ['jpg', 'jpeg', 'png'];
},
},
formatErrMsg: {
type: Function,
default() {
return file => `文件 ${file.name} 格式不正确,请上传 jpg 或 png 格式的图片。`;
},
},
uploadIconTitle: {
type: String,
default: '上传图片',
},
},
data() {
const _this = this;
return {
imgUrl: '',
visible: false,
show: true,
sknImageModel: false,
uploadList: [],
defaultList: _this.defaultFile ? [{ url: _this.defaultFile }] : [],
};
},
watch: {
defaultFile(newValue) {
if (newValue) {
this.defaultList = [{ url: newValue }];
}
},
},
mounted() {
this.uploadList = this.$refs.upload.fileList;
},
methods: {
handleView(url) {
this.imgUrl = url;
this.visible = true;
},
handleRemove() {
const file = this.uploadList[0];
const files = this.$refs.upload.fileList;
this.$refs.upload.fileList.splice(files.indexOf(file), 1);
this.uploadList = this.$refs.upload.fileList;
this.$emit('remove', this.id);
},
handleSuccess(response, file, files) {
if (_.get(response, 'data.imagesList.length', 0)) {
file.url = response.data.imagesList[0];
}
this.uploadList = files;
this.$emit('success', this.id, file);
},
handleError() {
this.$Notice.error('上传失败');
this.$emit('error', this.id);
},
handleFormatError(file) {
this.$Notice.warning({
title: '文件格式不正确',
desc: this.formatErrMsg(file),
});
},
handleMaxSize(file) {
this.$Notice.warning({
title: '超出文件大小限制',
desc: this.formatMaxSizeMsg(file),
});
},
browseOnline() {
this.sknImageModel = true;
},
selected(url) {
this.uploadList = this.defaultList = [{ url }];
this.$emit('success', this.id, { url });
},
},
};
</script>
<style lang="scss">
.upload-box {
width: 100px;
height: 100px;
background: #fff;
border: 1px dashed #dddee1;
border-radius: 4px;
text-align: center;
cursor: pointer;
position: relative;
overflow: hidden;
transition: border-color 0.2s ease;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;
&:hover {
border-color: #2d8cf0;
}
.ivu-upload {
height: 30px;
}
.ivu-icon {
font-size: 30px;
margin: 0 2px;
color: #000;
transition: color 0.2s ease;
&:hover {
color: #2d8cf0;
}
}
}
</style>