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

调式分享

@@ -20,15 +20,6 @@ use app\portal\model\PortalPostModel; @@ -20,15 +20,6 @@ use app\portal\model\PortalPostModel;
20 class LoginController extends HomeBaseController 20 class LoginController extends HomeBaseController
21 { 21 {
22 private $limit = 8;//收藏,搜索分页 22 private $limit = 8;//收藏,搜索分页
23 - private $appkey = 737607150;//微博appkey  
24 - private $appsecret = 'd80b43a1e74e8ba095590b36a3459480';//微博appsecret  
25 - private $redirect_uri = 'http://www.starplanet.cn/portal/login/wb_login';//回调地址  
26 -  
27 - private $appkey1 = 'wx9cfa880272f186bf';//微信开放平台appkey  
28 - private $appsecret1 = '11b643393b1e54d6ef5eaa984ba4e545';//微信开放平台appsecret  
29 -  
30 - private $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey  
31 - private $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret  
32 23
33 //登录页面 24 //登录页面
34 public function login(){ 25 public function login(){
@@ -522,92 +513,11 @@ class LoginController extends HomeBaseController @@ -522,92 +513,11 @@ class LoginController extends HomeBaseController
522 return $info; 513 return $info;
523 } 514 }
524 515
525 - //获取微信分享配置信息  
526 - public function wxShare($url=''){  
527 - $jsapiTicket = $this->getSignature();  
528 - // 注意 URL 一定要动态获取,不能 hardcode.  
529 - $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";  
530 - if($url === '') {  
531 - $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";  
532 - }  
533 - $timestamp = time();  
534 - $nonceStr = $this->createNonceStr();  
535 - $string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url='.$url;  
536 - $signature = sha1($string);  
537 - $data = [  
538 - "appId" => $this->appkey2,  
539 - "nonceStr" => $nonceStr,  
540 - "timestamp" => $timestamp,  
541 - "url" => $url,  
542 - "signature" => $signature,  
543 - "rawString" => $string  
544 - ];  
545 - $this->assign('data',$data);  
546 - }  
547 -  
548 - //获取微信分享签名随机字符串  
549 - public function createNonceStr($length = 16) {  
550 - $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
551 - $str = "";  
552 - for ($i = 0; $i < $length; $i++) {  
553 - $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);  
554 - }  
555 - return $str;  
556 - }  
557 -  
558 - //获取access_token  
559 - public function getWxAccessToken(){  
560 - $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;  
561 - $res = $this->http_get($url);  
562 - $json_arr = json_decode($res,true);  
563 - $token = $json_arr['access_token'];  
564 - return $token;  
565 - }  
566 -  
567 - //获取微信分享签名  
568 - public function getSignature(){  
569 - if(isset($_SESSION['ticket_expire_time']) && $_SESSION['ticket_expire_time'] > time() && $_SESSION['ticket']){  
570 - $ticket = $_SESSION['ticket'];  
571 - }else{  
572 - $token = $this->getWxAccessToken();  
573 - $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';  
574 - $res = $this->http_get($url);  
575 - $json_arr = json_decode($res,true);  
576 - $ticket = $json_arr['ticket'];  
577 - $_SESSION['ticket'] = $ticket;  
578 - $_SESSION['ticket_expire_time'] = time()+7000;  
579 - }  
580 - return $ticket;  
581 - }  
582 -  
583 - //curl get请求  
584 - public function http_get($url){  
585 - $curl = curl_init();//启动一个CURL会话  
586 - curl_setopt($curl, CURLOPT_URL,$url);  
587 - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
588 - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
589 - curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
590 - curl_setopt($curl, CURLOPT_HEADER, false);//不开启header  
591 - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回  
592 - $result = curl_exec($curl); //执行操作  
593 - curl_close($curl);  
594 - return $result;  
595 - }  
596 -  
597 - //curl post请求  
598 - public function http_post($url,$data){  
599 - $curl = curl_init();//启动一个CURL会话  
600 - curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址  
601 - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
602 - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
603 - curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求  
604 - curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包  
605 - curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
606 - curl_setopt($curl, CURLOPT_HEADER, false); // 开启header  
607 - //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部  
608 - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回  
609 - $result = curl_exec($curl); //执行操作  
610 - curl_close($curl);  
611 - return $result; 516 + //微信分享
  517 + public function wx_share(){
  518 + $article_id = $this->request->param('id');
  519 + $postModel = new PortalPostModel();
  520 + $postModel->where('id', $article_id)->setInc('post_share_wx', 1);
  521 + $this->apiResponse(1, '分享成功!');
612 } 522 }
613 } 523 }
@@ -152,6 +152,7 @@ @@ -152,6 +152,7 @@
152 </div> 152 </div>
153 </div> 153 </div>
154 <!--微信分享--> 154 <!--微信分享-->
  155 + <include file="public@share" />
