CustomizeFormUploadFile.vue 7.89 KB
<template>
    <div>
        <div class="hidden-input">
            <input
                ref="fileInputRef"
                :accept="
                    item.interactionType == 'image_upload'
                        ? 'image/*'
                        : 'video/*'
                "
                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">
                    {{
                        item.interactionType == "image_upload"
                            ? "上传图片"
                            : "上传视频"
                    }}
                </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
                    v-if="item.interactionType == 'image_upload'"
                    class="uploadBoxImg"
                    :src="url"
                />
                <!-- 视频 -->
                <video
                    v-if="item.interactionType == 'video_upload'"
                    class="uploadBoxImg"
                    controls
                    :src="url"
                >
                    您的浏览器不支持 video 标签。
                </video>
                <!-- 点击视频预览 -->
                <div
                    v-if="item.interactionType == 'video_upload'"
                    @click="previewVideo(url)"
                    class="videoPreviewBox"
                ></div>
            </div>
        </div>

        <!-- 视频预览弹窗 -->
        <a-modal v-model:open="videoVisible" title="视频播放" :width="760">
            <div style="width: 100%; height: 680px; overflow: auto">
                <video
                    style="width: 100%; height: 660px"
                    controls
                    :src="videoUrl"
                >
                    您的浏览器不支持 video 标签。
                </video>
            </div>
            <template #footer></template>
        </a-modal>
    </div>
</template>
<script lang="ts">
import { Component, Prop, Vue } from "vue-facing-decorator";
import { uploadFile, isLikelyImageFile, isLikelyVideoFile } 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 CustomizeFormUploadFile extends Vue {
    @Prop({ default: () => {} })
    item: any = {};
    @Prop({ default: false })
    readonly = false;

    pickerValue: any = [];

    videoVisible = false;
    videoUrl = "";

    mounted() {
        this.initData();
    }

    // 初始化数据
    initData() {
        this.pickerValue =
            this.item?.inspectionResultVO?.inspectionValueList
                ?.filter((item) => item.valueText)
                ?.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 (this.item.interactionType == "image_upload") {
            if (!isLikelyImageFile(file)) {
                return message.error("仅支持上传图片文件");
            }
        } else if (this.item.interactionType == "video_upload") {
            if (!isLikelyVideoFile(file)) {
                return message.error("仅支持上传视频文件");
            }
        }
        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() {
        const nextValue =
            this.pickerValue.length > 0
                ? JSON.stringify(this.pickerValue)
                : "";
        if (
            TrackHelper.normalize(this.item?.value) ===
            TrackHelper.normalize(nextValue)
        ) {
            return;
        }
        this.$emit("update", nextValue);
    }

    previewVideo(url: string) {
        const timestamp = new Date().getTime();
        const urlWithTimestamp = url.includes("?")
            ? `${url}&t=${timestamp}`
            : `${url}?t=${timestamp}`;
        this.videoUrl = urlWithTimestamp;
        this.videoVisible = true;
    }
}
</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;
}

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

.videoPreviewBox {
    position: absolute;
    top: 0px;
    left: 0;
    width: 88px;
    height: 88px;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0.1;
    z-index: 9;
}
</style>