feat 联调下载pdf接口

This commit is contained in:
Ironsp
2026-09-27 15:27:17 +08:00
parent 54a5f96644
commit d1b2d981b5
3 changed files with 107 additions and 5 deletions

View File

@@ -93,3 +93,9 @@ export function uploadReport(data: UploadReportParams) {
data, data,
}) })
} }
/** GET /api/report/download/{reportId} 下载地址 */
export function getReportDownloadUrl(reportId: number | string) {
const baseURL = (import.meta.env.VITE_API_BASE_URL || '').replace(/\/$/, '')
return `${baseURL}/api/report/download/${reportId}`
}

View File

@@ -3,7 +3,7 @@ import type { ReportItem } from '@/api/report'
import type { HistoryReport } from '@/service/historyReport' import type { HistoryReport } from '@/service/historyReport'
import { onLoad, onShow } from '@dcloudio/uni-app' import { onLoad, onShow } from '@dcloudio/uni-app'
import { ref } from 'vue' import { ref } from 'vue'
import { getReportDetail } from '@/api/report' import { getReportDetail, getReportDownloadUrl } from '@/api/report'
import { useDbStore } from '@/store/modules/db' import { useDbStore } from '@/store/modules/db'
definePage({ definePage({
@@ -34,7 +34,7 @@ const reportId = ref<number | null>(null)
const reportData = ref<ReportDetailView | null>(null) const reportData = ref<ReportDetailView | null>(null)
const apiReport = ref<ReportItem | null>(null) const apiReport = ref<ReportItem | null>(null)
const rawReport = ref<HistoryReport | null>(null) const rawReport = ref<HistoryReport | null>(null)
const uploading = ref(false) const downloading = ref(false)
function parseDate(value?: string | null) { function parseDate(value?: string | null) {
if (!value) if (!value)
@@ -180,15 +180,111 @@ onShow(() => {
loadReportDetail() loadReportDetail()
}) })
function resolveDownloadFileName(id: number) {
const reportNo = apiReport.value?.reportNo
const ext = apiReport.value?.filePath?.match(/\.[^./\\]+$/)?.[0] || '.pdf'
return `${reportNo || `report_${id}`}${ext.startsWith('.') ? ext : `.${ext}`}`
}
/** H5:浏览器直接下载 */
function downloadOnH5(url: string, fileName: string) {
const a = document.createElement('a')
a.href = url
a.download = fileName
a.target = '_blank'
a.rel = 'noopener'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
/** App:下载到本地文件夹并提示路径 */
function downloadOnApp(url: string, fileName: string) {
return new Promise<void>((resolve, reject) => {
const relativePath = `_downloads/RoadRoller/${fileName}`
const task = plus.downloader.createDownload(url, { filename: relativePath }, (download, status) => {
if (status !== 200) {
reject(new Error('下载失败'))
return
}
const localPath = download.filename || relativePath
let displayPath = localPath
try {
displayPath = plus.io.convertLocalFileSystemURL(localPath) || localPath
}
catch {
// 转换失败时展示原始路径
}
uni.showModal({
title: '下载成功',
content: `文件已保存至:\n${displayPath}`,
showCancel: false,
})
resolve()
})
task.start()
})
}
async function handleDownload() { async function handleDownload() {
console.log('下载报告') const id = reportId.value
if (!id) {
uni.showToast({ title: '报告ID缺失', icon: 'none' })
return
}
if (downloading.value)
return
const url = getReportDownloadUrl(id)
const fileName = resolveDownloadFileName(id)
downloading.value = true
uni.showLoading({ title: '下载中...', mask: true })
try {
// #ifdef H5
downloadOnH5(url, fileName)
uni.showToast({ title: '已开始下载', icon: 'success' })
// #endif
// #ifdef APP-PLUS
await downloadOnApp(url, fileName)
// #endif
// #ifndef H5 || APP-PLUS
await new Promise<void>((resolve, reject) => {
uni.downloadFile({
url,
success: (res) => {
if (res.statusCode !== 200) {
reject(new Error('下载失败'))
return
}
uni.openDocument({
filePath: res.tempFilePath,
showMenu: true,
fail: () => uni.showToast({ title: '文件已下载', icon: 'success' }),
})
resolve()
},
fail: reject,
})
})
// #endif
}
catch (e: any) {
uni.showToast({ title: e?.message || '下载失败', icon: 'none' })
}
finally {
downloading.value = false
uni.hideLoading()
}
} }
</script> </script>
<template> <template>
<view class="page"> <view class="page">
<back-nav title="报告详情"> <back-nav title="报告详情">
<wd-button size="large" variant="soft" icon="download" :loading="uploading" :disabled="uploading" @tap="handleDownload" /> <wd-button size="large" variant="soft" icon="download" :loading="downloading" :disabled="downloading" @tap="handleDownload" />
</back-nav> </back-nav>
<view class="content"> <view class="content">

View File

@@ -308,7 +308,7 @@ onShow(() => {
<!-- 报告列表 --> <!-- 报告列表 -->
<view class="report-header"> <view class="report-header">
<text class="report-title"> <text class="report-title">
报告列表11111 报告列表
</text> </text>
<text class="report-count"> <text class="report-count">
共{{ reports.length }}条 共{{ reports.length }}条