155 <include file="public@footer" /> 156 <include file="public@footer" />
156 </body> 157 </body>
157 <script src="__TMPL__/public/assets/js/base.js"></script> 158 <script src="__TMPL__/public/assets/js/base.js"></script>
@@ -18,11 +18,11 @@ @@ -18,11 +18,11 @@
18 wx.ready(function () { //需在用户可能点击分享按钮前就先调用 18 wx.ready(function () { //需在用户可能点击分享按钮前就先调用
19 wx.updateAppMessageShareData({ 19 wx.updateAppMessageShareData({
20 title: title_title, // 分享标题 20 title: title_title, // 分享标题
21 - desc: '', // 分享描述 21 + desc: url, // 分享描述
22 link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 22 link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
23 imgUrl: '', // 分享图标 23 imgUrl: '', // 分享图标
24 success: function (e) { 24 success: function (e) {
25 - // 设置成功 25 + wx_Share();
26 } 26 }
27 }); 27 });
28 wx.updateTimelineShareData({ 28 wx.updateTimelineShareData({
@@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
31 imgUrl: '', // 分享图标 31 imgUrl: '', // 分享图标
32 success: function () { 32 success: function () {
33 // 设置成功 33 // 设置成功
  34 + wx_Share();
34 } 35 }
35 }); 36 });
36 }); 37 });
@@ -39,4 +40,20 @@ @@ -39,4 +40,20 @@
39 }); 40 });
40 }); 41 });
41 42
  43 + function wx_Share(){
  44 + var article_id = getUrlParam('id');
  45 + $.ajax({
  46 + type: 'POST',
  47 + url: '/portal/login/wx_share',
  48 + data: {
  49 + 'id': article_id,
  50 + },
  51 + dataType: 'json',
  52 + async: false,
  53 + success: function(data) {
  54 +
  55 + }
  56 + });
  57 + }
  58 +
42 </script> 59 </script>
@@ -35,3 +35,4 @@ @@ -35,3 +35,4 @@
35 </ul> 35 </ul>
36 </div> 36 </div>
37 <!--微信分享--> 37 <!--微信分享-->
  38 +<include file="public@share" />
