微信二次分享報錯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方式本人未做測試)

 

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);
 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM