HTML——微信浏览器H5页面调用微信扫一扫

使用JS-SDK调用微信扫一扫,需要有公众号支持,通过公众号生成JS-SDK使用权限签名。本文直接调用已封装好的接口来获取随机数(noncestr)、时间戳(timestamp)、签名(signature),接口地址:http://xx.xx.com/xxmap/wechat/jsapi/getSignature.do?appId=APPID&url=URL。appId是公众号的标识,url是需要调用微信扫一扫的页面的地址,需要进行encodeURIComponent编码。

调用微信扫一扫前端页面代码:

<html lang="en">

<head>

    <meta charset="UTF-8">

    <script src="plug-in/qrcode/jquery.min.js"></script>

<script src=”http://res.wx.qq.com/open/js/jweixin-1.0.0.js”></script>

    <title>微信扫一扫</title>

</head>

<body>

<script type="application/javascript">

    var purl = encodeURIComponent(location.href);

    $.ajax({

        url:"voucherController.do?getSignature",

        data:{"url":purl},

        type:"POST",

        dataType:"json",

        success:function(result){

            wx.config({

                debug:false,

                appId:result.obj.appId,

                timestamp:result.obj.timestamp,

                nonceStr:result.obj.noncestr,

                signature:result.obj.signature,

                jsApiList:['scanQRCode']

            });

        },

        error:function(){

            alert("请求失败");

        }

    });

    function scanQr(){

        var ua = navigator.userAgent.toLowerCase();

        var isWeiXin  =ua.indexOf('micromessenger') != -1;

        if(!isWeiXin){

            alert('请用微信打开链接,才可使用扫一扫!');

        }

        wx.scanQRCode({

            needResult:1,

            scanType:["qrCode"],

            success:function (res) {

                var scan = res.resultStr;

                location.href=scan;

            },

            error:function(res) {

                if(res.errMsg.indexOf('function_not_exist') > 0){

                    alert('当前版本低,请进行升级!');

                }

            }

        });

    }

    wx.error(function(res){

        alert(res.errMsg);

    });

    //$(function(){

     //   setTimeout("scanQr()","1000");

    //});

</script>

<input type="button" οnclick="scanQr()" id="test" value="微信扫一扫" style="font-size: 30px;border: solid 1px;padding: 10px;"/>

</body>

</html>

调用微信扫一扫后端代码:

@RequestMapping(params = “getSignature”)

@ResponseBody

public AjaxJson getSignature(String url){

AjaxJson j = new AjaxJson();

String sigUrl = “http://xx.xx.com/xxmap/wechat/jsapi/getSignature.do?appId=APPID&url=” + url;

//Get请求接口,获取随机数、时间戳、签名

JSONObject jsonObject = HttpRequest.sendGet(sigUrl);

JSONObject jsonObj = JSON.parseObject(jsonObject.getString(“result”));

jsonObj.put(“appId” ,APPID);//公众号appid

j.setObj(jsonObj);

return j;

}

 

注意:jweixin-1.0.0.js的引用,使用vConsole调试,IOS系统可能会出现找不到变量:wx异常,这个时候可以直接把整个jweixin-1.0.0.js复制到你的JS中,就可以解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ddm01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值