复制及预览优化
This commit is contained in:
@@ -12,7 +12,8 @@ Page({
|
||||
remark: '',
|
||||
actualAccount: 0,
|
||||
activeValues: [0, 1, 2, 3, 4],
|
||||
demandInfo: null
|
||||
demandInfo: null,
|
||||
copyUrl: '' // 剪贴板复制失败时,弹出可长按选择的链接
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -41,41 +42,131 @@ 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] : '';
|
||||
},
|
||||
|
||||
//文件预览
|
||||
fileView(e) {
|
||||
const fileUrl = e.currentTarget.dataset.url;
|
||||
if (!fileUrl) {
|
||||
wx.showToast({
|
||||
title: '文件链接为空',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
const fileType = this.getFileType(fileUrl);
|
||||
// 指定带扩展名的保存路径:安卓/鸿蒙靠扩展名识别文件类型,
|
||||
// downloadFile 默认的 tempFilePath 无扩展名,容易打开失败
|
||||
const filePath = `${wx.env.USER_DATA_PATH}/demand_file_${Date.now()}.${fileType}`;
|
||||
wx.showLoading({
|
||||
title: '加载中...',
|
||||
mask: true
|
||||
});
|
||||
wx.downloadFile({
|
||||
url: fileUrl,
|
||||
success: function (res) {
|
||||
const filePath = res.tempFilePath
|
||||
filePath: filePath,
|
||||
success: (res) => {
|
||||
wx.hideLoading();
|
||||
if (res.statusCode !== 200) {
|
||||
wx.showToast({
|
||||
title: '文件加载失败(' + res.statusCode + ')',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
const openPath = res.filePath || res.tempFilePath || filePath;
|
||||
console.log('openDocument', { openPath, fileType });
|
||||
wx.openDocument({
|
||||
filePath: filePath,
|
||||
success: function (res) {
|
||||
console.log('打开文档成功')
|
||||
filePath: openPath,
|
||||
fileType: fileType,
|
||||
showMenu: true, // 支持转发/用其他应用打开
|
||||
success: () => {
|
||||
console.log('打开文档成功');
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('openDocument fail', err);
|
||||
// 安卓/鸿蒙部分机型无法直接预览,降级为复制链接到浏览器打开
|
||||
wx.showModal({
|
||||
title: '预览失败',
|
||||
content: '当前系统无法直接预览该文件,是否复制链接后在浏览器中打开?',
|
||||
confirmText: '复制链接',
|
||||
cancelText: '关闭',
|
||||
success: (res2) => {
|
||||
if (res2.confirm) {
|
||||
this.copyUrlToClipboard(fileUrl);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
//文件复制
|
||||
fileUrlCopy(e) {
|
||||
const url = e.currentTarget.dataset.url;
|
||||
wx.setClipboardData({
|
||||
data: url,
|
||||
success: function () {
|
||||
wx.showToast({
|
||||
title: '下载链接已复制',
|
||||
icon: 'success'
|
||||
});
|
||||
},
|
||||
fail: function () {
|
||||
wx.showToast({
|
||||
title: '下载链接复制失败',
|
||||
icon: 'none'
|
||||
fail: (err) => {
|
||||
wx.hideLoading();
|
||||
console.error('downloadFile fail', err);
|
||||
// 真机/体验版常见原因:downloadFile 合法域名未配置(开发者工具可关闭校验,真机不行)
|
||||
const host = this.getUrlHost(fileUrl);
|
||||
wx.showModal({
|
||||
title: '预览失败',
|
||||
showCancel: false,
|
||||
content: '文件下载失败(' + (err && err.errMsg ? err.errMsg : '未知错误') + '),请检查网络' + (host ? ';请确认已配置 downloadFile 合法域名:' + host : '')
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 复制链接(失败自动重试一次,仍失败则弹出可长按选择的链接供手动复制)
|
||||
copyUrlToClipboard(url) {
|
||||
if (!url) {
|
||||
wx.showToast({
|
||||
title: '文件链接为空',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
const doCopy = (retry) => {
|
||||
wx.setClipboardData({
|
||||
data: String(url),
|
||||
success: () => {
|
||||
wx.showToast({
|
||||
title: '下载链接已复制',
|
||||
icon: 'success'
|
||||
});
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('setClipboardData fail', err);
|
||||
if (retry > 0) {
|
||||
// 系统剪贴板偶发写入失败,短暂延迟后重试一次
|
||||
setTimeout(() => doCopy(retry - 1), 200);
|
||||
return;
|
||||
}
|
||||
// 降级:展示可长按选择的链接,用户手动复制
|
||||
this.setData({ copyUrl: String(url) });
|
||||
}
|
||||
});
|
||||
};
|
||||
doCopy(1);
|
||||
},
|
||||
|
||||
//文件复制
|
||||
fileUrlCopy(e) {
|
||||
this.copyUrlToClipboard(e.currentTarget.dataset.url);
|
||||
},
|
||||
|
||||
// 关闭手动复制弹层
|
||||
closeCopyModal() {
|
||||
this.setData({ copyUrl: '' });
|
||||
},
|
||||
handleChange(e) {
|
||||
console.log(e.detail.value);
|
||||
this.setData({
|
||||
|
||||
Reference in New Issue
Block a user