微信公众平台 =》 开发管理 =》 开发设置 =》业务域名

注意:配置为业务域名后,可调用web-view组件在小程序中打开

注意:需要区分是安卓还是ios,ios可以直接使用webview打开pdf文件,安卓需要先下载再预览

// 打开pdf文件
// url pdf文件地址  title pdf文件名称
export const downloadFileFunc = (url = '', title = '') => {
    const reg = /(\w*)《(.*)》/g;
    let str = url.split('/')[url.split('/').length - 1].split('.')[0];
    const titleClone = title ? title.replace(reg, '$1 $2 ') : str;
        uni.getSystemInfo({
            success: function(sysRes) {
                if (sysRes.platform === 'ios') {
                    jumpPage(
                        `/pages/webview/index?title=${encodeURIComponent(title)}&url=${encodeURIComponent(url)}`
                    );
                } else {
                    uni.downloadFile({
                        url: url,
                        success: function(res) {
                            const filePath = res.tempFilePath
                            let arr = filePath.split('.');
                            const mime = arr[arr.length - 1];

                            const fs = uni.getFileSystemManager();
                            // 修改文件名字,仅安卓可以,ios无权限
                            fs.rename({
                                oldPath: filePath,
                                newPath: `${wx.env.USER_DATA_PATH}/${titleClone}`,
                                success() {
                                    uni.openDocument({
                                        filePath: `${wx.env.USER_DATA_PATH}/${titleClone}`,
                                        fileType: mime,
                                        success: function(res) {
                                            console.log('打开文档成功')
                                        },
                                        fail: function() {
                                            uni.showToast({
                                                title: '打开文件失败',
                                                icon: 'none',
                                                duration: 2000
                                            });
                                        }
                                    })
                                },
                                fail: function() {
                                    uni.showToast({ title: '保存文件失败', icon: 'none', duration: 2000 });
                                },
                            });
                        },
                        fail:function(error){
                            uni.showToast({
                                title: '下载文件失败',
                                icon: 'none',
                                duration: 2000
                            });
                        }
                    })
                }
            },
                fail: function(err) {
                    uni.showToast({
                        title: '获取系统信息失败',
                        icon: 'none',
                        duration: 2000
                    });
                }
            })
        };
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