微信浏览器中H5使用wx-open-launch-app打开第三方APP

1、在微信开放平台配置关联相关APP、微信小程序、微信公众号;

2、在微信公众号中设置安全域名;

3、在开放者平台中的APP配置关联JS网页安全域名;

4、把相关Html页面代码放到对应域名下;

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <title>Demo</title>
</head>

<body>
    <div id="app">
        <wx-open-launch-weapp path="pages/index/index" id="launch-wxapp" username="gh_12">
            <template>
                <style>
                    .btn {
                        padding: 12px
                    }
                </style>
                <button class="btn-download">点击打开小程序</button>
            </template>
        </wx-open-launch-weapp>
        <wx-open-launch-app id="launch-btn" appid="wx12" extinfo='{"PageType":1001}'>
            <script type="text/wxtag-template">
                <style>
                    .btn {
                        padding: 12px
                    }
                </style>
                <button class="btn">点击打开APP</button>
        </script>
        </wx-open-launch-app>
    </div>
</body>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js "></script>
<script>
    getAppList()

    function getAppList() {
        var link = location.href;
        let _params = {
            url: link
        };

        $.ajax({
            url: "https://demo.com/WeiXinService.svc/GetWxInfo",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify(_params),
            success: function (data) {
                const result = data;
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                    appId: result.appid, // 必填,公众号的唯一标识
                    timestamp: result.timestamp, // 必填,生成签名的时间戳
                    nonceStr: result.noncestr, // 必填,生成签名的随机串
                    signature: result.signature, // 必填,签名
                    jsApiList: ['onMenuShareTimeline', 'chooseImage'], // 必填,需要使用的JS接口列表
                    openTagList: ['wx-open-launch-weapp', 'wx-open-launch-app'] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
                })
                wx.ready(function (res) {
                    // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中
                    var btn = document.getElementById('launch-btn');
                    btn.addEventListener('launch', function (e) {
                        console.log('success', e);
                    });
                    btn.addEventListener('error', function (e) {
                        console.log('fail', e.detail);
                        //打开失败则跳转到下载页
                        if (e.detail.errMsg == 'launch:fail') {
                            if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
                                window.location = "https://itunes.apple.com/"; //ios app下载地址  
                            } else if (navigator.userAgent.match(/android/i)) {
                                window.location = "https://demo.com/download.html"; //android app下载地址  
                            }
                        }
                    });
                });
                wx.error(function (res) {
                    // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名
                });
            }

        });
    }
</script>

</html>

 

posted @ 2021-08-10 18:10  web_liu  阅读(2124)  评论(0编辑  收藏  举报