鍍金池/ 問答/PHP  網(wǎng)絡(luò)安全  HTML/ 微信JSSDK config顯示OK,checkJsApi顯示OK,然后就沒有任

微信JSSDK config顯示OK,checkJsApi顯示OK,然后就沒有任何反應(yīng)了,求指導(dǎo)

問題描述

微信JSSDK config顯示OK,checkJsApi顯示OK,然后就沒有任何反應(yīng)了,不彈分享界面,也不報錯

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

我在網(wǎng)上看了很多案例,跟著做也沒成功,這是我的測試地址http://ecapex.top/wx/sample/p...
求大佬指點下,到底是什么原因

相關(guān)代碼

// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)
這是前端代碼

<?php
require_once "jssdk.php";
$jssdk = new JSSDK("Appid", "APPSecret");
$signPackage = $jssdk->GetSignPackage();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  
</body>
    <script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js">
    <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.js"></script>
<script>

  wx.config({
    debug: true,
    appId: '<?php echo $signPackage["appId"];?>',
    timestamp: <?php echo $signPackage["timestamp"];?>,
    nonceStr: '<?php echo $signPackage["nonceStr"];?>',
    signature: '<?php echo $signPackage["signature"];?>',
    jsApiList: [
      // 所有要調(diào)用的 API 都要加到這個列表中
        'updateAppMessageShareData',
        'updateTimelineShareData',
        'onMenuShareAppMessage',
        'onMenuShareTimeline'
    ]
  });

  //將分享的數(shù)組組成json數(shù)組
  window.share_config = {
      "share": {
          "imgUrl": "http://ecapex.top/upload/partertalents/min/20180629202242.jpg",//分享圖,默認當(dāng)相對路徑處理,所以使用絕對路徑的的話,“http://”協(xié)議前綴必須在。
          "desc" : "你真是太棒了",//摘要,如果分享到朋友圈的話,不顯示摘要。
          "title" : '你真是非常的棒',//分享卡片標(biāo)題
          "link": 'http://ecapex.top',//分享出去后的鏈接,這里可以將鏈接設(shè)置為另一個頁面。
          "success":function(){//分享成功后的回調(diào)函數(shù)
          },
          'cancel': function () {
              // 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
          }
      }
  };

  wx.ready(function () {
    // 在這里調(diào)用 API
      //判斷當(dāng)前客戶端版本是否支持指定JS接口
      wx.checkJsApi({
          jsApiList: [
              // 所有要調(diào)用的 API 都要加到這個列表中
              'updateAppMessageShareData',
              'updateTimelineShareData',
              'onMenuShareAppMessage',
              'onMenuShareTimeline'
          ], // 需要檢測的JS接口列表,所有JS接口列表見附錄2,
          success: function(res) {
              console.log(res)
              // 以鍵值對的形式返回,可用的api值true,不可用為false
              // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
          }
      });

  });




  function AppMessage() {
      wx.updateAppMessageShareData(share_config.share);
  }

  function Timeline() {
      wx.updateTimelineShareData(share_config.share);
  }

  function Messages() {
      wx.onMenuShareAppMessage(share_config.share);
  }

  function timelines() {
      wx.onMenuShareTimeline(share_config.share)
  }
</script>



<button onclick="AppMessage()">自定義分享給朋友</button><br>
<button onclick="Timeline()">自定義分享到朋友圈</button><br>
<button onclick="Messages()">分享給朋友</button>
<button onclick="timelines()">分享到朋友圈</button>
</html>

這是后端代碼

<?php
class JSSDK {
  private $appId;
  private $appSecret;

  public function __construct($appId, $appSecret) {
    $this->appId = $appId;
    $this->appSecret = $appSecret;
  }

