Mainly the following implementation steps:
1. Bind the domain name
First log in to the WeChat public platform and enter the "Function Settings" of "Public Account Settings" to fill in the "JS interface security domain name". (Special reminder does not need to add http or https, you have suffered a loss)
2. Page imports js file
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
3. Verify configuration through the config interface
wx.config({debug: true, // Turn on debug mode, the return values of all APIs called will be alerted on the client. To view the passed parameters, you can open them on the PC side, the parameter information will be printed through the log and will only be printed on the PC side. appId: '', // Required, the unique identifier of the official account timestamp: , // Required, the timestamp generated by the signature nonceStr: '', // Required, the random string generated by the signature signature: '', // Required, the signature is shown in Appendix 1 jsApiList: [] // Required, the list of JS interfaces that need to be used, and the list of all JS interfaces is shown in Appendix 2});4. Successfully verified through the ready interface processing
wx.ready(function(){ //Detailed code});5. Failed verification through the error interface
wx.error(function(res){});Detailed page code
<script> //WeChat share Moments $(function(){ /***User clicks to share to WeChat circle and loads the interface interface**********/ var url=window.location.href.split('#')[0]; url = url.replace(/&/g, '%26'); console.log("url:"+url); $.ajax({ url: "<%=basePath%>/lottery/shareToFriend.action?url="+url, type: "POST", async:true, cache: false, dataType: "json", success: function(data){ wx.config({ debug: false, appId: 'wx2948dfef9ef421ee', timestamp:data.timeStamp, nonceStr:data.nonceStr, signature:data.signature, jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'hideOptionMenu', 'onMenuShareAppMessage' ] }); wx.ready(function(){ //wx.hideOptionMenu();/***HideShared Sharing Menu****/ wx.checkJsApi({ jsApiList: [ 'getLocation', 'onMenuShareTimeline', 'onMenuShareAppMessage' ], success: function (res) { //alert(res.errMsg); } }); wx.onMenuShareAppMessage({ title: 'scratch', desc: 'scratch', link: '<%=basePath%>/lottery/lottery.action?lottery.id=${lottery.id}', imgUrl: '<%=basePath%>/resources/qjc/img/start.png', trigger: function (res) { //alert('User clicks to send to friends'); }, success: function (res) { alert('You have won the chance to draw, hurry up and win the jackpot~~'); //Add to increase the number of games after sharing$.ajax({ url: "<%=basePath%>/lottery/rewardPlayCount.action?openId=${openId}&lotteryId=${lottery.id}&shareType=friend", type: "POST", async:true, cache: false, dataType: "json", success: function(data){ } }); }, cancel: function (res) { //alert('Canceled'); }, fail: function (res) { alert(res.errMsg); } }); // 2.2 Monitor "Share to Moments" button click, customize sharing content and sharing results interface wx.onMenuShareTimeline({ title: 'Scratch', desc: 'Scratch', link: '<%=basePath%>/lottery/lottery.action?lottery.id=${lottery.id}', imgUrl: '<%=basePath%>/resources/qjc/img/start.png', trigger: function (res) { //alert('User clicking to share to Moments'); }, success: function (res) { alert('You have won the lottery opportunity, hurry up and win the jackpot~~'); //Add to increase the number of games after sharing $.ajax({ url: "<%=basePath%>/lottery/rewardPlayCount.action?openId=${openId}&lotteryId=${lottery.id}&shareType=friendCircle", type: "POST", async:true, cache: false, dataType: "json", success: function(data){ } }); }, cancel: function (res) { //alert('Canceled'); }, fail: function (res) { alert(res.errMsg); } }); wx.error(function (res) { alert(res.errMsg); }); }); }, error: function() { alert('ajax request failed!!!'); return; } }); }); </script>Java background action code:
//WeChat share public void shareToFriend(){ HttpServletRequest request = ServletActionContext.getRequest(); String timeStamp = Sha1Util.getTimeStamp();//Timestamp String nonceStr = WxConfig.getUUID();//Random string, not longer than 32 bits String url=request.getParameter("url"); String signature = WxConfig.getSignature("APPId", "APP_secret", url, timeStamp, nonceStr); request.setAttribute("timeStamp", timeStamp); request.setAttribute("nonceStr", nonceStr); request.setAttribute("url", url); request.setAttribute("signature", signature); WXjssdk result = new WXjssdk(timeStamp,nonceStr,signature,url); CommonUtil.returnMsg(ServletActionContext.getResponse(), new Gson().toJson(result)); } WxConfig.java code
">//jsapi_ticket public final static String WEIXIN_JSAPI_TICKET_URL ="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi"; //access_token public static String getAccessToken(String appId,String appSecret){ String access_token; access_token = mapToken.get("accessToken"); if(access_token==null){ String url = HttpUtil.WEIXIN_HOST_API + "/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret; String menuJsonStr = HttpUtil.get(url); final Type type = new TypeToken<Map<String, Object>>() {}.getType(); final Map<Object, Object> accessTokenInfo = new Gson().fromJson(menuJsonStr, type); try{ access_token = accessTokenInfo.get("access_token").toString(); Object expires_in = accessTokenInfo.get("expires_in"); mapToken.put("accessToken", access_token); logger.info("access_token:"+access_token+"; expires_in:"+expires_in); }catch (JSONException e) { access_token = null; e.printStackTrace(); logger.error("errcode:{}:"+accessTokenInfo.get("errcode")+"errmsg:{}:"+accessTokenInfo.get("errmsg")); } } return access_token; } //jsapi_ticket public static String getJsapiTicket(String accessToken){ String ticket; ticket = mapTicket.get("ticket"); if(ticket==null){ String url = HttpUtil.WEIXIN_HOST_API + "/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi"; String menuJsonStr = HttpUtil.get(url); final Type type = new TypeToken<Map<String, Object>>() {}.getType(); final Map<Object, Object> ticketInfo = new Gson().fromJson(menuJsonStr, type); try{ ticket = ticketInfo.get("ticket").toString(); String expires_in = ticketInfo.get("expires_in").toString(); mapTicket.put("ticket", ticket); logger.info("jsapi_ticket:"+ticket+"; expires_in:"+expires_in); }catch (JSONException e) { ticket = null; e.printStackTrace(); logger.error("ticket errcode:{}:"+ticketInfo.get("errcode")+"errmsg:{}:"+ticketInfo.get("errmsg")); } } return ticket; } //Generate random string UUID public static String getUUID(){ String uuid = UUID.randomUUID().toString().trim().replaceAll("-", ""); return uuid; } //JS-SDK Signature public static String getSignature(String appId,String appSecret,String url,String timeStamp,String nonceStr){ String accessToken = getAccessToken(appId,appSecret); String jsapi_ticket = getJsapiTicket(accessToken); logger.info("accessToken==="+accessToken); String signValue = "jsapi_ticket="+jsapi_ticket+"&noncestr="+nonceStr+"×tamp="+timeStamp+"&url="+url; logger.info("WeChat JS-SDK permission verification signature string: "+signValue); //This signature. It is mainly used for loading WeChat js. Don't be confused with the above. String signature = Sha1Util.getSha1((signValue)); logger.info("WeChat JS-SDK permission verification signature: "+signature); return signature; }In addition, Sha1Util.java and MD5Util.java used in the project can be downloaded directly on the platform.