/** * 统一文件预览处理 * @param url 文件链接 * @param name 文件名(可选,用于提示) */ export const previewFile = (url: string, name?: string) => { if (!url) { return uni.showToast({ title: '文件链接不存在', icon: 'none' }); } // #ifdef H5 window.open(url); // #endif // #ifndef H5 uni.showLoading({ title: name ? `正在打开 ${name}...` : '正在打开文件...' }); uni.downloadFile({ url, success: (res) => { if (res.statusCode === 200) { uni.openDocument({ filePath: res.tempFilePath, showMenu: true, success: () => uni.hideLoading(), fail: () => { uni.hideLoading(); uni.showToast({ title: '当前平台暂不支持打开该类型文件', icon: 'none' }); } }); } else { uni.hideLoading(); uni.showToast({ title: '下载失败', icon: 'none' }); } }, fail: () => { uni.hideLoading(); uni.showToast({ title: '网络异常,下载失败', icon: 'none' }); } }); // #endif };