Files
zkxw/mobile-h5/pages/oa/pdf-preview/index.vue
2026-08-13 16:50:36 +08:00

196 lines
5.6 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view class="pdf-preview">
<view class="pdf-preview__tip">{{ tip }}</view>
</view>
</template>
<script>
import { API_BASE_URL, MINIO_BASE_URL } from '../../../utils/config.js'
import { getToken } from '../../../utils/auth.js'
import {
buildDirectMinioUrl,
parsePdfPreviewQuery,
pdfPreviewLog,
toAbsoluteDownloadUrl
} from '../../../utils/oa-pdf-preview-query.js'
function absolutize(url) {
const abs = toAbsoluteDownloadUrl(url, {
apiBaseUrl: API_BASE_URL,
minioBaseUrl: MINIO_BASE_URL
})
if (String(url || '').trim() && abs && abs !== url) {
pdfPreviewLog('absolutize', {
from: String(url).slice(0, 120),
to: abs.slice(0, 160)
})
}
return abs
}
function directMinio(fileUrl) {
return buildDirectMinioUrl(fileUrl, {
apiBaseUrl: API_BASE_URL,
minioBaseUrl: MINIO_BASE_URL
})
}
export default {
data() {
return {
tip: '正在打开文档…',
_opened: false
}
},
onLoad(query) {
pdfPreviewLog('onLoad raw query', query)
this.openFromQuery(query || {})
},
methods: {
async openFromQuery(query) {
const parsed = parsePdfPreviewQuery(query)
const { fileUrl, legacyUrl, name, rawKeys } = parsed
pdfPreviewLog('parsed query', {
rawKeys,
name,
fileUrlLen: fileUrl.length,
fileUrlPreview: fileUrl.slice(0, 160),
legacyUrlLen: legacyUrl.length,
legacyUrlPreview: legacyUrl.slice(0, 120),
hasToken: !!getToken(),
minioBase: String(MINIO_BASE_URL || '').slice(0, 80)
})
// 微信 downloadFile:优先 MinIO 公网直链。
// download-url 预签名经 /minio 相对路径改写后签名失效,真机常 403。
let url = ''
let fallbackUrl = ''
let source = ''
if (legacyUrl) {
const absLegacy = absolutize(legacyUrl)
const direct = directMinio(fileUrl || legacyUrl)
// legacy 若带坏签名,仍准备直链兜底
if (direct && /^https?:\/\//i.test(direct)) {
url = direct
fallbackUrl =
absLegacy && absLegacy !== direct ? absLegacy : ''
source = 'minio-direct(+legacy-fallback)'
} else {
url = absLegacy
source = 'query.url'
}
} else if (fileUrl) {
url = directMinio(fileUrl)
source = 'minio-direct'
pdfPreviewLog('minio-direct', {
urlPreview: String(url || '').slice(0, 160)
})
}
url = absolutize(url) || url
fallbackUrl = absolutize(fallbackUrl) || fallbackUrl
if (!url || !/^https?:\/\//i.test(url)) {
const reason = !fileUrl && !legacyUrl
? '缺少 fileUrl/url 参数(请确认小程序与 oa-h5 已同步部署)'
: '无法解析为 http(s) 下载地址'
pdfPreviewLog('invalid url', { reason, fileUrl, legacyUrl, url })
this.tip = '文件地址无效'
uni.showToast({ title: '文件地址无效', icon: 'none' })
return
}
pdfPreviewLog('openPdf', {
source,
name,
urlPreview: url.slice(0, 160),
hasFallback: !!fallbackUrl
})
this.openPdf(url, name, fallbackUrl)
},
openPdf(url, name, fallbackUrl) {
if (this._opened) return
this._opened = true
this._tryDownload(url, name, fallbackUrl, false)
},
_tryDownload(url, name, fallbackUrl, isRetry) {
pdfPreviewLog('downloadFile start', {
name,
isRetry: !!isRetry,
urlPreview: String(url || '').slice(0, 160)
})
uni.downloadFile({
url,
success: (res) => {
const code = res && res.statusCode
pdfPreviewLog('downloadFile success', {
statusCode: code,
hasTemp: !!(res && res.tempFilePath),
isRetry: !!isRetry
})
if (code === 403 && fallbackUrl && !isRetry) {
pdfPreviewLog('403 → retry fallback', {
fallbackPreview: String(fallbackUrl).slice(0, 160)
})
this._tryDownload(fallbackUrl, name, '', true)
return
}
if (!res || code !== 200 || !res.tempFilePath) {
this.tip = '下载失败' + (code ? '(' + code + ')' : '')
uni.showToast({
title: '下载 PDF 失败' + (code ? ' ' + code : ''),
icon: 'none'
})
this._opened = false
return
}
uni.openDocument({
filePath: res.tempFilePath,
fileType: 'pdf',
showMenu: true,
success: () => {
pdfPreviewLog('openDocument success')
this.tip = '已打开预览'
setTimeout(() => {
uni.navigateBack({ fail: () => {} })
}, 300)
},
fail: (err) => {
pdfPreviewLog('openDocument fail', err)
this.tip = '无法打开文档'
uni.showToast({ title: '无法打开 PDF', icon: 'none' })
this._opened = false
}
})
},
fail: (err) => {
pdfPreviewLog('downloadFile fail', err)
if (fallbackUrl && !isRetry) {
pdfPreviewLog('fail → retry fallback')
this._tryDownload(fallbackUrl, name, '', true)
return
}
this.tip = '下载失败'
uni.showToast({ title: '下载 PDF 失败', icon: 'none' })
this._opened = false
}
})
}
}
}
</script>
<style scoped>
.pdf-preview {
min-height: 100vh;
padding: 48rpx 32rpx;
background: #f5f7fa;
}
.pdf-preview__tip {
text-align: center;
color: #64748b;
font-size: 28rpx;
}
</style>