吉安感知网项目-前端
chenyao
2026-01-29 46b4805a995c2042b3b23ac7ae824352df11bb67
feat:更新报告下载文件
2 files modified
117 ■■■■ changed files
applications/task-work-order/src/views/orderView/orderManage/inspectionReport/index.vue 114 ●●●● patch | view | raw | blame | history
applications/task-work-order/src/views/orderView/orderManage/inspectionReport/inspectionRequestApi.js 3 ●●●● patch | view | raw | blame | history
applications/task-work-order/src/views/orderView/orderManage/inspectionReport/index.vue
@@ -306,68 +306,60 @@
let loadingData
// 文档下载
async function handleDownload() {
    if (selectedFiles.value.length === 0) {
        ElMessage.warning('请选择要下载的文件')
        return
    }
    selectedFiles.value.forEach((file, index) => {
        setTimeout(() => {
            if (file.link) {
                // 检查链接是否可能返回 XML
                if (file.link.includes('.xml') || file.link.includes('error') || file.link.includes('exception')) {
                    ElMessage.error(`文件 ${file.originalName} 的链接可能无效`)
                    return
                }
                // 尝试使用 fetch 检查链接是否有效
                fetch(file.link, { method: 'HEAD' })
                    .then(response => {
                        if (response.ok) {
                            const contentType = response.headers.get('content-type')
                            if (contentType && contentType.includes('xml')) {
                                ElMessage.error(`文件 ${file.originalName} 的链接返回 XML 内容,可能无效`)
                                return
                            }
                            // 执行下载
                            const a = document.createElement('a')
                            a.href = file.link
                            a.download = file.originalName || `attachment_${index + 1}`
                            document.body.appendChild(a)
                            a.click()
                            document.body.removeChild(a)
                        } else {
                            ElMessage.error(`文件 ${file.originalName} 的链接无效,状态码:${response.status}`)
                        }
                    })
                    .catch(error => {
                        ElMessage.error(`文件 ${file.originalName} 的链接错误:${error.message}`)
                    })
            }
        }, index * 300) // 300ms delay between downloads
    })
    // 如果是勾选一个可以直接下载,超过一个打包下载
    // 依次下载每个文件(添加延迟以避免浏览器限制)
    if (selectedFiles.value.length === 0) {
        ElMessage.warning('请选择要下载的文件')
        return
    }
    // if (selectedFiles.value.length === 1) {
    //     selectedFiles.value.forEach(async (file, index) => {
    //         if (file.link) {
    //             try {
    //                 // 使用 fetch 获取 blob 数据来强制下载,避免浏览器打开文件
    //                 const response = await fetch(file.link)
    //                 if (!response.ok) {
    //                     throw new Error(`Failed to fetch file: ${response.status}`)
    //                 }
    //                 const blob = await response.blob()
    //                 const url = URL.createObjectURL(blob)
    //                 const a = document.createElement('a')
    //                 a.href = url
    //                 a.download = file.originalName || `attachment_${index + 1}`
    //                 document.body.appendChild(a)
    //                 a.click()
    //                 document.body.removeChild(a)
    //                 // 释放 URL 对象
    //                 setTimeout(() => URL.revokeObjectURL(url), 100)
    //                 ElMessage.success('下载成功')
    //             } catch (error) {
    //                 console.error('Download error:', error)
    //                 ElMessage.error(`下载文件 ${file.originalName} 失败: ${error.message}`)
    //             }
    //         }
    //     })
    // } else {
            try {
            const fileIds = selectedFiles.value.map(i => i.id).join(',')
            await fjDownloadByByteApi({ attachIds: fileIds }).then(res => {
            const blob = res.data
            const url = URL.createObjectURL(blob)
            const a = document.createElement('a')
            a.href = url
            a.download = `巡查报告.zip`
            document.body.appendChild(a)
            a.click()
            document.body.removeChild(a)
            URL.revokeObjectURL(url)
                    ElMessage.success('下载成功')
                })
            } catch (e) {
            } finally {
                // loadingData.close()
            }
    // }
}
// async function handleDownload() {
//     if (selectedFiles.value.length === 0) {
//         ElMessage.warning('请选择要下载的文件')
//         return
//     }
//     console.log(selectedFiles.value, '999')
//     selectedFiles.value.forEach((file, index) => {
//         setTimeout(() => {
//             if (file.link) {
//                 const a = document.createElement('a')
//                 a.href = file.link
//                 a.download = file.originalName || `attachment_${index + 1}`
//                 document.body.appendChild(a)
//                 a.click()
//                 document.body.removeChild(a)
//             }
//         }, index * 300) // 300ms delay between downloads
//     })
// }
// 文档上传
async function handleUpload() {
applications/task-work-order/src/views/orderView/orderManage/inspectionReport/inspectionRequestApi.js
@@ -47,9 +47,10 @@
// 批量打包下载
export const fjDownloadByByteApi = params => {
    return request({
        return request({
        url: `/blade-resource/attach/downloadByByte`,
        method: 'get',
        params,
        responseType: 'blob'
    })
}