PreviewMaterial.vue
18.2 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<script setup>
import { computed, nextTick, ref, watch } from "vue";
import { ZoomIn, ZoomOut } from "@element-plus/icons-vue";
import VueOfficeDocx from "@vue-office/docx";
import VueOfficeExcel from "@vue-office/excel";
import VueOfficePdf from "@vue-office/pdf";
import { ElMessage } from "element-plus";
import "@vue-office/docx/lib/index.css";
import "@vue-office/excel/lib/index.css";
// Props
const props = defineProps({
visible: {
type: Boolean,
default: false,
},
material: {
type: Object,
default: () => null,
},
});
// Emits
const emit = defineEmits([ "update:visible", "close" ]);
// 缩放比例(图片)
const scale = ref(1);
// PDF缩放比例
const pdfScale = ref(1);
// 旋转角度
const rotate = ref(0);
// 视频元素引用
const videoRef = ref(null);
// 计算属性:是否显示弹框
const dialogVisible = computed({
get: () => props.visible,
set: (val) => emit("update:visible", val),
});
// 计算属性:素材类型
const materialType = computed(() => {
if (!props.material) return "unknown";
const type = props.material.knowledgeType;
// 1:文档 2:图片 3:视频 4:其他
const typeMap = { 1: "document", 2: "image", 3: "video", 4: "other" };
return typeMap[type] || "other";
});
// 计算属性:是否可以缩放(图片和PDF可缩放)
const canScale = computed(() => materialType.value === "image" || isPdf.value);
// 计算属性:是否为 docx 文件
const isDocx = computed(() => {
if (!props.material?.filePath) return false;
const filePath = props.material.filePath.toLowerCase();
return filePath.endsWith(".docx");
});
// 计算属性:是否为 pdf 文件
const isPdf = computed(() => {
if (!props.material?.filePath) return false;
const filePath = props.material.filePath.toLowerCase();
return filePath.endsWith(".pdf");
});
// 计算属性:是否为 Excel 文件
const isExcel = computed(() => {
if (!props.material?.filePath) return false;
const filePath = props.material.filePath.toLowerCase();
return filePath.endsWith(".xlsx") || filePath.endsWith(".xls");
});
// 计算属性:是否为 PPTX 文件
const isPptx = computed(() => {
if (!props.material?.filePath) return false;
const filePath = props.material.filePath.toLowerCase();
return filePath.endsWith(".pptx") || filePath.endsWith(".ppt");
});
// 计算属性:Office Online Viewer URL
const officeViewerUrl = computed(() => {
if (!props.material?.filePath) return "";
const encodedUrl = encodeURIComponent(props.material.filePath);
return `http://view.officeapps.live.com/op/view.aspx?src=${encodedUrl}`;
});
// 计算属性:变换样式(图片)
const transformStyle = computed(() => {
if (materialType.value !== "image") return {};
return {
transform: `scale(${scale.value}) rotate(${rotate.value}deg)`,
transition: "transform 0.3s ease",
};
});
// 计算属性:PDF变换样式
const pdfTransformStyle = computed(() => {
if (!isPdf.value) return {};
return {
transform: `scale(${pdfScale.value})`,
transformOrigin: "top left",
transition: "transform 0.3s ease",
};
});
// 重置缩放和旋转
const resetTransform = () => {
scale.value = 1;
pdfScale.value = 1;
rotate.value = 0;
};
// 重置视频
const resetVideo = () => {
if (materialType.value === "video" && videoRef.value) {
try {
videoRef.value.pause();
videoRef.value.currentTime = 0;
// 移除 src 属性并重新加载,确保视频完全重置
videoRef.value.src = "";
videoRef.value.load();
// 如果需要,可以重新设置 src
if (props.material?.filePath) {
videoRef.value.src = props.material.filePath;
}
} catch (error) {
console.error("重置视频失败:", error);
}
}
};
// 重置所有数据(统一的重置函数)
const resetAll = () => {
// 重置图片和PDF的缩放、旋转
resetTransform();
// 重置视频
resetVideo();
// 重置文档滚动位置(通过查找滚动容器)
nextTick(() => {
try {
const scrollContainers = document.querySelectorAll(
".preview-material-dialog .overflow-auto",
);
scrollContainers.forEach((container) => {
if (container.scrollTop > 0 || container.scrollLeft > 0) {
container.scrollTop = 0;
container.scrollLeft = 0;
}
});
} catch (error) {
console.error("重置滚动位置失败:", error);
}
});
};
// 监听弹框显示状态,每次打开时重置
watch(dialogVisible, (newVal) => {
if (newVal) {
// 打开时重置所有状态
resetAll();
} else {
// 关闭时也重置所有状态,确保数据完全清理
resetAll();
}
});
// 放大
const handleZoomIn = () => {
if (isPdf.value) {
if (pdfScale.value < 3) {
pdfScale.value = Math.min(pdfScale.value + 0.25, 3);
}
} else {
if (scale.value < 3) {
scale.value = Math.min(scale.value + 0.25, 3);
}
}
};
// 缩小
const handleZoomOut = () => {
if (isPdf.value) {
if (pdfScale.value > 0.5) {
pdfScale.value = Math.max(pdfScale.value - 0.25, 0.5);
}
} else {
if (scale.value > 0.5) {
scale.value = Math.max(scale.value - 0.25, 0.5);
}
}
};
// 重置为 1:1
const handleReset = () => {
if (isPdf.value) {
pdfScale.value = 1;
} else {
scale.value = 1;
}
};
// 适应屏幕
const handleFit = () => {
if (isPdf.value) {
pdfScale.value = 0.8;
} else {
scale.value = 0.8;
}
};
// 下载
const handleDownload = async () => {
if (!props.material?.filePath) {
ElMessage.warning("文件地址不存在");
return;
}
try {
ElMessage.info("开始下载...");
const response = await fetch(props.material.filePath);
if (!response.ok) {
throw new Error(`下载失败: ${response.status}`);
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = props.material.name || "download";
document.body.append(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
ElMessage.success("下载成功");
} catch (error) {
console.error("下载失败:", error);
ElMessage.error("下载失败");
}
};
// 关闭弹框
const handleClose = () => {
// 重置所有数据
resetAll();
dialogVisible.value = false;
emit("close");
};
// PPTX 渲染完成处理
const renderedPptxHandler = () => {
console.log("PPTX 渲染完成");
};
// PPTX 渲染失败处理
const errorPptxHandler = (error) => {
console.error("PPTX 预览失败:", error);
ElMessage.error("PPTX 预览失败,请尝试下载后查看");
};
// 鼠标滚轮缩放处理(PDF)
const handlePdfWheel = (event) => {
if (!isPdf.value) return;
// 按住Ctrl键时进行缩放
if (event.ctrlKey || event.metaKey) {
event.preventDefault();
const delta = event.deltaY > 0 ? -0.1 : 0.1;
const newScale = Math.max(0.5, Math.min(3, pdfScale.value + delta));
pdfScale.value = newScale;
}
};
</script>
<template>
<el-dialog v-model="dialogVisible"
class="preview-material-dialog"
:destroy-on-close="true"
:show-close="false"
width="60%"
@close="handleClose">
<!-- 关闭按钮 -->
<div class="absolute right-[-20%] top-[-15%] z-50 flex cursor-pointer items-center justify-center rounded-full backdrop-blur-sm transition-all">
<img alt="关闭"
class="h-[40px] w-[40px] opacity-90"
src="/images/icon-preview-close.png"
@click="handleClose" />
</div>
<!-- 预览内容区 -->
<div class="flex h-[70vh] flex-col overflow-hidden bg-[#E5E5E5] p-0">
<!-- 图片预览 -->
<div v-if="materialType === 'image'"
class="flex flex-1 items-center justify-center overflow-hidden p-6">
<img :alt="material?.name"
class="max-h-full max-w-full select-none object-contain"
:src="material?.filePath"
:style="transformStyle" />
</div>
<!-- 视频预览 -->
<div v-else-if="materialType === 'video'"
class="flex flex-1 items-center justify-center overflow-hidden bg-black p-6">
<video ref="videoRef"
class="max-h-full max-w-full rounded-lg shadow-2xl"
controls
:src="material?.filePath">
您的浏览器不支持视频播放
</video>
</div>
<!-- 文档预览 -->
<div v-else-if="materialType === 'document'"
class="flex flex-1 flex-col overflow-hidden">
<!-- PDF 预览 -->
<div v-if="isPdf"
class="relative h-full w-full overflow-auto bg-white"
@wheel="handlePdfWheel">
<div class="pdf-container" :style="pdfTransformStyle">
<VueOfficePdf :src="material?.filePath"
style="width: 100%; height: 100%"
@error="
(error) => {
console.error('PDF 预览失败:', error);
ElMessage.error('PDF 预览失败,请尝试下载后查看');
}
"
@rendered="() => console.log('PDF 渲染完成')" />
</div>
</div>
<!-- docx 预览 -->
<div v-else-if="isDocx" class="h-full w-full overflow-auto bg-white">
<VueOfficeDocx :src="material?.filePath"
style="width: 100%; height: 100%"
@error="
(error) => {
console.error('docx 预览失败:', error);
ElMessage.error('文档预览失败,请尝试下载后查看');
}
"
@rendered="() => console.log('docx 渲染完成')" />
</div>
<!-- Excel 预览 -->
<div v-else-if="isExcel"
class="overflow-auto bg-white"
style="width: 100%; height: 640px; margin: 0 auto">
<VueOfficeExcel :src="material?.filePath"
style="height: 100%"
@error="
(error) => {
console.error('Excel 预览失败:', error);
ElMessage.error('Excel 预览失败,请尝试下载后查看');
}
"
@rendered="() => console.log('Excel 渲染完成')" />
</div>
<!-- PPTX 预览 -->
<div v-else-if="isPptx" class="flex flex-1 overflow-hidden bg-white">
<iframe allowfullscreen
class="h-full w-full border-0"
frameborder="0"
:src="officeViewerUrl"
@error="errorPptxHandler">
</iframe>
</div>
<!-- 其他文档类型 -->
<div v-else class="flex flex-1 items-center justify-center">
<div class="flex flex-col items-center text-center">
<div class="mb-6 flex h-20 w-20 items-center justify-center rounded-full bg-white shadow-lg">
<svg class="h-10 w-10 text-[#4BB79D]"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24">
<path d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2" />
</svg>
</div>
<p class="mb-3 text-base font-medium text-[#2F3133]">
{{ material?.name }}
</p>
<p class="text-sm text-[#777C85]">
文档预览暂不支持在线查看,请下载后查看
</p>
</div>
</div>
</div>
<!-- 其他类型 -->
<div v-else class="flex flex-1 items-center justify-center">
<div class="flex flex-col items-center text-center">
<div class="mb-6 flex h-20 w-20 items-center justify-center rounded-full bg-white shadow-lg">
<svg class="h-10 w-10 text-[#777C85]"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24">
<path d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2" />
</svg>
</div>
<p class="mb-3 text-base font-medium text-[#2F3133]">
{{ material?.name }}
</p>
<p class="text-sm text-[#777C85]">
该文件类型暂不支持预览
</p>
</div>
</div>
<!-- 底部控制栏 -->
<div v-if="false"
class="flex h-[60px] flex-shrink-0 items-center justify-center border-t border-[#E5E5E5] bg-white">
<div class="flex items-center justify-center gap-3">
<!-- 放大 -->
<button v-if="canScale"
class="flex h-9 w-9 items-center justify-center rounded-lg border-none text-sm transition-all duration-200"
:class="[
(isPdf ? pdfScale >= 3 : scale >= 3)
? 'cursor-not-allowed bg-[#F5F5F5] text-[#CCCCCC] opacity-50'
: 'cursor-pointer bg-[#F5F5F5] text-[#666666] hover:bg-[#E5E5E5] hover:text-[#1956FE] active:scale-95',
]"
:disabled="isPdf ? pdfScale >= 3 : scale >= 3"
@click="handleZoomIn">
<el-icon :size="18">
<ZoomIn />
</el-icon>
</button>
<!-- 缩小 -->
<button v-if="canScale"
class="flex h-9 w-9 items-center justify-center rounded-lg border-none text-sm transition-all duration-200"
:class="[
(isPdf ? pdfScale <= 0.5 : scale <= 0.5)
? 'cursor-not-allowed bg-[#F5F5F5] text-[#CCCCCC] opacity-50'
: 'cursor-pointer bg-[#F5F5F5] text-[#666666] hover:bg-[#E5E5E5] hover:text-[#1956FE] active:scale-95',
]"
:disabled="isPdf ? pdfScale <= 0.5 : scale <= 0.5"
@click="handleZoomOut">
<el-icon :size="18">
<ZoomOut />
</el-icon>
</button>
<!-- 1:1 -->
<button v-if="canScale"
class="flex h-9 w-auto min-w-[36px] cursor-pointer items-center justify-center rounded-lg border-none bg-[#F5F5F5] px-3 text-sm font-medium text-[#666666] transition-all duration-200 hover:bg-[#E5E5E5] hover:text-[#1956FE] active:scale-95"
@click="handleReset">
1:1
</button>
<!-- 适应屏幕 -->
<button v-if="canScale"
class="flex h-9 w-9 cursor-pointer items-center justify-center rounded-lg border-none bg-[#F5F5F5] text-[#666666] transition-all duration-200 hover:bg-[#E5E5E5] hover:text-[#1956FE] active:scale-95"
@click="handleFit">
<svg class="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24">
<rect height="18"
rx="2"
stroke-width="2"
width="18"
x="3"
y="3" />
<path d="M9 3v18M15 3v18M3 9h18M3 15h18" stroke-width="2" />
</svg>
</button>
<!-- 分隔线 -->
<!-- <div class="mx-1 h-6 w-px bg-[#E5E5E5]"></div> -->
<!-- 下载 -->
<!-- <button
class="flex h-9 w-9 cursor-pointer items-center justify-center rounded-lg border-none bg-[#F5F5F5] text-[#666666] transition-all duration-200 hover:bg-[#E5E5E5] hover:text-[#1956FE] active:scale-95"
@click="handleDownload"
>
<el-icon :size="18"><Download /></el-icon>
</button> -->
</div>
</div>
</div>
</el-dialog>
</template>
<style scoped>
/* 只保留必要的 Element Plus 样式覆盖 */
:deep(.preview-material-dialog) {
overflow: hidden;
background: #e5e5e5;
border-radius: 12px;
}
:deep(.preview-material-dialog .el-dialog__header) {
display: none;
}
:deep(.preview-material-dialog .el-dialog__body) {
padding: 0;
margin: 0;
}
/* 遮罩层样式 */
:deep(.el-overlay) {
background: #000 !important;
opacity: 0.8 !important;
}
/* PDF 容器样式 */
.pdf-container {
display: flex;
align-items: flex-start;
justify-content: flex-start;
width: 100%;
height: 100%;
}
</style>