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

调式微信分享

@@ -520,4 +520,85 @@ class LoginController extends HomeBaseController @@ -520,4 +520,85 @@ class LoginController extends HomeBaseController
520 ->find(); 520 ->find();
521 return $info; 521 return $info;
522 } 522 }
  523 +
  524 + //获取微信分享配置信息
  525 + public function configShareWx($url=''){
  526 + $jsapiTicket = $this->getSignature();
  527 + // 注意 URL 一定要动态获取,不能 hardcode.
  528 + $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  529 + if($url === '') {
  530 + $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  531 + }
  532 + $timestamp = time();
  533 + $nonceStr = $this->createNonceStr();
  534 + $string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url='.$url;
  535 + $signature = sha1($string);
  536 + $data = [
  537 + "appId" => $this->appkey2,
  538 + "nonceStr" => $nonceStr,
  539 + "timestamp" => $timestamp,
  540 + "url" => $url,
  541 + "signature" => $signature,
  542 + "rawString" => $string
  543 + ];
  544 + $this->apiResponse(1, '成功',$data);
  545 + }
  546 +
  547 + //获取微信分享签名随机字符串
  548 + public function createNonceStr($length = 16) {
  549 + $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  550 + $str = "";
  551 + for ($i = 0; $i < $length; $i++) {
  552 + $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  553 + }
  554 + return $str;
  555 + }
  556 +
  557 + //获取微信分享签名
  558 + public function getSignature(){
  559 + $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;
  560 + $res = $this->http_get($url);
  561 + $json_arr = json_decode($res,true);
  562 + if(isset($json_arr['errcode'])&&!empty($json_arr['errcode'])){
  563 + //用户取消登录
  564 + $this->apiResponse(0, '获取配置失败');
  565 + }
  566 + $token = $json_arr['access_token'];
  567 + $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
  568 + $res = $this->http_get($url);
  569 + $json_arr = json_decode($res,true);
  570 + $ticket = $json_arr['ticket'];
  571 + return $ticket;
  572 + }
  573 +
  574 + //curl get请求
  575 + public function http_get($url){
  576 + $curl = curl_init();//启动一个CURL会话
  577 + curl_setopt($curl, CURLOPT_URL,$url);
  578 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  579 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  580 + curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  581 + curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
  582 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  583 + $result = curl_exec($curl); //执行操作
  584 + curl_close($curl);
  585 + return $result;
  586 + }
  587 +
  588 + //curl post请求
  589 + public function http_post($url,$data){
  590 + $curl = curl_init();//启动一个CURL会话
  591 + curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  592 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  593 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  594 + curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
  595 + curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
  596 + curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  597 + curl_setopt($curl, CURLOPT_HEADER, false); // 开启header
  598 + //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
  599 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  600 + $result = curl_exec($curl); //执行操作
  601 + curl_close($curl);
  602 + return $result;
  603 + }
523 } 604 }
@@ -34,22 +34,48 @@ @@ -34,22 +34,48 @@
34 </li> 34 </li>
35 </ul> 35 </ul>
36 </div> 36 </div>
  37 +<script src="__TMPL__/public/assets/js/jquery-2.1.0.js"></script>
37 <script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script> 38 <script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
38 <script> 39 <script>
39 - wx.config({  
40 - debug: false,  
41 - appId: "{$data.appid}",  
42 - timestamp: "{$data.timestamp}",  
43 - nonceStr: "{$data.nonceStr}",  
44 - signature: "{$data.signature}",  
45 - jsApiList: [  
46 - 'checkJsApi',  
47 - 'onMenuShareTimeline',  
48 - 'onMenuShareAppMessage'  
49 - ]  
50 - });  
51 - wx.ready(function(){  
52 - // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。 40 + $(function () {
  41 + var appid = '';
  42 + var timestamp = '';
  43 + var nonceStr = '';
  44 + var signature = '';
  45 + $.ajax({
  46 + type: 'POST',
  47 + url: '/portal/login/configShareWx',
  48 + data: {
  49 + },
  50 + dataType: 'json',
  51 + async: false,
  52 + success: function(data) {
  53 + if (data.code == 1) {
  54 + appid = data.appid;
  55 + timestamp = data.timestamp;
  56 + nonceStr = data.nonceStr;
  57 + signature = data.signature;
  58 + } else {
  59 + console.log(data);
  60 + }
  61 + }
  62 + });
  63 + wx.config({
  64 + debug: false,
  65 + appId: appid,
  66 + timestamp: timestamp,
  67 + nonceStr: nonceStr,
  68 + signature: signature,
  69 + jsApiList: [
  70 + 'checkJsApi',
  71 + 'onMenuShareTimeline',
  72 + 'onMenuShareAppMessage'
  73 + ]
  74 + });
  75 + wx.ready(function(){
  76 + // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
53 77
  78 + });
54 }); 79 });
  80 +
