作者 jinglong
1 个管道 的构建 通过 耗费 0 秒

调式分享

@@ -35,4 +35,3 @@ @@ -35,4 +35,3 @@
35 </ul> 35 </ul>
36 </div> 36 </div>
37 <!--微信分享--> 37 <!--微信分享-->
38 -<include file="public@share" />  
@@ -21,8 +21,7 @@ use think\Session; @@ -21,8 +21,7 @@ use think\Session;
21 21
22 class HomeBaseController extends BaseController 22 class HomeBaseController extends BaseController
23 { 23 {
24 - private $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey  
25 - private $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret 24 +
26 protected function initialize() 25 protected function initialize()
27 { 26 {
28 // 监听home_init 27 // 监听home_init
@@ -38,7 +37,6 @@ class HomeBaseController extends BaseController @@ -38,7 +37,6 @@ class HomeBaseController extends BaseController
38 $this->is_collection(); 37 $this->is_collection();
39 $this->is_like(); 38 $this->is_like();
40 $this->getShareCount(); 39 $this->getShareCount();
41 - $this->wxShare();  
42 $keyword = $this->request->param('keyword'); 40 $keyword = $this->request->param('keyword');
43 $this->assign('keyword',$keyword); 41 $this->assign('keyword',$keyword);
44 } 42 }
@@ -398,76 +396,4 @@ hello; @@ -398,76 +396,4 @@ hello;
398 $this->assign('share',$share); 396 $this->assign('share',$share);
399 } 397 }
400 398
401 - //获取微信分享配置信息  
402 - public function wxShare($url=''){  
403 - $jsapiTicket = $this->getSignature();  
404 - // 注意 URL 一定要动态获取,不能 hardcode.  
405 - $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";  
406 - if($url === '') {  
407 - $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";  
408 - }  
409 - $timestamp = time();  
410 - $nonceStr = $this->createNonceStr();  
411 - $string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url='.$url;  
412 - $signature = sha1($string);  
413 - $data = [  
414 - "appId" => $this->appkey2,  
415 - "nonceStr" => $nonceStr,  
416 - "timestamp" => $timestamp,  
417 - "url" => $url,  
418 - "signature" => $signature,  
419 - "rawString" => $string  
420 - ];  
421 - $this->assign('data',$data);  
422 - }  
423 -  
424 - //获取微信分享签名随机字符串  
425 - public function createNonceStr($length = 16) {  
426 - $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
427 - $str = "";  
428 - for ($i = 0; $i < $length; $i++) {  
429 - $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);  
430 - }  
431 - return $str;  
432 - }  
433 -  
434 - //获取access_token  
435 - public function getWxAccessToken(){  
436 - $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;  
437 - $res = $this->http_get($url);  
438 - $json_arr = json_decode($res,true);  
439 - $token = $json_arr['access_token'];  
440 - return $token;  
441 - }  
442 -  
443 - //获取微信分享签名  
444 - public function getSignature(){  
445 - if(isset($_SESSION['ticket_expire_time']) && $_SESSION['ticket_expire_time'] > time() && $_SESSION['ticket']){  
446 - $ticket = $_SESSION['ticket'];  
447 - }else{  
448 - $token = $this->getWxAccessToken();  
449 - $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';  
450 - $res = $this->http_get($url);  
451 - $json_arr = json_decode($res,true);  
452 - $ticket = $json_arr['ticket'];  
453 - $_SESSION['ticket'] = $ticket;  
454 - $_SESSION['ticket_expire_time'] = time()+7000;  
455 - }  
456 - return $ticket;  
457 - }  
458 -  
459 - //curl get请求  
460 - public function http_get($url){  
461 - $curl = curl_init();//启动一个CURL会话  
462 - curl_setopt($curl, CURLOPT_URL,$url);  
463 - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
464 - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
465 - curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
466 - curl_setopt($curl, CURLOPT_HEADER, false);//不开启header  
467 - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回  
468 - $result = curl_exec($curl); //执行操作  
469 - curl_close($curl);  
470 - return $result;  
471 - }  
472 -  
473 } 399 }