基于微信公眾號開發(fā)的h5頁面(使用jssdk接口)腊徙,由用戶A分享給用戶B涮母,用戶B再次分享這個頁面時,不能成功分享。問題出在用戶B收到的分享鏈接與用戶A打開的鏈接不同
A用戶的鏈接為
http://test.com/test.html
B用戶收到的連接
http://test.com/test.html&from=singlemessage
"from=singlemessage"是微信客戶端為了區(qū)分分享來源再鏈接后自動添加的標記妹懒,
再次分享時,需要在js代碼中對自動獲取的連接進行encodeURIComponent處理双吆,后臺再對收到的url進行urldecode處理眨唬。
js與php示例代碼如下:
注意ajax,用的post好乐,get據(jù)說不用轉(zhuǎn)義(get方式本人未做測試)
js代碼
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, //調(diào)試模式
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 () { //需在用戶可能點擊分享按鈕前就先調(diào)用
wx.updateAppMessageShareData({
title : '', // 分享標題
desc : '', // 分享描述
link : nowurlo, // 自動獲任低颉(上面js代碼中)
imgUrl : '', // 分享圖標
success : function () {
}
});
wx.updateTimelineShareData({
title : '', // 分享標題
link : nowurlo, 自動獲攘胙(上面js代碼中)
imgUrl : '', // 分享圖標
success : function () {
},
});
});
}
});
}
PHP代碼
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);
}