55 </script> 81 </script>
@@ -21,9 +21,6 @@ use think\Session; @@ -21,9 +21,6 @@ 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  
26 -  
27 protected function initialize() 24 protected function initialize()
28 { 25 {
29 // 监听home_init 26 // 监听home_init
@@ -40,7 +37,6 @@ class HomeBaseController extends BaseController @@ -40,7 +37,6 @@ class HomeBaseController extends BaseController
40 $this->is_like(); 37 $this->is_like();
41 $this->getShareCount(); 38 $this->getShareCount();
42 $keyword = $this->request->param('keyword'); 39 $keyword = $this->request->param('keyword');
43 - $this->configShareWx();  
44 $this->assign('keyword',$keyword); 40 $this->assign('keyword',$keyword);
45 } 41 }
46 42
@@ -399,85 +395,4 @@ hello; @@ -399,85 +395,4 @@ hello;
399 $this->assign('share',$share); 395 $this->assign('share',$share);
400 } 396 }
401 397
402 - //获取微信分享配置信息  
403 - public function configShareWx($url=''){  
404 - $jsapiTicket = $this->getSignature();  
405 - // 注意 URL 一定要动态获取,不能 hardcode.  
406 - $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";  
407 - if($url === '') {  
408 - $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";  
409 - }  
410 - $timestamp = time();  
411 - $nonceStr = $this->createNonceStr();  
412 - $string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url='.$url;  
413 - $signature = sha1($string);  
414 - $data = [  
415 - "appId" => $this->appkey2,  
416 - "nonceStr" => $nonceStr,  
417 - "timestamp" => $timestamp,  
418 - "url" => $url,  
419 - "signature" => $signature,  
420 - "rawString" => $string  
421 - ];  
422 - $this->assign('data',$data);  
423 - }  
424 -  
425 - //获取微信分享签名随机字符串  
426 - public function createNonceStr($length = 16) {  
427 - $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
428 - $str = "";  
429 - for ($i = 0; $i < $length; $i++) {  
430 - $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);  
431 - }  
432 - return $str;  
433 - }  
434 -  
435 - //获取微信分享签名  
436 - public function getSignature(){  
437 - $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;  
438 - $res = $this->http_get($url);  
439 - $json_arr = json_decode($res,true);  
440 - if(!isset($json_arr['access_token'])&&empty($json_arr['access_token'])){  
441 - //用户取消登录  
442 - $this->redirect('/portal/login/thirdLogin');  
443 - }  
444 - $token = $json_arr['access_token'];  
445 - $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';  
446 - $res = $this->http_get($url);  
447 - $json_arr = json_decode($res,true);  
448 - $ticket = $json_arr['ticket'];  
449 - return $ticket;  
450 - }  
451 -  
452 - //curl get请求  
453 - public function http_get($url){  
454 - $curl = curl_init();//启动一个CURL会话  
455 - curl_setopt($curl, CURLOPT_URL,$url);  
456 - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
457 - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
458 - curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
459 - curl_setopt($curl, CURLOPT_HEADER, false);//不开启header  
460 - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回  
461 - $result = curl_exec($curl); //执行操作  
462 - curl_close($curl);  
463 - return $result;  
464 - }  
465 -  
466 - //curl post请求  
467 - public function http_post($url,$data){  
468 - $curl = curl_init();//启动一个CURL会话  
469 - curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址  
470 - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
471 - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
472 - curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求  
473 - curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包  
474 - curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
475 - curl_setopt($curl, CURLOPT_HEADER, false); // 开启header  
476 - //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部  
477 - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回  
478 - $result = curl_exec($curl); //执行操作  
479 - curl_close($curl);  
480 - return $result;  
481 - }  
482 -  
483 } 398 }