customizeUploadFile.vue
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<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>