CustomizeFormButtonSelectUploadFile.vue 4.67 KB
<template>
    <div>
        <div class="hidden-input">
            <input ref="fileInputRef" accept="image/*" type="file" @change="onFileChange">
        </div>
        <div class="uploadPage">
            <div v-if="(getValidationRules().maxCount || 3) > pickerValue.length" class="uploadBox"
                @click="handlePickFile()">
                <PlusOutlined class="plusBox" />
                <div class="uploadTxt">
                    上传图片
                </div>
            </div>
            <div v-for="(url, index) in pickerValue" :key="index" class="uploadBox">
                <!-- 删除按钮 -->
                <div class="deleteBox" title="点击删除" @click="handleDelete(url)">
                    <CloseCircleOutlined class="closeIcon" />
                </div>
                <!-- 图片 -->
                <a-image class="uploadBoxImg" :src="url" />
            </div>
        </div>
    </div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-facing-decorator";
import { uploadFile } from "@/utils/upload.js";
import { PlusOutlined, CloseCircleOutlined } from "@ant-design/icons-vue";
import { message } from "ant-design-vue";


@Component({ components: { PlusOutlined, CloseCircleOutlined } })

export default class CustomizeFormButtonSelectUploadFile extends Vue {
    @Prop({ default: () => { } })
    item: any = {};

    pickerValue: any = [];

    // item.resultStatus 变化时,清空选择项
    @Watch('item.resultStatus', { deep: true, immediate: true })
    watchItem(newValue: any, oldValue: any): void {
        if (newValue) {
            this.pickerValue = [];
            this.updateData();
        }
    }

    mounted() {
        this.initData();
    }

    // 初始化数据
    initData() {
        this.pickerValue = this.item?.inspectionResultVO?.inspectionValueList?.filter(item => item.valueText && item.interactionType == "image_upload")?.map(item => item.valueText) || [];
        this.updateData();
    }

    getValidationRules() {
        return JSON.parse(this.item.validationRules || "{}");
    }

    //点击上传文件
    handlePickFile() {
        this.$refs.fileInputRef?.click();
    }

    async onFileChange(ev: Event) {
        const input = ev.target as HTMLInputElement;
        const file = input.files?.[0];
        input.value = "";
        if (!file) return;
        const maxSize10MB = 10 * 1024 * 1024;
        const maxSize100MB = 100 * 1024 * 1024;
        if (this.item.interactionType == "image_upload" && file.size > maxSize10MB) {
            return message.error("文件大小不能超过10MB");
        }
        if (this.item.interactionType == "video_upload" && file.size > maxSize100MB) {
            return message.error("文件大小不能超过100MB");
        }
        try {
            const res = await uploadFile(file);
            const url = (typeof res.aliyunAddress === "string" && res.aliyunAddress) || (typeof res.url === "string" && res.url) || "";
            message.success("上传成功");
            this.pickerValue.push(url);
            this.updateData();
        } catch (error: unknown) {
            const msg = error instanceof Error ? error.message : String(error);
            message.error(`上传失败:${msg}`);
        } finally {
        }
    }

    handleDelete(item: string) {
        this.pickerValue = this.pickerValue.filter((i) => i !== item);
        this.updateData();
    }

    updateData() {
        if (this.pickerValue.length > 0) {
            this.$emit("update", JSON.stringify(this.pickerValue));
        } else {
            this.$emit("update", "");
        }
    }
}
</script>

<style scoped lang="less">
.hidden-input {
    position: absolute;
    width: 0;
    height: 0;
    pointer-events: none;
    opacity: 0;
}

.uploadPage {
    float: left;
    width: calc(100% - 24px);
    margin-left: 6px;
}

.uploadBox {
    position: relative;
    float: left;
    width: 88px;
    height: 88px;
    margin-right: 12px;
    margin-bottom: 16px;
    background: #f5f7fa;
    border: 1px solid #e1e3e6;
    border-radius: 6px;
    cursor: pointer;
}

.plusBox {
    float: left;
    margin-top: 22px;
    margin-right: 36px;
    margin-left: 36px;
    font-size: 12px;
    color: #9BA3AD;
}

.closeIcon {
    font-size: 20px;
    color: #999999;
}

.uploadTxt {
    float: left;
    width: 100%;
    height: 20px;
    margin-top: 8px;
    font-size: 12px;
    font-weight: 400;
    line-height: 20px;
    color: #969aa3;
    text-align: center;
}

.uploadBoxImg {
    width: 100%;
    height: 100%;
    z-index: 9;
    border-radius: 8px;
}

.deleteBox {
    position: absolute;
    top: 2px;
    right: 2px;
    z-index: 999;
    width: 20px;
    height: 20px;
    cursor: pointer;
}
</style>