本文以微信分享到朋友圈,分享給微信好友爲例爲參考,進行調用測試,想添加其餘的功能,自行查看開發人員文檔便可javascript
工欲善其事,必先利其器,好好利用下邊的幫助工具,都是騰訊給開發人員的工具php
1.微信開發者說明文檔:點擊查看html
2.微信WEB開發者工具:java
Windows 64位版本:下載地址web
Windows 32位版本:下載地址ajax
Mac版本:下載地址json
3.微信JSSDK分享sample:點擊下載api
4.在線DEMO:http://203.195.235.76/jssdk/微信
微信版本6.0之後,原有的WeixinJSBridge.on('menu:share:timeline', function (argv) {}再也不可在以使用,那如何在使用微信的其餘功能呢?官方給出了JSSDK的使用,幫助咱們解決~微信開發
前言:
雖然微信提供了JSSDK,可是這不意味着你能夠用自定義的按鈕來直接打開微信的分享界面,這套JSSDK只是把微信分享接口的內容定義好了,實際仍是須要用戶點擊右上角的菜單按鈕進行主動的分享,用戶點開分享界面以後,出現的內容就會是你定義的分享標題、圖片和連接。
- 2.實測圖解:
-
- 2.1 測試頁面:
2.2分享到朋友圈:
2.3分享到朋友圈:
(這個是觸發success的效果,配圖描述寫錯了,抱歉)
(這個是觸發cancel的效果)
2.3調試界面:
後臺代碼(jssdk.PHP見後邊):
- require_once "jssdk.php";
- $jssdk = new JSSDK($appid,$appsecret);
- $signPackage = $jssdk->GetSignPackage();
- $this->assign("signPackage",$signPackage);
JS配置代碼
- <script type="text/javascript" src='http://res.wx.qq.com/open/js/jweixin-1.0.0.js'></script>
- <script type="text/javascript">
- wx.config({
- debug: false,
- appId: '{$signPackage["appId"]}',
- timestamp: '{$signPackage["timestamp"]}',
- nonceStr: '{$signPackage["nonceStr"]}',
- signature: '{$signPackage["signature"]}',
- jsApiList: [
-
- 'checkJsApi',
- 'onMenuShareTimeline',
- 'onMenuShareAppMessage',
- 'onMenuShareQQ',
- 'onMenuShareWeibo',
- 'onMenuShareQZone'
- ]
- });
- </script>
- <script type="text/javascript" src='http://203.195.235.76/jssdk/js/zepto.min.js'></script>
JS分享代碼:
- <script type="text/javascript">
-
- wx.ready(function () {
-
- wx.onMenuShareTimeline({
- title: '1111111',
- link:window.location.href,
- imgUrl: "{pigcms:$res['pic']}",
- success: function () {
-
- alert('success');
- },
- cancel: function () {
- alert('cancel');
- }
- });
-
-
- wx.onMenuShareAppMessage({
- title: '22222',
- desc: '22222',
- link:window.location.href,
- imgUrl: "{pigcms:$res['pic']}",
- trigger: function (res) {
-
- },
- success: function (res) {
-
- alert('已分享');
- },
- cancel: function (res) {
- alert('已取消');
- },
- fail: function (res) {
- alert(JSON.stringify(res));
- }
- });
- });
-
- </script>
JSSDK類
jssdk.php
- <?php
- class JSSDK {
- private $appId;
- private $appSecret;
-
- public function __construct($appId, $appSecret) {
- $this->appId = $appId;
- $this->appSecret = $appSecret;
- }
-
- public function getSignPackage() {
- $jsapiTicket = $this->getJsApiTicket();
-
-
- $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();
-
-
- $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$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() {
-
- $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;
-
- return $ticket;
- }
-
- private function getAccessToken() {
-
-
-
- $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;
-
- return $access_token;
- }
-
- private function httpGet($url) {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 500);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_URL, $url);
-
- $res = curl_exec($curl);
- curl_close($curl);
-
- return $res;
- }
- }