CustomizeFormButtonSelectUploadFile.vue 6.79 KB
<template>
    <div class="abnormal-image-upload">
        <div class="abnormal-image-upload__title">
            异常图片<span class="abnormal-image-upload__required">*</span>
        </div>
        <div class="abnormal-image-upload__subtitle">
            有损伤请拍摄图片,无损伤请忽略
        </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"
                :class="{ 'uploadBox--readonly': readonly }"
                @click="handleUploadBoxClick()"
            >
                <PlusOutlined class="plusBox" />
                <div class="uploadTxt">上传图片</div>
            </div>
            <div
                v-for="(url, index) in pickerValue"
                :key="index"
                class="uploadBox"
            >
                <!-- 删除按钮 -->
                <div
                    v-if="!readonly"
                    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, isLikelyImageFile } from "@/utils/upload.js";
import { PlusOutlined, CloseCircleOutlined } from "@ant-design/icons-vue";
import { message } from "ant-design-vue";
import { TrackHelper } from "@/common/TrackHelper";

@Component({ components: { PlusOutlined, CloseCircleOutlined } })
export default class CustomizeFormButtonSelectUploadFile extends Vue {
    @Prop({ default: () => {} })
    item: any = {};
    @Prop({ default: false })
    readonly = false;

    pickerValue: any = [];

    // item.resultStatus 变化时,清空选择项
    @Watch("item.resultStatus")
    watchItem(newValue: boolean): 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 || "{}");
    }

    handleUploadBoxClick() {
        if (this.readonly) {
            return;
        }
        this.handlePickFile();
    }

    //点击上传文件
    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;
        if (!isLikelyImageFile(file)) {
            return message.error("仅支持上传图片文件");
        }
        const maxSize10MB = 10 * 1024 * 1024;
        const maxSize100MB = 100 * 1024 * 1024;
        if (
            (this.item.interactionType == "image_upload" ||
                this.item.interactionType == "image_upload_and_button_select") &&
            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() {
        const nextValue =
            this.pickerValue.length > 0
                ? JSON.stringify(this.pickerValue)
                : "";
        if (
            TrackHelper.normalize(this.item?.uploadValue) ===
            TrackHelper.normalize(nextValue)
        ) {
            return;
        }
        this.$emit("update", nextValue);
    }
}
</script>

<style scoped lang="less">
.abnormal-image-upload {
    width: 100%;
    float: left;
    margin-top: 8px;
}

.abnormal-image-upload__title {
    font-size: 14px;
    font-weight: 500;
    line-height: 22px;
    color: #2f3133;
}

.abnormal-image-upload__required {
    margin-left: 4px;
    color: #eb4e4e;
}

.abnormal-image-upload__subtitle {
    margin-top: 4px;
    margin-bottom: 12px;
    font-size: 12px;
    font-weight: 400;
    line-height: 20px;
    color: #777c85;
}

.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;
}

.uploadBox--readonly {
    cursor: default;
    pointer-events: none;
}

.uploadBox :deep(.ant-image),
.uploadBox :deep(.ant-image-img) {
    width: 100%;
    height: 100%;
    border-radius: 6px;
}

.uploadBox :deep(.ant-image-img) {
    object-fit: cover;
}

.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>