fix(oa): pass short fileUrl to PDF preview to avoid WeChat navigateTo limit
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
5c10b207d2
commit
804995ea45
2 files changed
+59
-48
No files matched your search
@@ -5,6 +5,15 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { post } from '../../../utils/request.js'
|
||||
import { MINIO_BASE_URL } from '../../../utils/config.js'
|
||||
import { getToken } from '../../../utils/auth.js'
|
||||
|
||||
function authHeader() {
|
||||
const token = getToken()
|
||||
return token ? { Authorization: 'Bearer ' + token } : {}
|
||||
}
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@@ -13,18 +22,53 @@ export default {
|
||||
}
|
||||
},
|
||||
onLoad(query) {
|
||||
const url = decodeURIComponent(String((query && query.url) || '').trim())
|
||||
const name = decodeURIComponent(
|
||||
String((query && query.name) || 'approval.pdf').trim()
|
||||
)
|
||||
if (!url || !/^https?:\/\//i.test(url)) {
|
||||
this.tip = '文件地址无效'
|
||||
uni.showToast({ title: '文件地址无效', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.openPdf(url, name)
|
||||
this.openFromQuery(query || {})
|
||||
},
|
||||
methods: {
|
||||
async openFromQuery(query) {
|
||||
const name = decodeURIComponent(
|
||||
String((query && query.name) || 'approval.pdf').trim()
|
||||
)
|
||||
const legacyUrl = decodeURIComponent(
|
||||
String((query && query.url) || '').trim()
|
||||
)
|
||||
const fileUrl = decodeURIComponent(
|
||||
String((query && query.fileUrl) || '').trim()
|
||||
)
|
||||
|
||||
let url = ''
|
||||
if (legacyUrl && /^https?:\/\//i.test(legacyUrl)) {
|
||||
url = legacyUrl
|
||||
} else if (fileUrl) {
|
||||
url = await this.resolveDownloadUrl(fileUrl, name)
|
||||
}
|
||||
|
||||
if (!url || !/^https?:\/\//i.test(url)) {
|
||||
this.tip = '文件地址无效'
|
||||
uni.showToast({ title: '文件地址无效', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.openPdf(url, name)
|
||||
},
|
||||
async resolveDownloadUrl(fileUrl, fileName) {
|
||||
try {
|
||||
const res = await post(
|
||||
'/web/admin/attachment/download-url',
|
||||
{
|
||||
fileUrl,
|
||||
fileName
|
||||
},
|
||||
authHeader()
|
||||
)
|
||||
const u = String(res?.downloadUrl || '').trim()
|
||||
if (u) return u
|
||||
} catch (_) {
|
||||
/* fall through to MinIO base URL */
|
||||
}
|
||||
const base = String(MINIO_BASE_URL || '').replace(/\/$/, '')
|
||||
const key = String(fileUrl || '').replace(/^\//, '')
|
||||
return base && key ? base + '/' + key : ''
|
||||
},
|
||||
openPdf(url, name) {
|
||||
if (this._opened) return
|
||||
this._opened = true
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
*/
|
||||
import html2canvas from 'html2canvas'
|
||||
import { jsPDF } from 'jspdf'
|
||||
import { uploadAttachment, fetchAttachmentDownloadUrl } from '../../api/oa.js'
|
||||
import { attachmentFullUrl } from '../../utils/attachment.js'
|
||||
import { uploadAttachment } from '../../api/oa.js'
|
||||
import { formatDeptDisplay } from '../kit/oa-dept-value.js'
|
||||
import { formatPersonDisplay } from '../kit/oa-person-value.js'
|
||||
import { formatIdNameDisplay } from '../../utils/id-name-value.js'
|
||||
@@ -738,21 +737,6 @@ export function buildPrintBodyHtml(model) {
|
||||
return `<style>${buildPrintStylesContent()}</style>${buildPrintPageContent(model)}`
|
||||
}
|
||||
|
||||
function buildPrintHtml(model) {
|
||||
return `<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1"/>
|
||||
<title>${escapeHtml(model.title)}</title>
|
||||
<style>${buildPrintStylesContent()}</style>
|
||||
</head>
|
||||
<body>
|
||||
${buildPrintPageContent(model)}
|
||||
</body>
|
||||
</html>`
|
||||
}
|
||||
|
||||
/** 用 canvas 绘制中文页码(jsPDF 内置字体不支持汉字,会乱码) */
|
||||
function renderPageLabelCanvas(text) {
|
||||
const font = '20px "PingFang SC", "Microsoft YaHei", "Heiti SC", sans-serif'
|
||||
@@ -873,10 +857,10 @@ function isWeChatMiniWebView() {
|
||||
return false
|
||||
}
|
||||
|
||||
function navigateToHostPdfPreview({ url, fileName }) {
|
||||
function navigateToHostPdfPreview({ fileUrl, fileName }) {
|
||||
const q =
|
||||
'url=' +
|
||||
encodeURIComponent(url) +
|
||||
'fileUrl=' +
|
||||
encodeURIComponent(fileUrl) +
|
||||
'&name=' +
|
||||
encodeURIComponent(fileName || 'approval.pdf')
|
||||
const path = '/pages/oa/pdf-preview/index?' + q
|
||||
@@ -927,25 +911,8 @@ export async function exportOaApprovalPdf(detail, opts = {}) {
|
||||
return { ok: false, message: '上传 PDF 失败:无文件地址' }
|
||||
}
|
||||
|
||||
let openUrl = ''
|
||||
try {
|
||||
const dl = await fetchAttachmentDownloadUrl({
|
||||
fileUrl,
|
||||
fileName: filename
|
||||
})
|
||||
openUrl = String(dl?.downloadUrl || dl?.data?.downloadUrl || '').trim()
|
||||
} catch {
|
||||
/* fall through */
|
||||
}
|
||||
if (!openUrl) {
|
||||
openUrl = attachmentFullUrl(fileUrl) || fileUrl
|
||||
}
|
||||
if (!/^https?:\/\//i.test(openUrl)) {
|
||||
return { ok: false, message: '无法打开 PDF:下载地址无效' }
|
||||
}
|
||||
|
||||
const navigated = navigateToHostPdfPreview({
|
||||
url: openUrl,
|
||||
fileUrl,
|
||||
fileName: filename
|
||||
})
|
||||
if (!navigated) {
|
||||
|
||||
Reference in new issue
Block a user