@@ -21,6 +21,15 @@ use think\Session; @@ -21,6 +21,15 @@ use think\Session;
21 21
22 class HomeBaseController extends BaseController 22 class HomeBaseController extends BaseController
23 { 23 {
  24 + public $appkey = 737607150;//微博appkey
  25 + public $appsecret = 'd80b43a1e74e8ba095590b36a3459480';//微博appsecret
  26 + public $redirect_uri = 'http://www.starplanet.cn/portal/login/wb_login';//回调地址
  27 +
  28 + public $appkey1 = 'wx9cfa880272f186bf';//微信开放平台appkey
  29 + public $appsecret1 = '11b643393b1e54d6ef5eaa984ba4e545';//微信开放平台appsecret
  30 +
  31 + public $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey
  32 + public $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret
24 33
25 protected function initialize() 34 protected function initialize()
26 { 35 {
@@ -39,6 +48,13 @@ class HomeBaseController extends BaseController @@ -39,6 +48,13 @@ class HomeBaseController extends BaseController
39 $this->getShareCount(); 48 $this->getShareCount();
40 $keyword = $this->request->param('keyword'); 49 $keyword = $this->request->param('keyword');
41 $this->assign('keyword',$keyword); 50 $this->assign('keyword',$keyword);
  51 +
  52 + $cmfDefaultTheme = cmf_get_current_theme();
  53 + //域名后面相对路径
  54 + $url = $_SERVER['REQUEST_URI'];
  55 + if($cmfDefaultTheme == 'simpleboot3_mobile' && strpos($url,'Detail')!==false){
  56 + $this->wxShare();
  57 + }
42 } 58 }
43 59
44 protected function _initializeView() 60 protected function _initializeView()
@@ -396,4 +412,93 @@ hello; @@ -396,4 +412,93 @@ hello;
396 $this->assign('share',$share); 412 $this->assign('share',$share);
397 } 413 }
398 414
  415 + //获取微信分享配置信息
  416 + public function wxShare($url=''){
  417 + $jsapiTicket = $this->getSignature();
  418 + // 注意 URL 一定要动态获取,不能 hardcode.
  419 + $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  420 + if($url === '') {
  421 + $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  422 + }
  423 + $timestamp = time();
  424 + $nonceStr = $this->createNonceStr();
  425 + $string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url='.$url;
  426 + $signature = sha1($string);
  427 + $data = [
  428 + "appId" => $this->appkey2,
  429 + "nonceStr" => $nonceStr,
  430 + "timestamp" => $timestamp,
  431 + "url" => $url,
  432 + "signature" => $signature,
  433 + "rawString" => $string
  434 + ];
  435 + $this->assign('data',$data);
  436 + }
  437 +
  438 + //获取微信分享签名随机字符串
  439 + public function createNonceStr($length = 16) {
  440 + $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  441 + $str = "";
  442 + for ($i = 0; $i < $length; $i++) {
  443 + $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  444 + }
  445 + return $str;
  446 + }
  447 +
  448 + //获取access_token
  449 + public function getWxAccessToken(){
  450 + $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;
  451 + $res = $this->http_get($url);
  452 + $json_arr = json_decode($res,true);
  453 + $token = $json_arr['access_token'];
  454 + return $token;
  455 + }
  456 +
  457 + //获取微信分享签名
  458 + public function getSignature(){
  459 + if(isset($_SESSION['ticket_expire_time']) && $_SESSION['ticket_expire_time'] > time() && $_SESSION['ticket']){
  460 + $ticket = $_SESSION['ticket'];
  461 + }else{
  462 + $token = $this->getWxAccessToken();
  463 + $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
  464 + $res = $this->http_get($url);
  465 + $json_arr = json_decode($res,true);
  466 + $ticket = $json_arr['ticket'];
  467 + $_SESSION['ticket'] = $ticket;
  468 + $_SESSION['ticket_expire_time'] = time()+7000;
  469 + }
  470 + return $ticket;
  471 + }
  472 +
  473 + //curl get请求
  474 + public function http_get($url){
  475 + $curl = curl_init();//启动一个CURL会话
  476 + curl_setopt($curl, CURLOPT_URL,$url);
  477 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  478 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  479 + curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  480 + curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
  481 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  482 + $result = curl_exec($curl); //执行操作
  483 + curl_close($curl);
  484 + return $result;
  485 + }
  486 +
  487 + //curl post请求
  488 + public function http_post($url,$data){
  489 + $curl = curl_init();//启动一个CURL会话
  490 + curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  491 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  492 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  493 + curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
  494 + curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
  495 + curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  496 + curl_setopt($curl, CURLOPT_HEADER, false); // 开启header
  497 + //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
  498 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  499 + $result = curl_exec($curl); //执行操作
  500 + curl_close($curl);
  501 + return $result;
  502 + }
  503 +
399 } 504 }