ThinkPHP5Ôõô¼¯³ÉJS-SDKʵÏÖ΢ÐÅ×Ô½ç˵·ÖÏí¹¦Ð§
jssdkÀà¿â
1¡¢ÎļþÃû¼°Î»ÖÃ
Ãû×Ö£ºJssdk.php
λÖãºextend\util\Jssdk.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'; //ÅжÏÎļþÊÇ·ñ±£´æ,ÈôÊDZ£´æ,¾ÍÈ¡³öÎļþÖеÄÊý¾ÝÖµ,ÈôÊDz»±£´æ,¾ÍÏò΢ÐŶËÇëÇó 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'; //ÅжÏÊÇ·ñ±£´æÔÝʱƱ¾ÝµÄÎļþ,ÈôÊDZ£´æ,¾ÍÖ±½ÓÈ¡Öµ,ÈôÊDz»±£´æ,¾Í·¢ËÍÇëÇó»ñÈ¡²¢ÉúÑÄ 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 < 16; $i++){ //½ç˵һ¸öËæ»úÊý $num = rand(0,strlen($seed) - 1); //Ñ»·ÅþÁ¬Ëæ»úÌìÉúµÄ×Ö·û´® $str .= $seed[$num]; } //½«Ëæ»úÊý·µ»Ø return $str; } /** * * ЧÀÍÆ÷Ö®¼äÇëÇóµÄcurlÒªÁì * * @param $url ÇëÇóµØµã * @param array $field post²ÎÊý * @return string */ public function curl($url,$field = []){ //³õʼ»¯curl $ch = curl_init(); //ÉèÖÃÇëÇóµÄµØµã curl_setopt($ch,CURLOPT_URL,$url); //ÉèÖÃÎüÊÕ·µ»ØµÄÊý¾Ý,²»Ö±½ÓչʾÔÚÒ³Ãæ curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //ÉèÖÃեȡ֤ÊéУÑé curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); //ÅжÏÊÇ·ñΪpostÇëÇó·½·¨,ÈôÊÇת´ïÁ˵ڶþ¸ö²ÎÊý,¾Í´ú±íÊÇpostÇëÇó,ÈôÊÇôÓÐת´ï,µÚ¶þ¸ö²ÎÊýΪ¿Õ,¾ÍÊÇgetÇëÇó if (!empty($field)){ //ÉèÖÃÇëÇó³¬Ê±Ê±¼ä curl_setopt($ch,CURLOPT_TIMEOUT,30); //ÉèÖÿªÆôpost curl_setopt($ch,CURLOPT_POST,1); //ת´ïpostÊý¾Ý curl_setopt($ch,CURLOPT_POSTFIELDS,$field); } //½ç˵һ¸ö¿Õ×Ö·û´®,ÓÃÀ´ÎüÊÕÇëÇóµÄЧ¹û $data = ''; if (curl_exec($ch)){ $data = curl_multi_getcontent($ch); } //¹Ø±Õcurl curl_close($ch); //½«»ñµÃµÄЧ¹û·µ»Ø return $data; } } //²âÊÔ»ñÈ¡access_tokenÖµµÄÒªÁì //$obj = new Wx(); //$data = $obj->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 app\index\controller; use think\Controller; use think\Db; use app\admin\model\Menu; use util\Jssdk; 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 src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> <script type="text/javascript"> // ͨ¹ý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 src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> <script type="text/javascript"> // ͨ¹ý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>
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇThinkPHP5Ôõô¼¯³ÉJS-SDKʵÏÖ΢ÐÅ×Ô½ç˵·ÖÏí¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡
ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£