发布于 4年前

微信二次分享报错:"invalid signature"

基于微信公众号开发的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);
    } 
©2020 edoou.com   京ICP备16001874号-3