customizeUploadFile.vue 3.46 KB
<script setup lang="ts">
import { ref } from "vue";
import { uploadFile } from "@/utils/upload.js";
const uploading = ref(false);

import { showToast } from "vant";

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

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

const pickerValue = ref([]);

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

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

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);
		updateData();
	} catch (e: unknown) {
		const msg = e instanceof Error ? e.message : String(e);
		showToast(`上传失败:${msg}`);
	} finally {
		uploading.value = false;
	}
}

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

function updateData() {
	props.item.value = pickerValue.value.join(',');
	emit("update", pickerValue.value.join(','));
}

</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 class="titleTip">有损伤请拍摄图片,无损伤请忽略</div>
		</div>

		<input ref="fileInputRef" accept="image/*" class="hidden-input" type="file" @change="onFileChange">
		<div class="uploadPage">
			<div class="uploadBox" @click="pickFile">
				<van-icon name="plus" size="24" color="#999999" class="plusBox" />
				<div class="uploadTxt">添加照片</div>
			</div>
			<div v-for="(item, index) in pickerValue" :key="index" class="uploadBox">
				<div class="deleteBox" @click="handleDelete(item)">
					<van-icon name="clear" size="20" color="#999999" />
				</div>
				<van-image width="96" height="96" style="border-radius: 8px;" :src="item" />
			</div>
		</div>
	</div>
</template>

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

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

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

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

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

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

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

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

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

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

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