pdf-upload.vue
3.88 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
<template>
<div>
<template v-show="uploadList.length !== 0">
<div v-for="(item, idx) in uploadList" :key="idx" class="upload-list upload-wrap">
<template v-if="item.status === 'finished'">
<Icon type="document" size="32" style="line-height: 64px"></Icon>
<div class="upload-list-cover">
<Icon type="ios-eye-outline" @click.native="handleView(item.name)"></Icon>
<Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
</div>
</template>
<template v-else>
<Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
</template>
</div>
</template>
<Upload
v-show="uploadList.length === 0"
ref="upload"
:data="{ bucket: 'invoices' }"
:show-upload-list="false"
:on-success="handleSuccess"
:format="['pdf']"
:max-size="1024 * 1024 * 5"
:on-format-error="handleFormatError"
:on-exceeded-size="handleMaxSize"
:before-upload="handleBeforeUpload"
type="drag"
action="/Api/upload/image"
style="display: inline-block;width:64px;"
>
<div class="upload-wrap">
<Icon type="ios-cloud-upload-outline" title="上标发票文件" size="32"></Icon>
</div>
</Upload>
<Modal v-model="visible" title="查看发票" :width="800">
<iframe v-if="visible" :src="uploadList[0].url" frameborder="0" scrolling="auto" />
</Modal>
</div>
</template>
<script>
import _ from 'lodash';
export default {
props: {
value: {
type: [String, null],
default: null,
},
},
data() {
return {
visible: false,
uploadList: [],
};
},
mounted() {
this.clearFileList();
this.unwatch = this.$watch(
'value',
newVal => {
let addNew = true;
if (this.uploadList[0]) {
if (this.uploadList[0].url !== newVal) {
this.clearFileList();
} else {
addNew = false;
}
}
if (addNew && newVal) {
this.uploadList = this.$refs.upload.fileList = [
{
url: newVal,
status: 'finished',
showProcent: false,
},
];
}
},
{
immediate: true,
}
);
},
destroyed() {
this.unwatch();
},
methods: {
clearFileList() {
this.uploadList = this.$refs.upload.fileList = [];
},
handleView() {
this.visible = true;
},
handleRemove() {
this.clearFileList();
this.$emit('input', null);
},
handleSuccess(res, file) {
if (_.get(res, 'data.imagesList.length', 0)) {
file.url = res.data.imagesList[0];
} else {
return;
}
this.$emit('input', file.url);
},
handleFormatError(file) {
this.$Notice.warning({
title: '格式错误',
desc: `${file.name}不是pdf文件,请上传pdf`,
});
},
handleMaxSize(file) {
this.$Notice.warning({
title: '文件大小错误',
desc: `${file.name}的大小三于5MB`,
});
},
handleBeforeUpload() {
return this.uploadList.length < 2;
},
},
};
</script>
<style lang="scss" scoped>
.upload-wrap {
width: 64px;
height: 64px;
line-height: 64px;
text-align: center;
i {
line-height: 64px;
}
}
.upload-list {
display: inline-block;
border: 1px solid #999;
border-radius: 4px;
overflow: hidden;
background: #fff;
position: relative;
}
.upload-list img {
width: 100%;
height: 100%;
}
.upload-list-cover {
display: none;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.6);
}
.upload-list:hover .upload-list-cover {
display: block;
}
.upload-list-cover i {
color: #fff;
font-size: 20px;
cursor: pointer;
margin: 0 2px;
}
iframe {
border: none;
width: 100%;
height: 600px;
}
</style>