customizeUploadFile.vue 4.88 KB
<script setup lang="ts">
import { ref } from "vue";

import { uploadFile } from "@/utils/upload.js";
import { showToast } from "vant";

const props = defineProps({
	item: {
		type: Object,
		default: () => {},
	},
});

const emit = defineEmits(["update"]);

const uploading = ref(false);

const pickerValue = ref([]);
const fileInputRef = ref<HTMLInputElement | null>(null);

function pickFile() {
	fileInputRef.value?.click();
}

// 页面加载时初始化数据
onMounted(() => {
	initData();
});

// 初始化数据
function initData() {
	pickerValue.value = [];
	if (props?.item?.inspectionResultVO) {
		const inspectionValueList = props?.item?.inspectionResultVO?.inspectionValueList || [];
		for (const i of inspectionValueList) {
			if (i.valueText) { pickerValue.value.push(i.valueText); }
		}
	}
	updateData();
}

// 计算属性
const validationRules = computed(() => {
	return JSON.parse(props.item.validationRules || "{}");
});

async function onFileChange(ev: Event) {
	const input = ev.target as HTMLInputElement;
	const file = input.files?.[0];
	input.value = "";
	if (!file) return;
	uploading.value = true;
	try {
		const res = await uploadFile(file);
		const url = (typeof res.aliyunAddress === "string" && res.aliyunAddress) || (typeof res.url === "string" && res.url) || "";
		showToast("上传成功");
		pickerValue.value.push(url);

		console.log("pickerValue.value", pickerValue.value);

		updateData();
	} catch (error: unknown) {
		const msg = error instanceof Error ? error.message : String(error);
		showToast(`上传失败:${msg}`);
	} finally {
		uploading.value = false;
	}
}

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

function updateData() {
	if (pickerValue.value.length > 0) {
		emit("update", JSON.stringify(pickerValue.value));
	} else {
		emit("update", "");
	}
}

</script>

<template>
    <div class="buttonSelect">
        <div class="title">
            <div class="titleContent">
                <span v-if="item.required === 1" class="isRequired">*</span>
                <span>{{ item.nodeName }}</span>
            </div>
            <div v-if="validationRules.maxCount && validationRules.maxCount.length > 1 && item.interactionType == 'image_upload'"
                 class="titleTip">
                有损伤请拍摄图片,无损伤请忽略
            </div>
        </div>
        <input ref="fileInputRef"
               :accept="item.interactionType == 'image_upload' ? 'image/*' : 'video/*'"
               class="hidden-input"
               type="file"
               @change="onFileChange">
        <div class="uploadPage">
            <div v-if="(validationRules.maxCount || 3) > pickerValue.length"
                 class="uploadBox"
                 @click="pickFile">
                <van-icon class="plusBox"
                          color="#999999"
                          name="plus"
                          size="24" />
                <div class="uploadTxt">
                    {{ item.interactionType == 'image_upload' ? '添加照片' : '添加视频' }}
                </div>
            </div>
            <div v-for="(url, index) in pickerValue"
                 :key="index"
                 class="uploadBox">
                <div class="deleteBox" @click="handleDelete(url)">
                    <van-icon color="#999999"
                              name="clear"
                              size="20" />
                </div>
                <van-image height="96"
                           :src="url"
                           style="border-radius: 8px;"
                           width="96" />
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.buttonSelect {
	float: left;
	width: 100%;
	background: #fff;
}

.title {
	float: left;
	width: 100%;
	padding-left: 16px;
	margin-top: 16px;
	margin-bottom: 10px;
}

.titleContent {
	float: left;
	width: 100%;
	height: 24px;
	font-size: 14px;
	line-height: 24px;
	color: #141e34;
}

.titleTip {
	float: left;
	width: 100%;
	height: 16px;
	font-size: 12px;
	font-weight: 400;
	line-height: 16px;
	color: #999;
}

.isRequired {
	padding-right: 2px;
	font-size: 14px;
	color: #ee3040;
}

.hidden-input {
	position: absolute;
	width: 0;
	height: 0;
	pointer-events: none;
	opacity: 0;
}

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

.uploadBox {
	position: relative;
	float: left;
	width: 96px;
	height: 96px;
	margin-right: 12px;
	margin-bottom: 16px;
	background: #f9f9f9;
	border: 1px solid #ddd;
	border-radius: 8px;
}

.plusBox {
	float: left;
	margin-top: 22px;
	margin-right: 36px;
	margin-left: 36px;
}

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

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