微信二次分享报错,invalid signature

8次阅读

共计 1722 个字符,预计需要花费 5 分钟才能阅读完成。

基于微信公众号开发的 h5 页面(使用 jssdk 接口),由用户 A 分享给用户 B,用户 B 再次分享这个页面时,不能成功分享。问题出在用户 B 收到的分享链接与用户 A 打开的链接不同 A 用户的链接为
http://test.com/test.html
B 用户收到的连接
http://test.com/test.html&from=singlemessage
from=singlemessage 是微信客户端为了区分分享来源再链接后自动添加的标记,再次分享时,需要在 js 代码中对自动获取的连接进行 encodeURIComponent 处理,后台再对收到的 url 进行 urldecode 处理。
js 与 php 示例代码如下:注意 ajax,用的 post,用 get 据说不用转义(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, // 调试模式
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 () {
},
});
});

}
});
}

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 . ‘&timestamp=’ . $timestamp . ‘&url=’ . $url);
$shareConfig[‘appId’] = ”;// 此处为 appId
$shareConfig[‘timestamp’] = $timestamp;
$shareConfig[‘noncestr’] = $noncestr;
$shareConfig[‘signature’] = $signature;
$shareConfig[‘url’] = $url;
echo json_encode($shareConfig);
}

正文完
 0