复制及预览优化
This commit is contained in:
@@ -240,6 +240,19 @@ Page({
|
||||
});
|
||||
},
|
||||
|
||||
// 取文件扩展名对应的 openDocument 类型(默认 pdf)
|
||||
getFileType(url) {
|
||||
const ext = String(url || '').split('?')[0].split('#')[0].split('.').pop().toLowerCase();
|
||||
const support = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'];
|
||||
return support.indexOf(ext) > -1 ? ext : 'pdf';
|
||||
},
|
||||
|
||||
// 取域名,用于下载失败时提示配置合法域名
|
||||
getUrlHost(url) {
|
||||
const match = String(url || '').match(/^https?:\/\/([^/:?#]+)/);
|
||||
return match ? match[1] : '';
|
||||
},
|
||||
|
||||
// 附件预览:客户点击开发者上传的附件进行查看
|
||||
handlePreviewAttach(e) {
|
||||
// 仅客户侧可预览;开发者侧附件可删除,避免删除时误触预览
|
||||
@@ -260,16 +273,22 @@ Page({
|
||||
return;
|
||||
}
|
||||
// 其他文件下载后打开
|
||||
const fileType = this.getFileType(fileUrl);
|
||||
// 指定带扩展名的保存路径:安卓/鸿蒙靠扩展名识别文件类型,默认 tempFilePath 无扩展名易失败
|
||||
const filePath = `${wx.env.USER_DATA_PATH}/contract_file_${Date.now()}.${fileType}`;
|
||||
wx.showLoading({ title: '加载中...' });
|
||||
wx.downloadFile({
|
||||
url: fileUrl,
|
||||
filePath: filePath,
|
||||
success: (res) => {
|
||||
wx.hideLoading();
|
||||
if (res.statusCode === 200) {
|
||||
wx.openDocument({
|
||||
filePath: res.tempFilePath,
|
||||
filePath: res.filePath || res.tempFilePath || filePath,
|
||||
fileType: fileType,
|
||||
showMenu: true,
|
||||
fail: () => {
|
||||
fail: (err) => {
|
||||
console.error('openDocument fail', err);
|
||||
wx.showToast({ title: '该文件类型暂不支持预览', icon: 'none' });
|
||||
}
|
||||
});
|
||||
@@ -277,9 +296,16 @@ Page({
|
||||
wx.showToast({ title: '附件加载失败', icon: 'none' });
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
fail: (err) => {
|
||||
wx.hideLoading();
|
||||
wx.showToast({ title: '附件加载失败', icon: 'none' });
|
||||
console.error('downloadFile fail', err);
|
||||
// 真机常见原因:downloadFile 合法域名未配置
|
||||
const host = this.getUrlHost(fileUrl);
|
||||
wx.showModal({
|
||||
title: '预览失败',
|
||||
showCancel: false,
|
||||
content: '附件下载失败,请检查网络' + (host ? ';若为体验版/真机,请确认已配置 downloadFile 合法域名:' + host : '')
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user