微信小程序开发微信小程序开发者小程序

小程序中打开网页和pdf

2018-08-19  本文已影响14人  王炳祺

打开网页的方法有两种第一种是最简单的微信官方提供的方法,直接把要打开的网页地址赋给web-view标签的src属性

     <web-view src="{{article}}"> </web-view>

第二种需要引入一个第三方插件,下面的写法只适用于wepy框架中,其他框架中写法略有不同。

<template>
<view>
    //插件中的固定写法
    <import src="../../wxParse/wxParse.wxml" />
    <template is="wxParse" data="{{wxParseData:article.nodes}}" />
    <view wx:if='article' class='addclass'></view>
</view>
</template>
<script>
//引入插件
import WxParse from "../../wxParse/wxParse";
export default class webview extends wepy.page {
    data = {
        //网页地址路径
        article: '',
    }
    methods = {
    }
    async onLoad(options) {
        let ret = await api.rentalContracts({
            id: this.id,
            method: 'GET'
        });
        this.article = ret.data
        //调用插件中的方法设置article中的网页路径
        WxParse.wxParse('article', 'html', this.article, this, 1);
    }
}

打开phf文件给按钮定义一个preview方法,在downloadFile方法中调用wx.openDocument方法就可以实现。

       preview() {
            let that=this.
            wx.downloadFile({
                url: 'https://www.*******.com/contract/default/pdf',
                success: function(res) {
                    console.log(res)
                    var Path = res.tempFilePath //返回的文件临时地址,用于后面打开本地预览所用
                    that.webview=Path
                    wx.openDocument({
                        filePath: Path,
                        success: function(res) {
                            console.log('打开文档成功')
                        }
                    })
                },
                fail: function(res) {
                    console.log(res)
                }
            })
        },
上一篇下一篇

猜你喜欢

热点阅读