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

测试微信移动登录

@@ -520,35 +520,4 @@ class LoginController extends HomeBaseController @@ -520,35 +520,4 @@ class LoginController extends HomeBaseController
520 ->find(); 520 ->find();
521 return $info; 521 return $info;
522 } 522 }
523 -  
524 - //curl get请求  
525 - public function http_get($url){  
526 - $curl = curl_init();//启动一个CURL会话  
527 - curl_setopt($curl, CURLOPT_URL,$url);  
528 - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
529 - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
530 - curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
531 - curl_setopt($curl, CURLOPT_HEADER, false);//不开启header  
532 - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回  
533 - $result = curl_exec($curl); //执行操作  
534 - curl_close($curl);  
535 - return $result;  
536 - }  
537 -  
538 - //curl post请求  
539 - public function http_post($url,$data){  
540 - $curl = curl_init();//启动一个CURL会话  
541 - curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址  
542 - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
543 - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
544 - curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求  
545 - curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包  
546 - curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
547 - curl_setopt($curl, CURLOPT_HEADER, false); // 开启header  
548 - //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部  
549 - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回  
550 - $result = curl_exec($curl); //执行操作  
551 - curl_close($curl);  
552 - return $result;  
553 - }  
554 } 523 }
@@ -34,7 +34,9 @@ @@ -34,7 +34,9 @@
34 <!--登陆--> 34 <!--登陆-->
35 <div class="login_right_main clearfix"> 35 <div class="login_right_main clearfix">
36 <div class="WeChat"> 36 <div class="WeChat">
37 - <a href="https://open.weixin.qq.com/connect/qrconnect?appid=wx9cfa880272f186bf&redirect_uri=http://www.starplanet.cn/portal/login/wx_login&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect"><img src="__TMPL__/public/assets/starImg/aicon_89.png" alt=""></a> 37 + <a href="https://open.weixin.qq.com/connect/qrconnect?appid=wx9cfa880272f186bf&redirect_uri=http://www.starplanet.cn/portal/login/wx_login&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect">
  38 + <img src="__TMPL__/public/assets/starImg/aicon_89.png" alt="">
  39 + </a>
38 </div> 40 </div>
39 <form action="https://api.weibo.com/oauth2/authorize" method="POST" id="wb_form"> 41 <form action="https://api.weibo.com/oauth2/authorize" method="POST" id="wb_form">
40 <div class="Sina" id="wb_login"> 42 <div class="Sina" id="wb_login">
@@ -267,10 +267,13 @@ $(function() { @@ -267,10 +267,13 @@ $(function() {
267 $('#wb_form').submit(); 267 $('#wb_form').submit();
268 }); 268 });
269 269
270 - //微信登录 270 + //微信登录(暂时无用)
271 $('#wx_login').click(function(){ 271 $('#wx_login').click(function(){
272 window.location.href = '/portal/login/wx_login_mobile'; 272 window.location.href = '/portal/login/wx_login_mobile';
273 }); 273 });
  274 +
  275 + //微信分享
  276 +
274 }); 277 });
275 //第三方分享 278 //第三方分享
276 function thirdShare(url){ 279 function thirdShare(url){
@@ -34,3 +34,22 @@ @@ -34,3 +34,22 @@
34 </li> 34 </li>
35 </ul> 35 </ul>
36 </div> 36 </div>
  37 +<script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
  38 +<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函数中。
  53 +
  54 + });
  55 +</script>
@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
10 // +---------------------------------------------------------------------- 10 // +----------------------------------------------------------------------
11 namespace cmf\controller; 11 namespace cmf\controller;
12 12
  13 +use app\portal\controller\LoginController;
13 use think\Db; 14 use think\Db;
14 use app\admin\model\ThemeModel; 15 use app\admin\model\ThemeModel;
15 use think\View; 16 use think\View;
@@ -20,6 +21,8 @@ use think\Session; @@ -20,6 +21,8 @@ use think\Session;
20 21
21 class HomeBaseController extends BaseController 22 class HomeBaseController extends BaseController
22 { 23 {
  24 + private $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey
  25 + private $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret
23 26
24 protected function initialize() 27 protected function initialize()
25 { 28 {
@@ -37,6 +40,7 @@ class HomeBaseController extends BaseController @@ -37,6 +40,7 @@ class HomeBaseController extends BaseController
37 $this->is_like(); 40 $this->is_like();
38 $this->getShareCount(); 41 $this->getShareCount();
39 $keyword = $this->request->param('keyword'); 42 $keyword = $this->request->param('keyword');
  43 + $this->configShareWx();
40 $this->assign('keyword',$keyword); 44 $this->assign('keyword',$keyword);
41 } 45 }
42 46
@@ -395,4 +399,85 @@ hello; @@ -395,4 +399,85 @@ hello;
395 $this->assign('share',$share); 399 $this->assign('share',$share);
396 } 400 }
397 401
  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 +
398 } 483 }