久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

詳解vue開(kāi)發(fā)中調(diào)用微信jssdk的問(wèn)題

起因

成都創(chuàng)新互聯(lián)專注于企業(yè)營(yíng)銷型網(wǎng)站、網(wǎng)站重做改版、薩迦網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為薩迦等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

微信分享網(wǎng)址時(shí)無(wú)法分享圖片,這個(gè)問(wèn)題需要用jssdk去解決。其實(shí)開(kāi)始的時(shí)候時(shí)可以看到圖片的,但后來(lái)微信禁止了。所以只能使用jssdk去解決。
普通網(wǎng)頁(yè)開(kāi)發(fā)很簡(jiǎn)單,但是使用vue或其他前端框架開(kāi)發(fā)spa單頁(yè)面webapp的時(shí)候就會(huì)有問(wèn)題了。只要url發(fā)生變化就會(huì)報(bào)簽名錯(cuò)誤。其實(shí)微信官方上已經(jīng)寫了說(shuō)明。

所有需要使用JS-SDK的頁(yè)面必須先注入配置信息,否則將無(wú)法調(diào)用(同一個(gè)url僅需調(diào)用一次,對(duì)于變化url的SPA的web app可在每次url變化時(shí)進(jìn)行調(diào)用,目前Android微信客戶端不支持pushState的H5新特性,所以使用pushState來(lái)實(shí)現(xiàn)web app的頁(yè)面會(huì)導(dǎo)致簽名失敗,此問(wèn)題會(huì)在Android6.2中修復(fù))。
但這些說(shuō)明然并卵(然而并沒(méi)有什么卵用)。

問(wèn)題根源

1 同一個(gè)url僅需調(diào)用一次,對(duì)于變化url的SPA的web app可在每次url變化時(shí)進(jìn)行調(diào)用
如果你的鏈接時(shí)采用hash方式,錨點(diǎn)變了也要重新調(diào)用,因?yàn)閡rl發(fā)生了變化,這一點(diǎn)可以放在router的監(jiān)聽(tīng)事件中比如watch一下$route,或者使用2.2 中引入的 beforeRouteUpdate 守衛(wèi)。

