index.vue
8.19 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
<!--
* @Description: 统一文档预览组件
* @Date: 2025-01-30
* @Features:
* - H5 环境:使用 OfficeViewer + PdfPreview 组件
* - 小程序环境:根据文件大小自动选择预览方式
* - 小于 10MB:微信原生 API (wx.openDocument)
* - 大于等于 10MB:web-view + 腾讯文档预览
-->
<template>
<view class="document-preview">
<!-- #ifdef H5 -->
<!-- H5 环境:使用现有组件 -->
<OfficeViewer
v-if="isOfficeDocument && src"
:src="src"
:fileType="finalFileType"
@rendered="handleRendered"
@error="handleError"
/>
<PdfPreview
v-if="isPdfDocument && src"
:url="src"
:show="show"
@update:show="handleUpdateShow"
@onLoad="handlePdfLoad"
/>
<!-- #endif -->
<!-- #ifdef WEAPP -->
<!-- 小程序环境:使用微信原生 API 或 web-view -->
<view class="preview-container">
<!-- 加载状态 -->
<view v-if="loading" class="loading-container">
<IconFont name="loading" size="24" class="animate-spin text-blue-600" />
<text class="loading-text">{{ loadingText }}</text>
</view>
<!-- 错误状态 -->
<view v-else-if="error" class="error-container">
<IconFont name="issue" size="48" color="#ff6b6b" />
<text class="error-text">{{ error }}</text>
<nut-button type="primary" size="small" @click="retry">
重试
</nut-button>
</view>
<!-- 预览按钮 -->
<view v-else class="action-container">
<view class="file-info">
<IconFont :name="fileIcon" size="64" class="text-blue-600" />
<text class="file-name">{{ fileName || '未知文件' }}</text>
<text class="file-size">{{ formatFileSize(fileSize) }}</text>
</view>
<nut-button
type="primary"
block
@click="openDocument"
:loading="loading"
>
{{ previewButtonText }}
</nut-button>
<text v-if="needWebView" class="hint-text">
大文件将使用在线预览
</text>
</view>
</view>
<!-- #endif -->
</view>
</template>
<script setup>
import { ref, computed, watch } from 'vue'
import { getFileSize, detectFileType, formatFileSize } from './utils'
import { IconFont } from '@nutui/icons-vue-taro'
// #ifdef H5
import OfficeViewer from '../OfficeViewer.vue'
import PdfPreview from '../PdfPreview.vue'
// #endif
// #ifdef WEAPP
import Taro from '@tarojs/taro'
// #endif
// Props 定义
const props = defineProps({
// 文档 URL
src: {
type: String,
required: true
},
// 文件类型(可选,自动检测)
fileType: {
type: String,
default: ''
},
// 是否显示(H5 PDF 预览用)
show: {
type: Boolean,
default: false
},
// 文件名(用于显示)
fileName: {
type: String,
default: ''
}
})
// Emits 定义
const emit = defineEmits(['rendered', 'error', 'update:show'])
const finalFileType = computed(() => {
const detectedType = detectFileType(props.src)
return (props.fileType || detectedType || '').toLowerCase()
})
const isOfficeDocument = computed(() => {
return ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'].includes(finalFileType.value)
})
const isPdfDocument = computed(() => {
return finalFileType.value === 'pdf'
})
// #ifdef WEAPP
// 响应式数据
const loading = ref(false)
const loadingText = ref('准备中...')
const error = ref('')
const fileSize = ref(0)
// 计算属性
const needWebView = computed(() => fileSize.value === 0 || fileSize.value >= 10 * 1024 * 1024)
const fileIcon = computed(() => {
const type = finalFileType.value.toLowerCase()
const iconMap = {
pdf: 'Order',
doc: 'Edit',
docx: 'Edit',
xls: 'Category',
xlsx: 'Category',
ppt: 'PlayCircleFill',
pptx: 'PlayCircleFill'
}
return iconMap[type] || 'Link'
})
const previewButtonText = computed(() => {
return needWebView.value ? '在线预览文档' : '打开文档'
})
/**
* 初始化文件信息
*/
const initFileInfo = async () => {
if (!props.src) return
loading.value = true
loadingText.value = '检测文件...'
error.value = ''
try {
// 获取文件大小
loadingText.value = '获取文件信息...'
const size = await getFileSize(props.src)
fileSize.value = size
console.log('文件信息:', {
url: props.src,
type: finalFileType.value,
size: size,
needWebView: needWebView.value
})
} catch (err) {
console.error('获取文件信息失败:', err)
error.value = '无法获取文件信息,请检查网络连接'
} finally {
loading.value = false
}
}
/**
* 打开文档
*/
const openDocument = async () => {
loading.value = true
loadingText.value = needWebView.value ? '跳转到在线预览...' : '下载中...'
error.value = ''
try {
if (needWebView.value) {
// 大文件:使用 web-view 在线预览
await openWithWebView()
} else {
// 小文件:使用微信原生 API
await openWithNativeAPI()
}
} catch (err) {
console.error('打开文档失败:', err)
error.value = err.message || '文档打开失败,请重试'
} finally {
loading.value = false
}
}
/**
* 使用微信原生 API 打开文档
*/
const openWithNativeAPI = async () => {
try {
// 下载文件
const downloadRes = await Taro.downloadFile({
url: props.src,
timeout: 30000 // 30秒超时
})
if (downloadRes.statusCode !== 200) {
throw new Error('文件下载失败')
}
// 打开文档
await Taro.openDocument({
filePath: downloadRes.tempFilePath,
fileType: finalFileType.value
})
console.log('文档打开成功')
} catch (err) {
console.error('微信原生 API 打开文档失败:', err)
throw new Error('文档打开失败: ' + (err.errMsg || err.message))
}
}
/**
* 使用 web-view 在线预览
*/
const openWithWebView = async () => {
try {
// 跳转到 web-view 容器页面
const previewUrl = encodeURIComponent(props.src)
const fileType = encodeURIComponent(finalFileType.value)
await Taro.navigateTo({
url: `/pages/document-preview/index?url=${previewUrl}&type=${fileType}`
})
console.log('跳转到在线预览页面')
} catch (err) {
console.error('跳转失败:', err)
throw new Error('打开预览页面失败')
}
}
/**
* 重试
*/
const retry = () => {
initFileInfo()
}
// 监听 src 变化
watch(() => props.src, (newSrc) => {
if (newSrc) {
initFileInfo()
}
}, { immediate: true })
// #endif
// #ifdef H5
const handleRendered = () => {
console.log('H5: 文档渲染完成')
emit('rendered')
}
const handleError = (err) => {
console.error('H5: 文档渲染失败', err)
emit('error', err)
}
const handleUpdateShow = (value) => {
emit('update:show', value)
}
const handlePdfLoad = () => {
console.log('H5: PDF 加载完成')
}
// #endif
</script>
<style lang="less" scoped>
.document-preview {
width: 100%;
height: 100%;
background: #f5f5f5;
}
// #ifdef WEAPP
.preview-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 800rpx;
padding: 60rpx;
background: #fff;
border-radius: 32rpx;
}
.loading-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 40rpx;
.loading-text {
font-size: 56rpx;
color: #999;
}
}
.error-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 40rpx;
padding: 80rpx 40rpx;
.error-text {
font-size: 56rpx;
color: #666;
text-align: center;
line-height: 1.6;
}
}
.action-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 60rpx;
width: 100%;
max-width: 1000rpx;
.file-info {
display: flex;
flex-direction: column;
align-items: center;
gap: 40rpx;
padding: 80rpx;
background: #f8f9fa;
border-radius: 32rpx;
width: 100%;
.file-name {
font-size: 64rpx;
font-weight: 500;
color: #333;
text-align: center;
word-break: break-all;
}
.file-size {
font-size: 48rpx;
color: #999;
}
}
.hint-text {
font-size: 48rpx;
color: #ff9800;
text-align: center;
}
}
// #endif
</style>