// 微信JSSDK的AccessToken请求URL地址ublic final static String weixin_jssdk_acceToken_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=公众号appid&secret=众号appsecret;
// 微信JSSDK的ticket请求URL地址
public final static String weixin_jssdk_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
/*** 获取微信JSSDK的access_token * @author Benson*/public static String getJSSDKAccessToken() { String returnString="";String requestUrl = weixin_jssdk_acceToken_url; JSONObject jsonObject = httpRequest(requestUrl, "GET", null); //Http GET请求// 如果请求成功 if (null != jsonObject) { try { returnString=jsonObject.getString("access_token"); } catch (JSONException e) { returnString = null; } } return returnString; } /*** 获取微信JSSDK的ticket * @author Benson*/public static String getJSSDKTicket(String access_token) { String returnString="";String requestUrl = weixin_jssdk_ticket_url.replace("ACCESS_TOKEN", access_token); JSONObject jsonObject = httpRequest(requestUrl, "GET", null); // 如果请求成功 if (null != jsonObject) { try { returnString=jsonObject.getString("ticket"); } catch (JSONException e) { returnString = null; } } return returnString; }
//获取微信JSSDK签名,用于调用微信扫一扫,返回对应数据
@Overridepublic Map getSignture(HttpServletRequest request,ModelAndView mav) {String js_accessToken = WeixinUtil.getJSSDKAccessToken(); //获取微信jssdk---access_tokenString jsapi_ticket = WeixinUtil.getJSSDKTicket(js_accessToken); //获取微信jssdk---ticketSystem.out.println("jsapi_ticket==="+jsapi_ticket);//获取完整的URL地址String fullPath=BasePath.getFullPath(request);Map data = WxJSsign.sign(jsapi_ticket, fullPath);mav.addObject("timestamp", data.get("timestamp"));mav.addObject("nonceStr", data.get("nonceStr"));mav.addObject("signature", data.get("signature"));return data;}
//JSP页面 放置3个隐藏的input 获取时间戳,签名等信息
<input type="hidden" id="timestamp" value="${timestamp}"/>
<input type="hidden" id="nonceStr" value="${nonceStr}"/>
<input type="hidden" id="signature" value="${signature}"/>
var timestamp = $("#timestamp").val();//时间戳var nonceStr = $("#nonceStr").val();//随机串var signature = $("#signature").val();//签名wx.config({debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。appId: '公众号ID', // 必填,公众号的唯一标识timestamp: timestamp, // 必填,生成签名的时间戳nonceStr: nonceStr, // 必填,生成签名的随机串signature: signature,// 必填,签名,见附录1jsApiList: ['scanQRCode'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2});//点击扫描按钮,扫描二维码并返回结果document.querySelector('#scanQRCode').onclick = function () {wx.scanQRCode({needResult: 1,desc: 'scanQRCode desc',success: function (res) {//扫码后获取结果参数:htpp://xxx.com/c/?6123,截取到url中的防伪码后,赋值给Inputvar url = res.resultStr;var tempArray = url.split('?');var tempNum = tempArray[1];$("#id_securityCode_input").val(tempNum);}});};
//演示结果“