UploadImages.vue
5.4 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
<template>
<div class="clearfix">
<a-upload v-model:fileList="fileList" :headers="headers" list-type="picture-card" :multiple="multiple" :action="apiUrl+action" :max-count="max" :showUploadList="showUploadList"
@change="handleChange" @download="download" @preview="handlePreview" :beforeUpload="beforeUploadImage" @remove="handleRemove" :accept="fileType">
<div v-if="showButton">
<LoadingOutlined v-if="upLoading"/>
<PlusOutlined v-else/>
<div class="ant-upload-text">{{ buttonName || $t('common.upload') }}</div>
</div>
</a-upload>
<a-modal :visible="previewVisible" @cancel="handleCancel" :title="previewTitle" :width="previewWidth">
<template #footer>
<a-button v-for="button in previewButtons" :key="button.key" :type="button.type??'primary'" @click="button.onclick(button.key,previewImage)">{{ button.name }}</a-button>
</template>
<img alt="example" style="width: 100%" :src="previewImage?.url"/>
</a-modal>
</div>
<div class="file-type2" v-if="showSuffixesHint">
<div class="text-danger">{{ $t('common.file.type') }}:{{ suffixes }}</div>
</div>
<div class="file-type" v-if="showMaxHint">
<div class="text-danger">{{ $t('common.file.limit') }}:{{ max }}</div>
</div>
</template>
<script lang="ts">
import {DeleteFilled, EditFilled, LoadingOutlined, PlusOutlined, PlusSquareOutlined, SearchOutlined, TableOutlined, UndoOutlined} from '@ant-design/icons-vue';
import {message, Upload} from "ant-design-vue";
import * as imageConvsersion from "image-conversion";
import {Component, Prop, Watch} from "vue-facing-decorator";
import {UploadBase} from '../UploadBase';
export interface ButtonPros {
key: string,
name: string,
type: string,
onclick: (key: string, file: any) => void
}
@Component({
name: "MUploadImages",
components: {
SearchOutlined,
PlusSquareOutlined,
UndoOutlined,
PlusOutlined,
EditFilled, DeleteFilled, TableOutlined, LoadingOutlined
}
})
export default class UploadImages extends UploadBase {
/**
* 图片大小
*/
@Prop({type: Number, required: false, default: undefined}) fileSize: number;
/**
* 宽度
*/
@Prop({type: Number, required: false, default: undefined}) width: number;
/**
* 高度
*/
@Prop({type: Number, required: false, default: undefined}) height: number;
/**
* 质量
*/
@Prop({type: Number, required: false, default: 1}) quality: number;
@Prop({type: Array, required: false})
previewButtons: ButtonPros[];
@Prop({type: String, required: false})
previewTitle: string;
/**
* 预览图片宽度
*/
@Prop({type: Number, required: false, default: 1024})
previewWidth: number;
/**
* 模态框图片
*/
previewImage: any = null;
beforeUploadImage(file: any) {
const isOk = this.suffixes.split(',').some((regex) => file.name.endsWith(regex.toLowerCase()) || file.name.endsWith(regex.toUpperCase()));
return new Promise((resolve, reject) => {
if (this.max !== 1 && this.fileList.length >= this.max) {
message.error(this.$t('common.upload_max', {max: this.max}));
reject(file);
return;
}
if (!isOk) {
message.error(this.$t('common.upload_file_type_error', {name: file.name, suffixes: this.suffixes}));
reject(file);
} else if (!(file.size / 1024 / 1024 < this.size)) {
message.error(this.$t('common.upload_max_size', {size: this.size}));
// Upload.LIST_IGNORE解决上传失败但是文件还会显示在组建列表的问题
return Upload.LIST_IGNORE;
} else {
let compressAccuratelyConfig = null;
if (this.size && this.width && this.height) {
compressAccuratelyConfig = {size: this.fileSize, width: this.width, height: this.height, quality: this.quality};
}
if (compressAccuratelyConfig && (file.name.endsWith('.jpg') || file.name.endsWith('.png')
|| file.name.endsWith('.JPG') || file.name.endsWith('.PNG') || file.name.endsWith('.JPEG') || file.name.endsWith('.jpeg'))) {
console.log(compressAccuratelyConfig);
imageConvsersion.compressAccurately(file, compressAccuratelyConfig).then(res => {
resolve(res);
});
} else {
resolve(file);
}
}
});
}
showMessage(msg: string, type: "success" | "warning" | "error" | "info"): void {
switch (type) {
case "success":
message.success(msg);
break;
case "warning":
message.warning(msg);
break;
case "error":
message.error(msg);
break;
case "info":
message.info(msg);
break;
}
}
}
</script>
<style scoped>
.avatar-uploader > .ant-upload {
width: 128px;
height: 128px;
}
.ant-upload-select-picture-card i {
font-size: 32px;
color: #999;
}
.ant-upload-select-picture-card .ant-upload-text {
margin-top: 8px;
color: #666;
}
</style>