customizeUploadFile.vue
8.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<script setup lang="ts">
import { computed, onMounted, 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;
const maxSize10MB = 10 * 1024 * 1024;
const maxSize100MB = 100 * 1024 * 1024;
if (props.item.interactionType == "image_upload" && file.size > maxSize10MB) {
showToast("文件大小不能超过10MB");
return;
}
if (props.item.interactionType == "video_upload" && file.size > maxSize100MB) {
showToast("文件大小不能超过100MB");
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);
sourceSrcUrl.value = "";
sourceUrl.value = [];
videoShow.value = false;
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);
console.log("pickerValue.value111111111111111", pickerValue.value);
// 清除视频预览状态,防止缓存
sourceSrcUrl.value = "";
sourceUrl.value = [];
videoShow.value = false;
updateData();
}
function updateData() {
if (pickerValue.value.length > 0) {
emit("update", JSON.stringify(pickerValue.value));
} else {
emit("update", "");
}
}
// 图片预览
const imagePreview = ref(false);
const previewImagesList = ref([]);
function previewImage(url: string) {
previewImagesList.value = [url];
imagePreview.value = true;
}
// 视频预览
const videoShow = ref(false);
const sourceUrl = ref([]);
const sourceSrcUrl = ref("");
function previewVideo(url: string) {
// 添加时间戳参数,防止浏览器缓存
const timestamp = new Date().getTime();
const urlWithTimestamp = url.includes('?') ? `${url}&t=${timestamp}` : `${url}?t=${timestamp}`;
sourceSrcUrl.value = urlWithTimestamp;
sourceUrl.value = [urlWithTimestamp];
console.log("sourceUrl.value", sourceUrl.value);
videoShow.value = true;
}
</script>
<template>
<div>
<van-overlay :show="uploading"
teleport="#vant-portal-host"
z-index="2000">
<div class="loadingBox">
<van-loading color="#1989fa"
size="24px"
vertical>
上传中...
</van-loading>
</div>
</van-overlay>
<!-- 图片预览 -->
<van-image-preview v-model:show="imagePreview" :closeable="true" :close-on-click-image="false" :images="previewImagesList"></van-image-preview>
<!-- 视频预览 -->
<van-image-preview v-model:show="videoShow"
:close-on-click-image="false"
:closeable="true"
:images="sourceUrl">
<template #image="{ src }">
<video controls style="width: 100%;">
<source :src="sourceSrcUrl" />
</video>
</template>
</van-image-preview>
<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 v-if="item.interactionType == 'image_upload'"
class="uploadBoxImg"
radius="8px"
:src="url"
@click="previewImage(url)" />
<!-- 视频 -->
<video v-if="item.interactionType == 'video_upload'"
class="uploadBoxImg"
controls
:src="url">
您的浏览器不支持 video 标签。
</video>
<!-- 点击视频预览 -->
<div v-if="item.interactionType == 'video_upload'"
class="videoPreviewBox"
@click="previewVideo(url)">
</div>
</div>
</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 #f9f9f9;
border-radius: 8px;
}
.uploadBoxImg {
width: 100%;
height: 100%;
z-index: 9;
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: 999;
width: 20px;
height: 20px;
cursor: pointer;
}
.loadingBox {
position: absolute;
top: 50%;
left: 50%;
width: 80px;
height: 80px;
transform: translate(-50%, -50%);
}
.videoPreviewBox {
position: absolute;
top: 0px;
left: 0;
width: 96px;
height: 96px;
background: rgba(0, 0, 0, 0.5);
opacity: 0.1;
z-index: 9;
}
</style>