PHP部分
擴充套件類程式碼部分:
<?php namespace Think; class JsSdk { private $appId; private $appSecret; public $debug = false; public function __construct($appId, $appSecret) { $this->appId = $appId; $this->appSecret = $appSecret; } public function getSignPackage() { $jsapiTicket = $this->getJsApiTicket(); $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = $this->createNonceStr(); // 這裡引數的順序要按照 key 值 ASCII 碼升序排序 $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() { //debug模式 if ($this->debug) { // jsapi_ticket 應該全域性儲存與更新,以下程式碼以寫入到檔案中做示例 $data = json_decode(file_get_contents("jsapi_ticket.json")); } else { //從cache中讀取,基於ThinkPHP的快取機制 $data = (object)(S('jsapi_ticket_json')); } if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); $url = "http://api.weixin.qq.com/cgi-bin/ticket/getticket?type=1&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7200; $data->jsapi_ticket = $ticket; //debug模式 if ($this->debug) { $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } else { //將物件以陣列的形式進行快取 S('jsapi_ticket_json', (array)$data); } } } else { $ticket = $data->jsapi_ticket; } return $ticket; } private function getAccessToken() { //debug模式 if ($this->debug) { // access_token 應該全域性儲存與更新,以下程式碼以寫入到檔案中做示例 $data = json_decode(file_get_contents("access_token.json")); dump($data); die(); } else { //從快取中讀取陣列並轉成物件 $data = (Object)(S('access_token.json')); } 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; //debug模式 if ($this->debug) { $fp = fopen("access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } else { //快取陣列 S('access_token.json', (array)$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); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); //錯誤檢測 $error = curl_error($curl); curl_close($curl); //發生錯誤,丟擲異常 if($error) throw new \Exception('請求發生錯誤(表檢查是否在授權域名下訪問):' . $error); return $res; } }
控制器部分程式碼:
public function reurnWeixinCode() { $jssdk = new \Think\Jssdk(WX_GONGZHONGHAO_APPID, WX_GONGZHONGHAO_APPSECRET); $signPackage = $jssdk->GetSignPackage(); return $signPackage; }
Html部分
<script type="text/javascript"> var imgUrl = "{$img_url}"; var link = "{$huodong_url}"; var descContent = '{$desc}'; var shareTitle = '{$title}'; var appid = ''; </script> <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: true, // 開啟除錯模式,呼叫的所有api的返回值會在客戶端alert出來,若要檢視傳入的引數, 可以在pc端開啟,引數資訊會通過log打出,僅在pc端時才會列印。 appId: '{$signPackage['appId']}', // 必填,公眾號的唯一標識 timestamp: {$signPackage['timestamp']}, // 必填,生成簽名的時間戳 nonceStr: '{$signPackage['nonceStr']}', // 必填,生成簽名的隨機串 signature: '{$signPackage['signature']}',// 必填,簽名,見附錄1 jsApiList: [ 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo' ] // 必填,需要使用的JS介面列表,所有JS介面列表見附錄2 }); wx.ready(function(){ //朋友圈 wx.onMenuShareTimeline({ title: shareTitle, link: link, imgUrl: imgUrl, trigger: function (res) { // alert('使用者點選並朋友圈'); }, success: function () { // 使用者確認分享後執行的回撥函式 //alert('Timeline分享成功'); }, cancel: function () { // 使用者取消分享後執行的回撥函式 // alert('您取消了分享Timeline'); } }); wx.onMenuShareAppMessage({ title: shareTitle, link: link, desc: descContent, imgUrl: imgUrl, type: '', // 分享型別,music、video或link,不填預設為link dataUrl: '', // 如果type是music或video,則要提供資料連結,預設為空 success: function () { // alert('分享給朋友分享成功'); }, cancel: function () { // alert('您取消了分享給朋友'); } }); wx.onMenuShareQQ({ title: shareTitle, desc: descContent, link: link, imgUrl: imgUrl, success: function () { // alert('qq分享成功'); }, cancel: function () { // alert('您取消了分享給qq'); } }); wx.onMenuShareWeibo({ title: shareTitle, desc: descContent, link: link, imgUrl: imgUrl, success: function () { // alert('weibo分享成功'); }, cancel: function () { // alert('weibo取消'); } }); // config資訊驗證後會執行ready方法,所有介面呼叫都必須在config介面獲得結果之後, config是一個客戶端的非同步操作,所以如果需要在頁面載入時就呼叫相關介面, 則須把相關介面放在ready函式中呼叫來確保正確執行。對於使用者觸發時才呼叫的介面, 則可以直接呼叫,不需要放在ready函式中。 }); wx.error(function (res) { // alert(res.errMsg); }); </script>