drag-file-upload.vue
4.44 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
<template>
<div v-if="show">
<div class="upload-image-item" v-if="uploadList[0]">
<template v-if="uploadList[0].status === 'finished' || uploadList[0].url">
<img :src="uploadList[0].url">
<div class="upload-image-item-cover">
<Icon type="ios-eye-outline" size="30" @click.native="handleView(uploadList[0].url)"></Icon>
<Icon type="ios-trash-outline" size="30" @click.native="handleRemove(uploadList[0])"></Icon>
</div>
</template>
<template v-else>
<Progress v-if="uploadList[0].showProgress" :percent="uploadList[0].percentage" hide-info></Progress>
</template>
</div>
<Upload v-show="!uploadList[0]"
ref="upload"
:show-upload-list="false"
:data="{bucket: bucket}"
:on-success="handleSuccess"
:on-error="handleError"
:default-file-list="defaultList"
:format="['jpg','jpeg','png']"
:max-size="2048"
:on-format-error="handleFormatError"
:on-exceeded-size="handleMaxSize"
type="drag"
action="/Api/upload/image"
style="display: inline-block;width:120px;">
<div style="width: 120px;height:120px;line-height: 140px;">
<Icon type="plus-round" size="50"></Icon>
</div>
</Upload>
<Modal title="查看图片" v-model="visible">
<img :src="imgUrl" v-if="visible" style="width: 100%">
</Modal>
</div>
</template>
<script>
import _ from 'lodash';
export default {
name: 'drag-file-upload',
props: {
id: {
type: Object
},
defaultFile: {
type: String
},
bucket: {
type: String,
default() {
return 'goodsimg';
}
}
},
data() {
let _this = this;
return {
imgUrl: '',
visible: false,
show: true,
uploadList: [],
defaultList: _this.defaultFile ? [{url: _this.defaultFile}] : []
};
},
methods: {
handleView(url) {
this.imgUrl = url;
this.visible = true;
},
handleRemove(file) {
let files = this.$refs.upload.fileList;
this.$refs.upload.fileList.splice(files.indexOf(file), 1);
this.uploadList = this.$refs.upload.fileList;
this.$emit('on-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: '文件 ' + file.name + ' 格式不正确,请上传 jpg 或 png 格式的图片。'
});
},
handleMaxSize(file) {
this.$Notice.warning({
title: '超出文件大小限制',
desc: '文件 ' + file.name + ' 太大,不能超过 2M。'
});
}
},
mounted() {
this.uploadList = this.$refs.upload.fileList;
},
watch: {
defaultFile: function(newValue) {
if (newValue) {
this.defaultList = [{url: newValue}];
}
}
}
};
</script>
<style>
.upload-image-item {
display: inline-block;
width: 120px;
height: 120px;
text-align: center;
line-height: 120px;
border: 2px solid transparent;
border-radius: 4px;
overflow: hidden;
background: #fff;
position: relative;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
margin-right: 4px;
}
.upload-image-item img {
width: 120px;
height: 120px;
}
.upload-image-item-cover {
display: none;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.6);
}
.upload-image-item:hover .upload-image-item-cover {
display: block;
}
.upload-image-item-cover i {
color: #fff;
font-size: 20px;
cursor: pointer;
margin: 0 2px;
}
</style>