jssdk類庫
1、文件名及位置
名字:Jssdk.php
位置:extendutilJssdk.php
2、代碼
<?php namespace util; class Jssdk { protected $appid = 'xxxx'; protected $secret = 'xxxx'; /** * 獲取access_token方法 */ public function getAccessToken(){ //定義文件名稱 $name = 'token_' . md5($this->appid?.?$this->secret); ????????//定義存儲文件路徑 ????????//?$filename?=?__DIR__?.?'/cache/'?.?$name?.?'.php'; $filename?=?'../runtime/temp/'?.?$name?.?'.php'; ????????//判斷文件是否存在,如果存在,就取出文件中的數據值,如果不存在,就向微信端請求 ????????if?(is_file($filename)?&&?filemtime($filename)?+?7100?>?time()){ ????????????$result?=?include?$filename; ????????????//定義需要返回的內容$data ????????????$data?=?$result['access_token']; ????????}else{ ????????????//?https請求方式:?GET //?https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET ????????????//?調用curl方法完成請求 ????????????$url?=?'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='?.?$this->secret; ????????????$result?=?$this->curl($url); ????????????//將返回得到的json數據轉成php數組 ????????????$result?=?json_decode($result,true); ????????????//將內容寫入文件中 ????????????file_put_contents($filename,"<?php nreturn " . var_export($result,true) . ";n?>"); ????????????//定義需要返回的內容 ????????????$data?=?$result['access_token']; ????????} ????????//將得到的access_token的值返回 ????????return?$data; ????} ????/** ?????* ?????*?獲取臨時票據方法 ?????* ?????*?@return?mixed ?????*/ ????public?function?getJsapiTicket(){ ????????//存入文件中,定義文件的名稱和路徑 ????????$name?=?'ticket_'?.?md5($this->appid?.?$this->secret); ????????//定義存儲文件路徑 ????????//$filename?=?__DIR__?.?'/cache/'?.?$name?.?'.php'; $filename?=?'../runtime/temp/'?.?$name?.?'.php'; ????????//判斷是否存在臨時票據的文件,如果存在,就直接取值,如果不存在,就發送請求獲取并保存 ????????if?(is_file($filename)?&&?filemtime($filename)?+?7100?>?time()){ ????????????$result?=?include?$filename; ????????}else{ ????????????//定義請求地址 ????????????$url?=?'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this ????????????????????->getAccessToken().'&type=jsapi'; ????????????//使用curl方法發送請求,獲取臨時票據 ????????????$result?=?$this->curl($url); ????????????//轉換成php數組 ????????????$result?=?json_decode($result,true); ????????????//將獲取到的值存入文件中 ????????????file_put_contents($filename,"<?php nreturn " . var_export($result,true) . ";n?>"); ????????} ????????//定義返回的數據 ????????$data?=?$result['ticket']; ????????//將得到的臨時票據結果返回 ????????return?$data; ????} ????/** ?????*?獲取簽名方法 ?????*/ ????public?function?sign(){ ????????//需要定義4個參數,分別包括隨機數,臨時票據,時間戳和當前url地址 ????????$nonceStr?=?$this->makeStr(); ????????$ticket?=?$this->getJsapiTicket(); ????????$time?=?time(); ????????//組合url //$url?=?$_SERVER['REQUEST_SCHEME']?.?'://'?.?$_SERVER['SERVER_NAME']?.?$_SERVER['REQUEST_URI']; ????????$url?=?'http://'?.?$_SERVER['SERVER_NAME']?.?$_SERVER['REQUEST_URI']; ????????//將4個參數放入一個數組中 ????????$arr?=?[ ????????????'noncestr='?.?$nonceStr, ????????????'jsapi_ticket='?.?$ticket, ????????????'timestamp='?.?$time, ????????????'url='?.?$url ????????]; ????????//對數組進行字段化排序 ????????sort($arr,SORT_STRING); ????????//對數組進行組合成字符串 ????????$string?=?implode('&',$arr); ????????//將字符串加密生成簽名 ????????$sign?=?sha1($string); ????????//由于調用簽名方法的時候不只需要簽名,還需要生成簽名的時候的隨機數,時間戳,所以我們應該返回由這些內容組成的一個數組 ????????$reArr?=?[ ????????????'appId'?=>?$this->appid, ????????????'timestamp'?=>?$time, ????????????'nonceStr'?=>?$nonceStr, ????????????'signature'?=>?$sign, ????????????'url'?=>?$url ????????]; ????????//將數組返回 ????????return?$reArr; ????} ????/** ?????* ?????*?生成隨機數 ?????* ?????*?@return?string ?????*/ ????protected?function?makeStr(){ ????????//定義字符串組成的種子 ????????$seed?=?'www512wayanbao1qasxianrendong5tgblaochaguan8ik9500net'; ????????//通過循環來組成一個16位的隨機字符串 ????????//定義一個空字符串?用來接收組合成的字符串內容 ????????$str?=?''; ????????for?($i?=?0;$i?getAccessToken(); //echo?$data; //測試獲取jsapiticket方法 //$obj?=?new?Wx(); //$data?=?$obj->getJsapiTicket(); //echo?$data; //測試生成簽名方法 //$obj?=?new?Wx(); //$data?=?$obj->sign(); //echo?'<pre class="brush:php;toolbar:false">'; //print_r($data); ?>
后臺控制器處理
<?php namespace appindexcontroller; use thinkController; use thinkDb; use appadminmodelMenu; use utilJssdk; class Index extends Controller { public function demo(){ $id = input('id',0);//ID $catid = input('catid',0);//分類ID $modelInfo = getModInfoById($catid); $info = Db::name($modelInfo['tablename'])->where('id',$id)->find(); ????????$catinfo?=?getCatInfoById($catid); ????????$p_catname?=?getCatInfoById($catinfo['parentid'],'catname'); $obj?=?new?Jssdk(); $data?=?$obj->sign(); ????????$this->assign('infos',$info); ????????$this->assign('catids',$catid); ????????$this->assign('catnames',$catinfo['catname']); ????????$this->assign('p_catnames',$p_catname); $this->assign('data',$data); ????????return?view('../application/index/view/default/index/'?.?$modelInfo['show_template']); ????} } ?>
微信事件響應
<script></script><script> // 通過config接口注入權限驗證配置 wx.config({ debug: false, appId: '{$data.appId}', timestamp: '{$data.timestamp}', nonceStr: '{$data.nonceStr}', signature: '{$data.signature}', jsApiList: [ 'onMenuShareTimeline', 'onMenuShareAppMessage' ] }); // 通過ready接口處理成功驗證 wx.ready(function(){ // 分享到朋友圈 wx.onMenuShareTimeline({ title: '{$info.title}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用戶點擊了分享后執行的回調函數 } }); // 分享給朋友 wx.onMenuShareAppMessage({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', type: 'link', // 分享類型,music、video或link,不填默認為link dataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認為空 success: function () { // 用戶點擊了分享后執行的回調函數 } }); }); </script>
?全部分享接口
<script></script><script> // 通過config接口注入權限驗證配置 wx.config({ debug: true, appId: '{$data.appId}', timestamp: '{$data.timestamp}', nonceStr: '{$data.nonceStr}', signature: '{$data.signature}', jsApiList: [ 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone' ] }); // 通過ready接口處理成功驗證 wx.ready(function(){ // 分享到朋友圈 wx.onMenuShareTimeline({ title: '{$info.title}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用戶點擊了分享后執行的回調函數 } }); // 分享給朋友 wx.onMenuShareAppMessage({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', type: 'link', // 分享類型,music、video或link,不填默認為link dataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認為空 success: function () { // 用戶點擊了分享后執行的回調函數 } }); // 分享到QQ wx.onMenuShareQQ({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用戶確認分享后執行的回調函數 }, cancel: function () { // 用戶取消分享后執行的回調函數 } }); // 分享到騰訊微博 wx.onMenuShareWeibo({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用戶確認分享后執行的回調函數 }, cancel: function () { // 用戶取消分享后執行的回調函數 } }); // 分享到QQ空間 wx.onMenuShareQZone({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用戶確認分享后執行的回調函數 }, cancel: function () { // 用戶取消分享后執行的回調函數 } }); }); </script>
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END