微信扫一扫接口

<!DOCTYPE html>
<html>
<head lang="en">
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
   <title>调用扫一扫</title>
</head>
<body>
<div id="scanQRCode">点我调用扫一扫</div>
</body>
<script type="text/javascript" src="../../js/common/jquery-3.2.1.min.js"></script>
<script src='https://res.wx.qq.com/open/js/jweixin-1.0.0.js'></script>
<script>
    $.ajax({
        type : "post",
        url : "后台接口?originUrl=" + window.location.href,
        data : {
        },
        dataType : "json",
        success : function(data) {
            wx.config({
                debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId: data.appId, // 必填,公众号的唯一标识
                timestamp: data.timestamp, // 必填,生成签名的时间戳
                nonceStr: data.nonceStr, // 必填,生成签名的随机串
                signature: data.signature,// 必填,签名,见附录1
                jsApiList: [
                    'checkJsApi',
                    'startRecord',
                    'stopRecord',
                    'translateVoice',
                    'scanQRCode',// 微信扫一扫接口
                    'openCard'
                ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
            });
            wx.error(function(res) {
                alert("出错了:" + res.errMsg);//这个地方的好处就是wx.config配置错误,会弹出窗口哪里错误,然后根据微信文档查询即可。
            });

            wx.ready(function() {
                wx.checkJsApi({
                    jsApiList : ['scanQRCode'],
                    success : function(res) {

                    }
                });
            });
            //点击按钮扫描二维码
            $("#scanQRCode").click(function(){
                wx.scanQRCode({
                    needResult : 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
                    scanType : [ "qrCode"], // 可以指定扫二维码还是一维码,默认二者都有
                    success : function(res) {
                        alert(JSON.stringify(res));
                        var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
                        window.location.href = result;//因为我这边是扫描后有个链接,然后跳转到该页面
                    },
                    error : function(){
                        console.log('123');
                    }
                });
            });
        }
    });


</script>
</html>

猜你喜欢

转载自blog.csdn.net/Neil_1993/article/details/84633749
今日推荐