浏览器h5唤起微信小程序相应的页面

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title>判断手机是否安装微信</title>
</head>

<body>

    <!--<a href="javascript:testApp('weixin://dl/business/?t=Q7YHRPNermq')" class="dl-btn" id="download">打开微信</a>-->
    <script>
    window.onload = function () {

        // Get the BootstrapValidator instance

        let url = "https://quantum.allwinnn.com/index/getScheme?path=pages/frame/index";
        // 把参数对象转换为json
        let param = {
            path: 'pages/frame/index'
        };
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 &&  xhr.status === 200) {
                /** 你的逻辑代码 **/
                this.response
                console.log(this.response)

                var url = this.response.data.openlink
                // var url = 'weixin://dl/business/?t=9ALQaakaYQg'
                var timeout, t = 1000, hasApp = true;
                    // setTimeout(function () {
                    //     if (!hasApp) {
                    //         //没有安装微信
                    //         var r = confirm("您没有安装微信,请先安装微信!");
                    //         if (r == true) {
                    //             location.href = "http://weixin.qq.com/"
                    //         }
                    //     } else {
                    //         //安装微信
                    //     }
                    //     document.body.removeChild(ifr);
                    // }, 2000)
        
                    var t1 = Date.now();
                    var ifr = document.createElement("iframe");
                    ifr.setAttribute('src', url);
                    ifr.setAttribute('style', 'display:none');
                    document.body.appendChild(ifr);
                    timeout = setTimeout(function () {
                        var t2 = Date.now();
                        if (!t1 || t2 - t1 < t + 100) {
                            hasApp = false;
                        }
                    }, t);
            }
        };
        xhr.open(
            "get",
            url,
            true
        );
        xhr.responseType = "json";
        // 注意,设置请求头的信息必须写在下面,否则会报错
        // 设置以json传参
        // xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
        // 解决跨域问题
        // xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
        // 设置请求体携带的参数
        xhr.send();
        

        
    };
    </script>
</body>

</html>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title>判断手机是否安装微信</title>
</head>

<body>

    <!--<a href="javascript:testApp('weixin://dl/business/?t=Q7YHRPNermq')" class="dl-btn" id="download">打开微信</a>-->
    <script>
    window.onload = function () {

        // Get the BootstrapValidator instance

        let url = "https://quantum.allwinnn.com/index/getScheme?path=pages/frame/index";
        // 把参数对象转换为json
        let param = {
            path: 'pages/frame/index'
        };
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 &&  xhr.status === 200) {
                /** 你的逻辑代码 **/
                this.response
                console.log(this.response)

                var url = this.response.data.openlink
                // var url = 'weixin://dl/business/?t=9ALQaakaYQg'
                try {
                    window.location.href = url; //(安卓)
                } catch (error) {
                    window.open(url); //(iOS)
                }
            }
        };
        xhr.open(
            "get",
            url,
            true
        );
        xhr.responseType = "json";
        // 注意,设置请求头的信息必须写在下面,否则会报错
        // 设置以json传参
        // xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
        // 解决跨域问题
        // xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
        // 设置请求体携带的参数
        xhr.send();
        

        
    };
    </script>
</body>

</html>

 

posted @ 2023-06-14 16:45  归一山人  阅读(39)  评论(0编辑  收藏  举报