基於微信公眾號開發的h5頁面(使用jssdk接口),由用戶A分享給用戶B,用戶B再次分享這個頁面時,不能成功分享。問題出在用戶B收到的分享鏈接與用戶A打開的鏈接不同
A用戶的鏈接為
B用戶收到的連接
http://test.com/test.html&from=singlemessage
from=singlemessage是微信客戶端為了區分分享來源再鏈接后自動添加的標記,再次分享時,需要在js代碼中對自動獲取的連接進行encodeURIComponent處理,后台再對收到的url進行urldecode處理。
js與php示例代碼如下:
注意ajax,用的post,用get據說不用轉義(get方式本人未做測試)
function share(){ var nowurl = window.location.href; var nowurlo = nowurl.split('&')[0]; $.ajax({ type : "post", url : "***********************", //后端接口 dataType : "json", data : { 'url': encodeURIComponent(nowurl) }, // 注意此處對nowurl進行encode; success : function (data) { wx.config({ debug : false, //調試模式 appId : data.appId, //公眾號appid timestamp : data.timestamp, //時間戳 nonceStr : data.noncestr, //生成簽名的隨機串 signature : data.signature, //簽名 jsApiList : [ 'updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareAppMessage', 'onMenuShareTimeline', 'chooseWXPay', 'showOptionMenu', "hideMenuItems", "showMenuItems", "onMenuShareTimeline", 'onMenuShareAppMessage', ] // 必填,需要使用的JS接口列表 }); wx.ready(function () { //需在用戶可能點擊分享按鈕前就先調用 wx.updateAppMessageShareData({ title : '', // 分享標題 desc : '', // 分享描述 link : nowurlo, // 自動獲取(上面js代碼中) imgUrl : '', // 分享圖標 success : function () { } }); wx.updateTimelineShareData({ title : '', // 分享標題 link : nowurlo, 自動獲取(上面js代碼中) imgUrl : '', // 分享圖標 success : function () { }, }); }); } }); }
public function generateSignature(){ $timestamp = time(); $jsapiTicket = ;//此處獲取jsapi_ticket $noncestr = md5(uniqid(microtime(true),true));//我用的noncestr $url = urldecode(I('post.url')); $signature = sha1('jsapi_ticket=' . $jsapiTicket . '&noncestr=' . $noncestr . '×tamp=' . $timestamp . '&url=' . $url); $shareConfig['appId'] = '';//此處為appId $shareConfig['timestamp'] = $timestamp; $shareConfig['noncestr'] = $noncestr; $shareConfig['signature'] = $signature; $shareConfig['url'] = $url; echo json_encode($shareConfig); }