实现微信公众号自定义分享功能,分享给朋友,分享到朋友圈,点击链接,获取点击分享者的openid。
生活随笔
收集整理的這篇文章主要介紹了
实现微信公众号自定义分享功能,分享给朋友,分享到朋友圈,点击链接,获取点击分享者的openid。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
微信公眾號自定義分享功能 與 獲取點擊分享者的openid,通過cookic校驗,重新請求獲取
引用的微信 js:
前端jsp 實現:
<form id="bmform" onSubmit="javascript:savebaoming(this,event)" action="${ctx }/bm/savebaoming" method="post" enctype="multipart/form-data"><input type="hidden" name="weixinUsername" value="${openid}" id="weixinUsername"/><input type="hidden" name="weixinNickname" value="${nickname}" id="weixinNickname"/> <input type="hidden" name="weixinHeadImgUrl" value="${headImgUrl}" id="weixinHeadImgUrl"/> </form>前端js 實現代碼:
/* 微信獲取登錄者詳情信息 */ $(document).ready(function(){//這里的cookic 解決微信分享成功后,獲取點擊分享者的openid,讓其重新請求獲取就可以。var str=$.cookie('cookic');var openid=$("#weixinUsername").val();if(str=="" || str==undefined || openid==null || openid==''){//獲取網頁地址var url=window.location.href;//對網頁地址進行處理var repUrl = url.replace("&eId=", "_");var index=url.indexOf("bm");// 發送請求,后臺處理,經過微信登錄,授權,獲取到登錄者的詳情信息。再重定向到這個頁面。var newurl=url.substring(0,index)+"weixin/palogin?redirect_uri="+repUrl+"&company="+'${activity.company}';window.location.href=newurl;//重新賦值,不是undefind就好,不然頁面會一直刷新$.cookie('cookic',"aaa")}========================================================================= /* 微信分享功能 *///獲取分享的地址,生成參數不能變,不能隨便定義分享鏈接, ( 需要去微信公眾號配置 JS安全域名。)var strurl=window.location.href.split('#')[0];//頁面初始化發送請求,根據路徑strurl 與根據你所需要的參數,獲取wx.config的必要參數。注意請求回來的參數大小寫$.get("${ctx}/pub/config",{"url":strurl,company:"${activity.company}"},function(data,status){if(status == "success"){wx.config({debug: false, // 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時才會打印。appId: data.appid, // 必填,公眾號的唯一標識timestamp: data.timestamp, // 必填,生成簽名的時間戳nonceStr: data.noncestr, // 必填,生成簽名的隨機串signature: data.signature,// 必填,簽名jsApiList: [ //把需要的接口加入至列表 全部用的舊的接口方法,新的可能不好使。"onMenuShareTimeline", //分享到朋友圈"onMenuShareAppMessage", //分享到朋友"onMenuShareQQ", //分享到QQ] // 必填,需要使用的JS接口列表,所有JS接口列表}); wx.ready(function(){wx.onMenuShareTimeline({title: "活動報名,分享給朋友圈", // 分享標題desc: "活動報名", // 分享描述link: strurl, // 分享鏈接,該鏈接域名或路徑必須與當前頁面對應的公眾號JS安全域名一致imgUrl:"", // 分享圖標stype: 'link', // 分享類型,music、video或link,不填默認為linkdataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認為空success: function () {},cancel: function () {// 用戶取消分享后執行的回調函數//alert("fail");},fail: function () {// 用戶取消分享后執行的回調函數//alert("fail");},});wx.onMenuShareQQ({title: "活動報名,分享給好友", // 分享標題desc: "分享描述", // 分享描述link: strurl, // 分享鏈接,imgUrl:"", // 分享圖標stype: 'link', // 分享類型,music、video或link,不填默認為linkdataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認為空success: function () {// 用戶確認分享后執行的回調函數//alert("OK");},cancel: function () {// 用戶取消分享后執行的回調函數//alert("fail");} ,fail: function () {// 用戶取消分享后執行的回調函數// alert("fail");},});wx.onMenuShareAppMessage({title: "活動報名,分享給朋友哦", // 分享標題desc: "分享描述", // 分享描述link: strurl, // 分享鏈接,imgUrl:"", // 分享圖標stype: 'link', // 分享類型,music、video或link,不填默認為linkdataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認為空success: function () {// 用戶確認分享后執行的回調函數// alert("OK");},cancel: function () {// 用戶取消分享后執行的回調函數//alert("fail");},fail: function () {// 用戶取消分享后執行的回調函數//alert("fail");},});});wx.error(function(res){// config信息驗證失敗會執行error函數,如簽名過期導致驗證失敗,具體錯誤信息可以打開config的debug模式查看,//也可以在返回的res參數中查看,對于SPA可以在這里更新簽名。// alert("失敗!");});}},"json");})引用的jar包:
<!-- 微信開發工具包 --><dependency><groupId>me.chanjar</groupId><artifactId>weixin-java-mp</artifactId><version>1.3.3</version></dependency><dependency><groupId>me.chanjar</groupId><artifactId>weixin-java-common</artifactId><version>1.3.3</version></dependency>分享的后端代碼:
/*** * 功能:微信分享接口,獲取wx.config 所需的必要參數* 作者:wj* 日期:2019年5月23日上午9:22:13*/@RequestMapping(value = "/config", method = RequestMethod.GET)@ResponseBodypublic WxJsapiSignature wxJsSdkConfig(HttpServletRequest request,String url,String company) {WxMpService wxMpService = new WxMpServiceImpl();WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();CustomConfig customConfig = SysConfigUtil.getSysConfigBySystem("weChatPaymentPlugin", company);//微信公眾號的appidString AppId = customConfigService.findByKey("wx_app_id_"+company).getParaKeyValue();// 微信公眾號的app corpSecretString Secret = customConfigService.findByKey("wx_app_secrate_"+company).getParaKeyValue()wxMpService.setWxMpConfigStorage(config);try { // 直接調用wxMpServer 接口 WxJsapiSignature wxJsapiSignature = wxMpService.createJsapiSignature(url);return wxJsapiSignature;} catch (WxErrorException e) {e.getMessage();System.out.println(e.getMessage());return null;}}微信號公共平臺:
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432成功頁面展示;
總結
以上是生活随笔為你收集整理的实现微信公众号自定义分享功能,分享给朋友,分享到朋友圈,点击链接,获取点击分享者的openid。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哈尔滨工业大学软件学院诚聘英才
- 下一篇: ad中按钮开关的符号_火速收藏!电工最常