PdfPreview.vue
3.84 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
<!--
* @Date: 2024-01-17
* @Description: PDF预览组件
-->
<template>
<view v-if="show" class="pdf-preview">
<view class="mask" @tap="close"></view>
<view class="panel">
<view class="header">
<text class="title">{{ title || 'PDF 预览' }}</text>
<view class="close" @tap="close">
<IconFont name="Del" size="16" color="#666" />
</view>
</view>
<view class="body">
<!-- #ifdef H5 -->
<iframe
v-if="url"
class="pdf-iframe"
:src="url"
frameborder="0"
@load="on_loaded"
/>
<view v-else class="empty">
<text class="empty-text">PDF 地址不能为空</text>
</view>
<!-- #endif -->
<!-- #ifdef WEAPP -->
<view class="empty">
<text class="empty-text">小程序不支持内嵌 PDF 预览</text>
</view>
<!-- #endif -->
</view>
<view v-if="loading" class="loading">
<IconFont name="Loading" size="24" class="animate-spin text-blue-600" />
<text class="loading-text">加载中...</text>
</view>
</view>
</view>
</template>
<script setup>
import { ref, watch } from 'vue'
import IconFont from '@/components/IconFont.vue'
const props = defineProps({
show: {
type: Boolean,
default: false
},
url: {
type: String,
default: ''
},
title: {
type: String,
default: ''
}
})
const emit = defineEmits(['update:show', 'onLoad'])
const loading = ref(false)
const close = () => {
emit('update:show', false)
}
const on_loaded = () => {
loading.value = false
emit('onLoad', false)
}
watch(() => props.show, (new_show) => {
if (new_show) {
loading.value = true
} else {
loading.value = false
}
}, { immediate: true })
</script>
<style lang="less" scoped>
.pdf-preview {
position: fixed;
inset: 0;
z-index: 999;
.mask {
position: absolute;
inset: 0;
background: rgba(0, 0, 0, 0.5);
}
.panel {
position: absolute;
top: 0;
right: 0;
width: 100%;
height: 100%;
background: #fff;
display: flex;
flex-direction: column;
}
.header {
height: 96rpx;
padding: 0 24rpx;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 2rpx solid #f1f5f9;
.title {
font-size: 30rpx;
font-weight: 600;
color: #111827;
}
.close {
width: 64rpx;
height: 64rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 9999rpx;
background: #f3f4f6;
}
}
.body {
flex: 1;
position: relative;
}
.pdf-iframe {
width: 100%;
height: 100%;
border: none;
}
.empty {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: #f9fafb;
.empty-text {
font-size: 28rpx;
color: #6b7280;
}
}
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(255, 255, 255, 0.9);
border-radius: 16rpx;
padding: 24rpx 32rpx;
display: flex;
align-items: center;
gap: 16rpx;
.loading-text {
font-size: 28rpx;
color: #333;
}
}
}
</style>