2 生成簽名時(shí)url中不能包含錨點(diǎn)
微信jssdk的簽名是需要服務(wù)端來(lái)生成的,所以我們需要將當(dāng)前頁(yè)面的網(wǎng)址傳遞給服務(wù)端,由服務(wù)端生成wx.config初始化所需要的參數(shù)。
但是url傳遞的時(shí)候需要注意,一定一定一定不能帶有錨點(diǎn)鏈接??梢允褂胠ocation.href.split(‘#')[0]獲取url中錨點(diǎn)之前的部分。
比如你的網(wǎng)址是http://domain.com/index.html#/food/1
你只需要把http://domain.com/index.html傳遞到服務(wù)端,讓服務(wù)端生成簽名就可以了,你在調(diào)用jssdk的時(shí)候可以把url后面添加錨點(diǎn)鏈接。

實(shí)例

安裝jssdk

npm install weixin-js-sdk --save

 前段代碼

export default {
  mounted() {
   this.$nextTick(function () {
    this.getConfig()
   })
  },
  data () {
   return {
    detail: [],
   }
  },
  methods: {   
   // 微信分參數(shù)
   getConfig() { 
    let url = location.href.split('#')[0] //獲取錨點(diǎn)之前的鏈接
    this.$http.get('/index.php',{
     params: {
      url: url
     }
    }).then(response => {
      let res = response.data;
      this.wxInit(res);
     })
   },
   // 微信分享
   wxInit(res) {
    let url = location.href.split('#')[0] //獲取錨點(diǎn)之前的鏈接 
    let links = url+'#/Food/' + this.$route.params.id;
    let title = this.detail.name + '-嘌呤查';
    let desc = '了解更多知識(shí),請(qǐng)關(guān)注“嘌呤查”公眾號(hào)';
    let imgUrl = this.thumb;
    wx.config({
     debug: false,
     appId: res.appId,
     timestamp: res.timestamp,
     nonceStr: res.nonceStr,
     signature: res.signature,
     jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone']
    });
    wx.ready(function() {
     wx.onMenuShareTimeline({
      title: title, // 分享標(biāo)題
      desc: desc, // 分享描述
      link: links, // 分享鏈接
      imgUrl: imgUrl, // 分享圖標(biāo)
      success: function() {
//        alert("分享到朋友圈成功")
       //Toast({
        //message: "成功分享到朋友圈"
       //});
      },
      cancel: function() {
//        alert("分享失敗,您取消了分享!")
       //Toast({
        //message: "分享失敗,您取消了分享!"
       //});
      }
     });
     //微信分享菜單測(cè)試
     wx.onMenuShareAppMessage({
      title: title, // 分享標(biāo)題
      desc: desc, // 分享描述
      link: links, // 分享鏈接
      imgUrl: imgUrl, // 分享圖標(biāo)
      success: function() {
       // alert("成功分享給朋友")
//       Toast({
//        message: "成功分享給朋友"
//       });
      },
      cancel: function() {
       // alert("分享失敗,您取消了分享!")
//       Toast({
//        message: "分享失敗,您取消了分享!"
//       });
      }
     });

     wx.onMenuShareQQ({
      title: title, // 分享標(biāo)題
      desc: desc, // 分享描述
      link: links, // 分享鏈接
      imgUrl: imgUrl, // 分享圖標(biāo)
      success: function() {
       // alert("成功分享給QQ")
//       Toast({
//        message: "成功分享到QQ"
//       });
      },
      cancel: function() {
       // alert("分享失敗,您取消了分享!")
//       Toast({
//        message: "分享失敗,您取消了分享!"
//       });
      }
     });
     wx.onMenuShareWeibo({
      title: title, // 分享標(biāo)題
      desc: desc, // 分享描述
      link: links, // 分享鏈接
      imgUrl: imgUrl, // 分享圖標(biāo)
      success: function() {
       // alert("成功分享給朋友")
//       Toast({
//        message: "成功分享到騰訊微博"
//       });
      },
      cancel: function() {
       // alert("分享失敗,您取消了分享!")
//       Toast({
//        message: "分享失敗,您取消了分享!"
//       });
      }
     });
     wx.onMenuShareQZone({
      title: title, // 分享標(biāo)題
      desc: desc, // 分享描述
      link: links, // 分享鏈接
      imgUrl: imgUrl, // 分享圖標(biāo)
      success: function() {
       // alert("成功分享給朋友")
//       Toast({
//        message: "成功分享到QQ空間"
//       });
      },
      cancel: function() {
       // alert("分享失敗,您取消了分享!")
//       Toast({
//        message: "分享失敗,您取消了分享!"
//       });
      }
     });

    });
    wx.error(function(err) {
     alert(JSON.stringify(err))
    });
   }
  }
 }

index.php頁(yè)面代碼

// 官方實(shí)例,生成wx.config需要的配置信息
class JSSDK {
 private $appId;
 private $appSecret;

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

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

  // 注意 URL 一定要?jiǎng)討B(tài)獲取,不能 hardcode.
  $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  // 注意這里是重點(diǎn)
  $url = !empty($_GET['url']) ? $_GET['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,
   "signature" => $signature,
   "rawString" => $string
  );
  return $signPackage; 
 }

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

 private function getJsApiTicket() {
  // jsapi_ticket 應(yīng)該全局存儲(chǔ)與更新,以下代碼以寫入到文件中做示例
  $data = json_decode($this->get_php_file("jsapi_ticket.php"));
  if ($data->expire_time < time()) {
   $accessToken = $this->getAccessToken();
   // 如果是企業(yè)號(hào)用以下 URL 獲取 ticket
   // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
   $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;
 }

 private function getAccessToken() {
  // access_token 應(yīng)該全局存儲(chǔ)與更新,以下代碼以寫入到文件中做示例
  $data = json_decode($this->get_php_file("access_token.php"));
  if ($data->expire_time < time()) {
   // 如果是企業(yè)號(hào)用以下URL獲取access_token
   // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
   $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) {
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  // 為保證第三方服務(wù)器與微信服務(wù)器之間數(shù)據(jù)傳輸?shù)陌踩?,所有微信接口采用https方式調(diào)用,必須使用下面2行代碼打開(kāi)ssl安全校驗(yàn)。
  // 如果在部署過(guò)程中代碼在此處驗(yàn)證失敗,請(qǐng)到 http://curl.haxx.se/ca/cacert.pem 下載新的證書判別文件。
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
  curl_setopt($curl, CURLOPT_URL, $url);

  $res = curl_exec($curl);
  curl_close($curl);

  return $res;
 }

 private function get_php_file($filename) {
  return trim(substr(file_get_contents($filename), 15));
 }
 private function set_php_file($filename, $content) {
  $fp = fopen($filename, "w");
  fwrite($fp, "<?php exit();?>" . $content);
  fclose($fp);
 }
}



// 邏輯代碼
$appId = '你的appid';
$appSecret = '你的appSecret';
$jssdk = new JSSDK($appId, $appSecret);
$signPackage = $jssdk->GetSignPackage();
echo json_encode($signPackage);

看到上面的類中使用$_GET[‘url']接收前段傳過(guò)來(lái)的數(shù)據(jù)

以上所述是小編給大家介紹的vue開(kāi)發(fā)中調(diào)用微信jssdk的問(wèn)題詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

分享文章:詳解vue開(kāi)發(fā)中調(diào)用微信jssdk的問(wèn)題
轉(zhuǎn)載源于:http://sd-ha.com/article22/gcjjcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、企業(yè)建站全網(wǎng)營(yíng)銷推廣、網(wǎng)站收錄、域名注冊(cè)、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)