這篇文章主要介紹微信js-sdk+JAVA如何實現(xiàn)“分享到朋友圈”和“發(fā)送給朋友”功能,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,先為蒲城等服務(wù)建站,蒲城等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為蒲城企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
主要為以下實現(xiàn)步驟:
1.綁定域名
先登錄微信公眾平臺進(jìn)入“公眾號設(shè)置”的“功能設(shè)置”里填寫“JS接口安全域名”。(特別提示不需要加上http或者h(yuǎn)ttps,吃過虧)
2.頁面引入js文件
<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.通過config接口注入權(quán)限驗證配置
wx.config({ debug: true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。 appId: '', // 必填,公眾號的唯一標(biāo)識 timestamp: , // 必填,生成簽名的時間戳 nonceStr: '', // 必填,生成簽名的隨機串 signature: '',// 必填,簽名,見附錄1 jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2 });
4.通過ready接口處理成功驗證
wx.ready(function(){ //詳細(xì)代碼 });
5.通過error接口處理失敗驗證
wx.error(function(res){});
詳細(xì)頁面代碼
<script> //微信分享朋友圈 $(function(){ /***用戶點擊分享到微信圈后加載接口接口*******/ 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();/***隱藏分享菜單****/ wx.checkJsApi({ jsApiList: [ 'getLocation', 'onMenuShareTimeline', 'onMenuShareAppMessage' ], success: function (res) { //alert(res.errMsg); } }); wx.onMenuShareAppMessage({ title: '刮刮樂', desc: '刮刮樂開始啦', link: '<%=basePath%>/lottery/lottery.action?lottery.id=${lottery.id}', imgUrl: '<%=basePath%>/resources/qjc/img/start.png', trigger: function (res) { //alert('用戶點擊發(fā)送給朋友'); }, success: function (res) { alert('您已獲得抽獎機會,趕緊去贏大獎吧~~'); //分享之后增加游戲次數(shù) $.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('已取消'); }, fail: function (res) { alert(res.errMsg); } }); // 2.2 監(jiān)聽“分享到朋友圈”按鈕點擊、自定義分享內(nèi)容及分享結(jié)果接口 wx.onMenuShareTimeline({ title: '刮刮樂', desc: '刮刮樂開始啦', link: '<%=basePath%>/lottery/lottery.action?lottery.id=${lottery.id}', imgUrl: '<%=basePath%>/resources/qjc/img/start.png', trigger: function (res) { //alert('用戶點擊分享到朋友圈'); }, success: function (res) { alert('您已獲得抽獎機會,趕緊去贏大獎吧~~'); //分享之后增加游戲次數(shù) $.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('已取消'); }, fail: function (res) { alert(res.errMsg); } }); wx.error(function (res) { alert(res.errMsg); }); }); }, error: function() { alert('ajax request failed!!!!'); return; } }); }); </script>
java后臺action代碼:
//微信分享 public void shareToFriend(){ HttpServletRequest request = ServletActionContext.getRequest(); String timeStamp = Sha1Util.getTimeStamp();//時間戳 String nonceStr = WxConfig.getUUID();//隨機字符串,不長于32位 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代碼
">//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; } //生成隨機字符串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("微信JS-SDK權(quán)限驗證的簽名串:"+signValue); //這個簽名.主要是給加載微信js使用.別和上面的搞混了. String signature = Sha1Util.getSha1((signValue)); logger.info("微信JS-SDK權(quán)限驗證的簽名:"+signature); return signature; }
以上是“微信js-sdk+JAVA如何實現(xiàn)“分享到朋友圈”和“發(fā)送給朋友”功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文標(biāo)題:微信js-sdk+JAVA如何實現(xiàn)“分享到朋友圈”和“發(fā)送給朋友”功能
網(wǎng)頁地址:http://jinyejixie.com/article48/igojhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、、網(wǎng)站導(dǎo)航、服務(wù)器托管、ChatGPT、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)