From c54c823a8ce1506bbc85bd76320f8ec8a291ba96 Mon Sep 17 00:00:00 2001 From: zlwu <704541015@qq.com> Date: Sun, 10 Aug 2025 21:39:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mobile/src/components/FileUpload/index.vue | 26 +- .../pages/xg/xsjcxx/xsqj/leaveEdit/index.vue | 889 ++++++++++-------- 2 files changed, 522 insertions(+), 393 deletions(-) diff --git a/mobile/src/components/FileUpload/index.vue b/mobile/src/components/FileUpload/index.vue index 48ed290c..165b8c9e 100644 --- a/mobile/src/components/FileUpload/index.vue +++ b/mobile/src/components/FileUpload/index.vue @@ -37,7 +37,7 @@ - + = 15) { - uni.showToast({ - title: '最多只能上传15个文件', - icon: 'none', - }); - return; - } + // // 检查文件数量限制 + // if (this.attachmentList.length >= 15) { + // uni.showToast({ + // title: '最多只能上传15个文件', + // icon: 'none', + // }); + // return; + // } - this.wechatImageLoading = true; - this.uploadProgress = 0; - this.addWechatLog('开始选择微信图片...', 'info'); + // this.wechatImageLoading = true; + // this.uploadProgress = 0; + // this.addWechatLog('开始选择微信图片...', 'info'); - try { - // 计算剩余可选择的图片数量 - const remainingCount = 5 - this.attachmentList.length; - const selectCount = Math.min(remainingCount, 9); + // try { + // // 计算剩余可选择的图片数量 + // const remainingCount = 5 - this.attachmentList.length; + // const selectCount = Math.min(remainingCount, 9); - this.addWechatLog(`可选择图片数量: ${selectCount}`, 'info'); + // this.addWechatLog(`可选择图片数量: ${selectCount}`, 'info'); - const result = await this.wechatSDK.chooseImage({ - count: selectCount, - sizeType: ['original', 'compressed'], - sourceType: ['album', 'camera'], - }); + // const result = await this.wechatSDK.chooseImage({ + // count: selectCount, + // sizeType: ['original', 'compressed'], + // sourceType: ['album', 'camera'], + // }); - this.addWechatLog(`成功选择了 ${result.localIds.length} 张图片`, 'success'); + // this.addWechatLog(`成功选择了 ${result.localIds.length} 张图片`, 'success'); - // 处理选中的图片 - if (result.localIds && result.localIds.length > 0) { - await this.processWechatImages(result.localIds); - } - } catch (error) { - console.error('微信选择图片失败:', error); - this.addWechatLog(`选择图片失败: ${error.message}`, 'error'); + // // 处理选中的图片 + // if (result.localIds && result.localIds.length > 0) { + // await this.processWechatImages(result.localIds); + // } + // } catch (error) { + // console.error('微信选择图片失败:', error); + // this.addWechatLog(`选择图片失败: ${error.message}`, 'error'); - uni.showToast({ - title: '选择图片失败', - icon: 'none', - }); - } finally { - this.wechatImageLoading = false; - this.uploadProgress = 0; - } - }, + // uni.showToast({ + // title: '选择图片失败', + // icon: 'none', + // }); + // } finally { + // this.wechatImageLoading = false; + // this.uploadProgress = 0; + // } + // }, // 上传微信图片到OSS(参考stuMsgDialog.vue的实现) async uploadWechatImageToOSS(localId) { @@ -1069,112 +1069,112 @@ export default { }, // 处理微信选中的图片(支持多文件上传) - async processWechatImages(localIds) { - this.addWechatLog(`开始处理 ${localIds.length} 张微信图片...`, 'info'); + // async processWechatImages(localIds) { + // this.addWechatLog(`开始处理 ${localIds.length} 张微信图片...`, 'info'); - // 检查文件数量限制 - if (this.attachmentList.length + localIds.length > 15) { - const canUpload = 15 - this.attachmentList.length; - if (canUpload <= 0) { - uni.showToast({ - title: '最多只能上传15个文件', - icon: 'none', - }); - return; - } + // // 检查文件数量限制 + // if (this.attachmentList.length + localIds.length > 15) { + // const canUpload = 15 - this.attachmentList.length; + // if (canUpload <= 0) { + // uni.showToast({ + // title: '最多只能上传15个文件', + // icon: 'none', + // }); + // return; + // } - // 只处理允许上传的数量 - localIds = localIds.slice(0, canUpload); - uni.showToast({ - title: `已选择前${canUpload}张图片`, - icon: 'none', - duration: 2000, - }); - } + // // 只处理允许上传的数量 + // localIds = localIds.slice(0, canUpload); + // uni.showToast({ + // title: `已选择前${canUpload}张图片`, + // icon: 'none', + // duration: 2000, + // }); + // } - const totalFiles = localIds.length; - let successCount = 0; - let failureCount = 0; + // const totalFiles = localIds.length; + // let successCount = 0; + // let failureCount = 0; - for (let i = 0; i < totalFiles; i++) { - const localId = localIds[i]; + // for (let i = 0; i < totalFiles; i++) { + // const localId = localIds[i]; - // 计算总体进度 - const overallProgress = Math.round((i / totalFiles) * 100); - this.addWechatLog( - `正在处理第 ${i + 1}/${totalFiles} 张图片... (${overallProgress}%)`, - 'info', - ); + // // 计算总体进度 + // const overallProgress = Math.round((i / totalFiles) * 100); + // this.addWechatLog( + // `正在处理第 ${i + 1}/${totalFiles} 张图片... (${overallProgress}%)`, + // 'info', + // ); - try { - // 创建文件对象并添加到列表(显示上传中状态) - const file = { - uid: `wechat_${Date.now()}_${i}`, - name: `微信图片_${Date.now()}_${i + 1}.jpg`, - url: localId, - localId: localId, - status: 'uploading', - type: 'image/jpeg', - size: 0, // 微信API无法直接获取文件大小 - source: 'wechat', - progress: 0, - }; + // try { + // // 创建文件对象并添加到列表(显示上传中状态) + // const file = { + // uid: `wechat_${Date.now()}_${i}`, + // name: `微信图片_${Date.now()}_${i + 1}.jpg`, + // url: localId, + // localId: localId, + // status: 'uploading', + // type: 'image/jpeg', + // size: 0, // 微信API无法直接获取文件大小 + // source: 'wechat', + // progress: 0, + // }; - // 添加到附件列表(立即显示) - this.attachmentList.push(file); + // // 添加到附件列表(立即显示) + // this.attachmentList.push(file); - // 上传到OSS - const uploadResult = await this.uploadWechatImageToOSS(localId); + // // 上传到OSS + // const uploadResult = await this.uploadWechatImageToOSS(localId); - // 更新文件信息 - file.status = 'success'; - file.ossId = uploadResult.ossId; - file.url = uploadResult.url; - file.name = uploadResult.fileName || file.name; - file.size = uploadResult.fileSize || 0; - file.progress = 100; + // // 更新文件信息 + // file.status = 'success'; + // file.ossId = uploadResult.ossId; + // file.url = uploadResult.url; + // file.name = uploadResult.fileName || file.name; + // file.size = uploadResult.fileSize || 0; + // file.progress = 100; - successCount++; - this.addWechatLog(`✅ 第 ${i + 1} 张图片上传成功: ${file.name}`, 'success'); - } catch (error) { - console.error(`处理微信图片失败:`, error); + // successCount++; + // this.addWechatLog(`✅ 第 ${i + 1} 张图片上传成功: ${file.name}`, 'success'); + // } catch (error) { + // console.error(`处理微信图片失败:`, error); - // 更新失败的文件状态 - const failedFile = this.attachmentList.find((f) => f.localId === localId); - if (failedFile) { - failedFile.status = 'error'; - failedFile.progress = 0; - } + // // 更新失败的文件状态 + // const failedFile = this.attachmentList.find((f) => f.localId === localId); + // if (failedFile) { + // failedFile.status = 'error'; + // failedFile.progress = 0; + // } - failureCount++; - this.addWechatLog(`❌ 第 ${i + 1} 张图片上传失败: ${error.message}`, 'error'); - } + // failureCount++; + // this.addWechatLog(`❌ 第 ${i + 1} 张图片上传失败: ${error.message}`, 'error'); + // } - // 重置单个文件的进度条 - this.uploadProgress = 0; - } + // // 重置单个文件的进度条 + // this.uploadProgress = 0; + // } - // 显示最终结果 - const resultMessage = `上传完成: 成功${successCount}个,失败${failureCount}个`; - this.addWechatLog(resultMessage, successCount > 0 ? 'success' : 'warning'); + // // 显示最终结果 + // const resultMessage = `上传完成: 成功${successCount}个,失败${failureCount}个`; + // this.addWechatLog(resultMessage, successCount > 0 ? 'success' : 'warning'); - if (successCount > 0) { - uni.showToast({ - title: `成功上传${successCount}个文件`, - icon: 'success', - duration: 2000, - }); - } + // if (successCount > 0) { + // uni.showToast({ + // title: `成功上传${successCount}个文件`, + // icon: 'success', + // duration: 2000, + // }); + // } - if (failureCount > 0) { - uni.showModal({ - title: '部分文件上传失败', - content: `${failureCount}个文件上传失败,请检查网络后重试`, - showCancel: false, - confirmText: '知道了', - }); - } - }, + // if (failureCount > 0) { + // uni.showModal({ + // title: '部分文件上传失败', + // content: `${failureCount}个文件上传失败,请检查网络后重试`, + // showCancel: false, + // confirmText: '知道了', + // }); + // } + // }, // 打开地图查看位置 async openLocationInMap() { @@ -1975,16 +1975,16 @@ export default { }, // 保留原有方法作为备用(标记为废弃) - async tryWeChatLocation() { - console.warn('tryWeChatLocation 方法已废弃,请使用 tryWeChatLocationWithSDK 方法'); - return this.tryWeChatLocationWithSDK(); - }, + // async tryWeChatLocation() { + // console.warn('tryWeChatLocation 方法已废弃,请使用 tryWeChatLocationWithSDK 方法'); + // return this.tryWeChatLocationWithSDK(); + // }, // 一键定位:优先使用微信JSSDK,失败回退到uni.getLocation - async handleGetWeChatLocation() { - console.warn('handleGetWeChatLocation 方法已废弃,请使用 handleGetLocation 方法'); - return this.handleGetLocation(); - }, + // async handleGetWeChatLocation() { + // console.warn('handleGetWeChatLocation 方法已废弃,请使用 handleGetLocation 方法'); + // return this.handleGetLocation(); + // }, // 回退定位(浏览器/APP内置能力) async handleFallbackLocation() { @@ -2308,8 +2308,8 @@ export default { }); }, - // 图片下载 - async handleImageDownload(image, event) { + // 图片下载(带重试机制) + async handleImageDownload(image, event, retryCount = 0) { this.addWechatLog('=== 图片下载方法开始执行 ===', 'info'); this.addWechatLog( '图片信息', @@ -2321,6 +2321,7 @@ export default { name: image.name, url: image.url, status: image.status, + retryCount: retryCount, }, null, 2, @@ -2339,18 +2340,52 @@ export default { } this.addWechatLog('📤 开始准备下载图片...', 'info'); - // uni.showLoading({ - // title: '准备下载...', - // mask: true, - // }); + + // 显示下载进度提示 + uni.showLoading({ + title: '正在下载图片...', + mask: true, + }); try { // 如果有ossId,通过API下载 if (image.ossId) { const downloadUrl = `/resource/oss/download/${image.ossId}`; this.addWechatLog(`📥 使用API下载图片,URL: ${downloadUrl}`, 'info'); - // const downloadUrl = `/resource/oss/download/${file.ossId}`; - const response = await downloadFile(downloadUrl); + + // 调用下载接口并等待完成 + const response = await downloadFile(downloadUrl, image.name); + + if (response.statusCode === 200) { + uni.hideLoading(); + uni.saveFile({ + tempFilePath: response.tempFilePath, + success: function (res) { + uni.showToast({ + title: '下载成功', + icon: 'success', + }); + }, + fail: function () { + uni.showToast({ + title: '保存失败', + icon: 'none', + }); + }, + }); + } + // 添加短暂延迟确保下载完成(特别是H5环境) + // await new Promise((resolve) => setTimeout(resolve, 500)); + + // this.addWechatLog('✅ 图片下载完成', 'success'); + + // 隐藏loading并显示成功提示 + + // uni.showToast({ + // title: '下载完成', + // icon: 'success', + // duration: 2000, + // }); // // 在H5环境中,使用blob数据创建下载链接 // // #ifdef H5 @@ -2413,75 +2448,105 @@ export default { // }, // }); // // #endif - } else { - // 没有ossId的情况,直接使用图片URL - const imageUrl = image.blobUrl || image.url; - if (imageUrl) { - // #ifdef H5 - const link = document.createElement('a'); - link.href = imageUrl; - link.download = image.name || 'image.jpg'; - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); - - uni.hideLoading(); - uni.showToast({ - title: '下载已开始', - icon: 'success', - }); - // #endif - - // #ifndef H5 - uni.downloadFile({ - url: imageUrl, - success: (res) => { - if (res.statusCode === 200) { - uni.saveImageToPhotosAlbum({ - filePath: res.tempFilePath, - success: () => { - uni.showToast({ - title: '保存成功', - icon: 'success', - }); - }, - fail: () => { - uni.showToast({ - title: '保存失败', - icon: 'none', - }); - }, - }); - } - }, - fail: (error) => { - this.addWechatLog( - '❌ 图片下载失败', - 'error', - error.message || error.toString(), - ); - uni.showToast({ - title: '下载失败', - icon: 'none', - }); - }, - complete: () => { - uni.hideLoading(); - }, - }); - // #endif - } } + // else { + // // 没有ossId的情况,直接使用图片URL + // const imageUrl = image.blobUrl || image.url; + // if (imageUrl) { + // // #ifdef H5 + // const link = document.createElement('a'); + // link.href = imageUrl; + // link.download = image.name || 'image.jpg'; + // document.body.appendChild(link); + // link.click(); + // document.body.removeChild(link); + + // uni.hideLoading(); + // uni.showToast({ + // title: '下载已开始', + // icon: 'success', + // }); + // // #endif + + // // #ifndef H5 + // uni.downloadFile({ + // url: imageUrl, + // success: (res) => { + // if (res.statusCode === 200) { + // uni.saveImageToPhotosAlbum({ + // filePath: res.tempFilePath, + // success: () => { + // uni.showToast({ + // title: '保存成功', + // icon: 'success', + // }); + // }, + // fail: () => { + // uni.showToast({ + // title: '保存失败', + // icon: 'none', + // }); + // }, + // }); + // } + // }, + // fail: (error) => { + // this.addWechatLog( + // '❌ 图片下载失败', + // 'error', + // error.message || error.toString(), + // ); + // uni.showToast({ + // title: '下载失败', + // icon: 'none', + // }); + // }, + // complete: () => { + // uni.hideLoading(); + // }, + // }); + // // #endif + // } + // } } catch (error) { this.addWechatLog( '❌ 图片下载过程出错', 'error', error.message || error.toString(), ); + + // 确保隐藏loading状态 uni.hideLoading(); + + // 重试机制(最多重试2次) + if (retryCount < 2) { + this.addWechatLog(`🔄 准备进行第${retryCount + 1}次重试...`, 'warning'); + + // 显示重试提示 + uni.showToast({ + title: `下载失败,正在重试(${retryCount + 1}/2)...`, + icon: 'none', + duration: 1500, + }); + + setTimeout(() => { + this.handleImageDownload(image, event, retryCount + 1); + }, 1500); // 延迟1.5秒后重试 + + return; + } + + // 超过重试次数,显示最终错误信息 + const errorMsg = error.message?.includes('网络') + ? '网络连接失败,请检查网络后重试' + : error.message?.includes('timeout') + ? '下载超时,请重试' + : '下载失败,已重试2次,请检查网络或稍后重试'; + uni.showToast({ - title: '下载失败', + title: errorMsg, icon: 'none', + duration: 3000, }); } this.addWechatLog('=== 图片下载方法执行完成 ===', 'success'); @@ -2497,9 +2562,9 @@ export default { target.tagName === 'BUTTON'; // 如果点击的是操作按钮区域,不触发预览 - if (!isActionButton) { - this.handleImagePreview(image, index); - } + // if (!isActionButton) { + // this.handleImagePreview(image, index); + // } }, // 删除按钮点击处理(更强的事件处理) @@ -2762,19 +2827,19 @@ export default { case 0: this.chooseDocuments(); break; - case 1: - this.chooseImages(); - break; - case 2: - if (this.isWechatBrowser) { - this.chooseWechatImage(); - } else { - uni.showToast({ - title: '请在微信中使用', - icon: 'none', - }); - } - break; + // case 1: + // this.chooseImages(); + // break; + // case 2: + // if (this.isWechatBrowser) { + // this.chooseWechatImage(); + // } else { + // uni.showToast({ + // title: '请在微信中使用', + // icon: 'none', + // }); + // } + // break; } }, fail: () => { @@ -2784,36 +2849,36 @@ export default { }, // 选择图片文件 - chooseImages() { - uni.chooseImage({ - count: Math.min(9, 15 - this.attachmentList.length), - sizeType: ['original', 'compressed'], - sourceType: ['album', 'camera'], - success: (res) => { - console.log('选择图片成功:', res); - const files = res.tempFilePaths.map((path, index) => { - const tempFile = res.tempFiles?.[index]; - return { - name: path.split('/').pop() || `image_${Date.now()}_${index}.jpg`, - path: path, - url: path, - tempFilePath: path, - size: tempFile?.size || 0, - type: tempFile?.type || 'image/jpeg', - }; - }); + // chooseImages() { + // uni.chooseImage({ + // count: Math.min(9, 15 - this.attachmentList.length), + // sizeType: ['original', 'compressed'], + // sourceType: ['album', 'camera'], + // success: (res) => { + // console.log('选择图片成功:', res); + // const files = res.tempFilePaths.map((path, index) => { + // const tempFile = res.tempFiles?.[index]; + // return { + // name: path.split('/').pop() || `image_${Date.now()}_${index}.jpg`, + // path: path, + // url: path, + // tempFilePath: path, + // size: tempFile?.size || 0, + // type: tempFile?.type || 'image/jpeg', + // }; + // }); - this.handleFileUpload({ tempFiles: files }); - }, - fail: (err) => { - console.error('选择图片失败:', err); - uni.showToast({ - title: '选择图片失败', - icon: 'none', - }); - }, - }); - }, + // this.handleFileUpload({ tempFiles: files }); + // }, + // fail: (err) => { + // console.error('选择图片失败:', err); + // uni.showToast({ + // title: '选择图片失败', + // icon: 'none', + // }); + // }, + // }); + // }, // 选择文档文件 chooseDocuments() { @@ -2821,7 +2886,6 @@ export default { if (typeof uni.chooseFile === 'function') { uni.chooseFile({ count: Math.min(5, 15 - this.attachmentList.length), - extension: ['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.txt', '.zip', '.rar'], success: (res) => { console.log('选择文档成功:', res); if (res.tempFiles && res.tempFiles.length > 0) { @@ -3395,71 +3459,95 @@ export default { // 下载图片并保存到相册 async downloadAndSaveImage(downloadUrl, fileName) { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { // #ifdef MP-WEIXIN // 微信小程序:使用downloadFile下载到临时文件,然后保存到相册 - uni.downloadFile({ - url: this.getFullApiUrl(downloadUrl), - header: this.getRequestHeaders(), - success: (res) => { - console.log('✅ 图片下载成功:', res); - if (res.statusCode === 200) { - // 保存到相册 - uni.saveImageToPhotosAlbum({ - filePath: res.tempFilePath, - success: () => { - console.log('✅ 图片保存到相册成功'); - uni.hideLoading(); - uni.showToast({ - title: '图片已保存到相册', - icon: 'success', - duration: 2000, - }); - resolve(); - }, - fail: (err) => { - console.error('❌ 保存图片到相册失败:', err); - uni.hideLoading(); - if (err.errMsg.includes('auth deny')) { - uni.showModal({ - title: '权限提示', - content: - '需要授权访问相册才能保存图片,请在设置中开启权限', - showCancel: false, - }); - } else { - uni.showToast({ - title: '保存图片失败', - icon: 'none', - }); - } - reject(err); - }, - }); - } else { - uni.hideLoading(); - uni.showToast({ - title: '图片下载失败', - icon: 'none', - }); - reject(new Error('下载失败')); - } - }, - fail: (error) => { - console.error('❌ 图片下载失败:', error); - uni.hideLoading(); - uni.showToast({ - title: '图片下载失败', - icon: 'none', - }); - reject(error); - }, - }); + // uni.downloadFile({ + // url: this.getFullApiUrl(downloadUrl), + // header: this.getRequestHeaders(), + // success: (res) => { + // console.log('✅ 图片下载成功:', res); + // if (res.statusCode === 200) { + // // 保存到相册 + // uni.saveImageToPhotosAlbum({ + // filePath: res.tempFilePath, + // success: () => { + // console.log('✅ 图片保存到相册成功'); + // uni.hideLoading(); + // uni.showToast({ + // title: '图片已保存到相册', + // icon: 'success', + // duration: 2000, + // }); + // resolve(); + // }, + // fail: (err) => { + // console.error('❌ 保存图片到相册失败:', err); + // uni.hideLoading(); + // if (err.errMsg.includes('auth deny')) { + // uni.showModal({ + // title: '权限提示', + // content: + // '需要授权访问相册才能保存图片,请在设置中开启权限', + // showCancel: false, + // }); + // } else { + // uni.showToast({ + // title: '保存图片失败', + // icon: 'none', + // }); + // } + // reject(err); + // }, + // }); + // } else { + // uni.hideLoading(); + // uni.showToast({ + // title: '图片下载失败', + // icon: 'none', + // }); + // reject(new Error('下载失败')); + // } + // }, + // fail: (error) => { + // console.error('❌ 图片下载失败:', error); + // uni.hideLoading(); + // uni.showToast({ + // title: '图片下载失败', + // icon: 'none', + // }); + // reject(error); + // }, + // }); // #endif // #ifdef H5 // H5环境:直接使用浏览器下载 - this.downloadFileInBrowser(downloadUrl, fileName); + // this.downloadFileInBrowser(downloadUrl, fileName); + // const downloadUrl = `/resource/oss/download/${image.ossId}`; + this.addWechatLog(`📥 使用API下载图片,URL: ${downloadUrl}`, 'info'); + + // 调用下载接口并等待完成 + const response = await downloadFile(downloadUrl, fileName); + + if (response.statusCode === 200) { + uni.hideLoading(); + uni.saveFile({ + tempFilePath: response.tempFilePath, + success: function (res) { + uni.showToast({ + title: '下载成功', + icon: 'success', + }); + }, + fail: function () { + uni.showToast({ + title: '保存失败', + icon: 'none', + }); + }, + }); + } uni.hideLoading(); resolve(); // #endif @@ -3468,73 +3556,94 @@ export default { // 下载文档并打开预览 async downloadAndOpenDocument(downloadUrl, fileName) { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { // #ifdef MP-WEIXIN // 微信小程序:下载到临时文件并直接打开 - uni.downloadFile({ - url: this.getFullApiUrl(downloadUrl), - header: this.getRequestHeaders(), - success: (res) => { - console.log('✅ 文档下载成功:', res); - if (res.statusCode === 200) { - uni.hideLoading(); - // 直接打开文档预览 - uni.openDocument({ - filePath: res.tempFilePath, - fileType: this.getFileType(fileName), - success: () => { - console.log('✅ 文档打开成功'); - resolve(); - }, - fail: (err) => { - console.error('❌ 文档打开失败:', err); - // 如果无法直接打开,提供其他选项 - uni.showModal({ - title: '无法预览', - content: '该文件格式不支持预览,您可以选择其他操作', - showCancel: true, - cancelText: '取消', - confirmText: '分享文件', - success: (modalRes) => { - if (modalRes.confirm) { - // 可以通过分享功能让用户保存文件 - // #ifdef MP-WEIXIN - wx.shareFileMessage && - wx.shareFileMessage({ - filePath: res.tempFilePath, - fileName: fileName, - }); - // #endif - } - }, - }); - reject(err); - }, - }); - } else { - uni.hideLoading(); + // uni.downloadFile({ + // url: this.getFullApiUrl(downloadUrl), + // header: this.getRequestHeaders(), + // success: (res) => { + // console.log('✅ 文档下载成功:', res); + // if (res.statusCode === 200) { + // uni.hideLoading(); + // // 直接打开文档预览 + // uni.openDocument({ + // filePath: res.tempFilePath, + // fileType: this.getFileType(fileName), + // success: () => { + // console.log('✅ 文档打开成功'); + // resolve(); + // }, + // fail: (err) => { + // console.error('❌ 文档打开失败:', err); + // // 如果无法直接打开,提供其他选项 + // uni.showModal({ + // title: '无法预览', + // content: '该文件格式不支持预览,您可以选择其他操作', + // showCancel: true, + // cancelText: '取消', + // confirmText: '分享文件', + // success: (modalRes) => { + // if (modalRes.confirm) { + // // 可以通过分享功能让用户保存文件 + // // #ifdef MP-WEIXIN + // wx.shareFileMessage && + // wx.shareFileMessage({ + // filePath: res.tempFilePath, + // fileName: fileName, + // }); + // // #endif + // } + // }, + // }); + // reject(err); + // }, + // }); + // } else { + // uni.hideLoading(); + // uni.showToast({ + // title: '文档下载失败', + // icon: 'none', + // }); + // reject(new Error('下载失败')); + // } + // }, + // fail: (error) => { + // console.error('❌ 文档下载失败:', error); + // uni.hideLoading(); + // uni.showToast({ + // title: '文档下载失败', + // icon: 'none', + // }); + // reject(error); + // }, + // }); + // // #endif + + // // #ifdef H5 + // // H5环境:直接使用浏览器下载 + // this.downloadFileInBrowser(downloadUrl, fileName); + // 调用下载接口并等待完成 + const response = await downloadFile(downloadUrl, fileName); + + if (response.statusCode === 200) { + uni.hideLoading(); + uni.saveFile({ + tempFilePath: response.tempFilePath, + success: function (res) { uni.showToast({ - title: '文档下载失败', + title: '下载成功', + icon: 'success', + }); + }, + fail: function () { + uni.showToast({ + title: '保存失败', icon: 'none', }); - reject(new Error('下载失败')); - } - }, - fail: (error) => { - console.error('❌ 文档下载失败:', error); - uni.hideLoading(); - uni.showToast({ - title: '文档下载失败', - icon: 'none', - }); - reject(error); - }, - }); - // #endif - - // #ifdef H5 - // H5环境:直接使用浏览器下载 - this.downloadFileInBrowser(downloadUrl, fileName); + }, + }); + } uni.hideLoading(); resolve(); // #endif