使用方法看下列代码:
php:action如下
/** * # +======================================================================== * # | - @name 初始化参数 * # | - @author cq <just_leaf@foxmail.com> * # | - @copyright zmtek 2018-11-12 * # +------------------------------------------------------------------------ * # | - 1.初始化传参值页面 * # +======================================================================== */ public function _initialize() { # 引入 Vendor('WxJDK.jssdk'); # 公众号获取 $appid = C('APPID'); # 公众号获取 $appSecret = C('APPSECRET'); # 实例化 $wx = new \JSSDK($appid,$appSecret); # 获取参数 $info = $wx-> getWxConfig(); # 传参页面 $this -> assign('wxConfig',$info); }
html:页面如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/> <title>微信扫一扫</title> </head> <body> <h1 id="wxcode" style="text-align: center;">扫一扫</h1> </body> <script src="__ROOT__/statics/Wxshop/js/jquery.min.js" type="text/javascript"></script> <script src='https://res.wx.qq.com/open/js/jweixin-1.4.0.js'></script> <script> wx.config({ // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 debug: false, // 必填,公众号的唯一标识 appId: "{$wxConfig.appId}", // 必填,生成签名的时间戳 timestamp:"{$wxConfig.timestamp}", // 必填,生成签名的随机串 nonceStr:"{$wxConfig.nonceStr}", // 必填,签名,见附录1 signature:"{$wxConfig.signature}", // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 jsApiList : [ 'scanQRCode' ] }); wx.error(function(res) { // alert("----------出错了-----------:" + res.errMsg);//这个地方的好处就是wx.config配置错误,会弹出窗口哪里错误,然后根据微信文档查询即可。 }); wx.ready(function() { wx.checkJsApi({ jsApiList : ['scanQRCode'], success : function(res) { } }); //点击按钮扫描二维码 $('#wxcode').click(function(){ // alert(1); wx.scanQRCode({ needResult: 0, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果, scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都有 success: function (res) { var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果 } }); }) }); </script> </html>