UploadImages.vue 5.4 KB
<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>