  public function getSignPackage() {
    $jsapiTicket = $this->getJsApiTicket();

    // 注意 URL 一定要動態(tài)獲取,不能 hardcode.
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    $timestamp = time();
    $nonceStr = $this->createNonceStr();

    // 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

    $signature = sha1($string);

    $signPackage = array(
      "appId"     => $this->appId,
      "nonceStr"  => $nonceStr,//隨機字符串
      "timestamp" => $timestamp,//時間戳
      "url"       => $url,//當(dāng)前網(wǎng)頁的URL
      "signature" => $signature,//簽名
      "rawString" => $string
    );
    return $signPackage; 
  }

  //設(shè)置簽名的隨機串并按ASCII 碼從小到大排序
  private function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }

  //獲取ticket
  private function getJsApiTicket() {
    // jsapi_ticket 應(yīng)該全局存儲與更新,以下代碼以寫入到文件中做示例
    $data = json_decode($this->get_php_file("jsapi_ticket.php"));
    if ($data->expire_time < time()) {
      $accessToken = $this->getAccessToken();

      $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
      $res = json_decode($this->httpGet($url));
      $ticket = $res->ticket;
      if ($ticket) {
        $data->expire_time = time() + 7000;
        $data->jsapi_ticket = $ticket;
        $this->set_php_file("jsapi_ticket.php", json_encode($data));
      }
    } else {
      $ticket = $data->jsapi_ticket;
    }

    return $ticket;
  }

  //獲取accesstoken
  private function getAccessToken() {
    // access_token 應(yīng)該全局存儲與更新,以下代碼以寫入到文件中做示例
    $data = json_decode($this->get_php_file("access_token.php"));
    if ($data->expire_time < time()) {

      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
      $res = json_decode($this->httpGet($url));
      $access_token = $res->access_token;
      if ($access_token) {
        $data->expire_time = time() + 7000;
        $data->access_token = $access_token;
        $this->set_php_file("access_token.php", json_encode($data));
      }
    } else {
      $access_token = $data->access_token;
    }
    return $access_token;
  }

    private function httpGet($url,$type='get',$res='json',$arr='')
    {
        $ch = curl_init();
        if (class_exists('\CURLFile')) {
            curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
        } else {
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if($type=='post'){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        if($res=='json'){
            if( curl_errno($ch) ){
                //請求失敗
                return curl_error($ch);
                curl_close($ch);
            }else{
                //請求成功
                curl_close($ch);
                return $output;
            }
        }

    }

  //獲取對應(yīng)文件內(nèi)容(accesstoken、ticket)
  private function get_php_file($filename) {
    return trim(substr(file_get_contents($filename), 15));
  }
  //設(shè)置對應(yīng)文件內(nèi)容(accesstoken、ticket)
  private function set_php_file($filename, $content) {
    $fp = fopen($filename, "w");
    fwrite($fp, "<?php exit();?>" . $content);
    fclose($fp);
  }
}

你期待的結(jié)果是什么?實際看到的錯誤信息又是什么?

有么有大佬幫我看看是什么原因?指導(dǎo)一下,在線等急

回答
編輯回答
萌吟

本來就不會彈框呀,在微信webview打開網(wǎng)頁,右上角有三個點,點開之后有分享朋友圈、分享給朋友等等選項,如果你不調(diào)用的話,分享出來的是默認的,如果你成功調(diào)用,分享出來的就是你config里的信息。(也就是你成功調(diào)用并不會彈出框 而應(yīng)該點開右上角三個小點,進去分享一下,看看分享結(jié)果,也就是下面的信息)

"share": {
          "imgUrl": "http://ecapex.top/upload/partertalents/min/20180629202242.jpg",//分享圖,默認當(dāng)相對路徑處理,所以使用絕對路徑的的話,“http://”協(xié)議前綴必須在。
          "desc" : "你真是太棒了",//摘要,如果分享到朋友圈的話,不顯示摘要。
          "title" : '你真是非常的棒',//分享卡片標(biāo)題
          "link": 'http://ecapex.top',//分享出去后的鏈接,這里可以將鏈接設(shè)置為另一個頁面。
          "success":function(){//分享成功后的回調(diào)函數(shù)
          },
          'cancel': function () {
              // 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
          }
      }
2018年8月17日 23:09