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

写第三方微博登录

@@ -11,8 +11,8 @@ @@ -11,8 +11,8 @@
11 11
12 return [ 12 return [
13 // 应用调试模式 13 // 应用调试模式
14 - 'app_debug' => false, 14 + 'app_debug' => true,
15 // 应用Trace 15 // 应用Trace
16 - 'app_trace' => false, 16 + 'app_trace' => true,
17 17
18 ]; 18 ];
@@ -19,6 +19,9 @@ use think\Config; @@ -19,6 +19,9 @@ use think\Config;
19 class LoginController extends HomeBaseController 19 class LoginController extends HomeBaseController
20 { 20 {
21 private $limit = 8;//收藏,搜索分页 21 private $limit = 8;//收藏,搜索分页
  22 + private $appkey = 737607150;//微博appkey
  23 + private $appsecret = 'd80b43a1e74e8ba095590b36a3459480';//微博appsecret
  24 + private $redirect_uri = 'http://www.xingqiu.cn/portal/login/wb_login';//回调地址
22 //登录页面 25 //登录页面
23 public function login(){ 26 public function login(){
24 return $this->fetch(); 27 return $this->fetch();
@@ -94,6 +97,7 @@ class LoginController extends HomeBaseController @@ -94,6 +97,7 @@ class LoginController extends HomeBaseController
94 $info['mobile'] = $param['mobile']; 97 $info['mobile'] = $param['mobile'];
95 $info['user_pass'] = cmf_password($param['user_pass']); 98 $info['user_pass'] = cmf_password($param['user_pass']);
96 $info['user_type'] = 2; 99 $info['user_type'] = 2;
  100 + $info['source'] = '本站';
97 $info['create_time'] = time(); 101 $info['create_time'] = time();
98 $res = $userModel->allowField(true)->save($info); 102 $res = $userModel->allowField(true)->save($info);
99 if($res){ 103 if($res){
@@ -355,6 +359,83 @@ class LoginController extends HomeBaseController @@ -355,6 +359,83 @@ class LoginController extends HomeBaseController
355 var_dump($res);exit; 359 var_dump($res);exit;
356 } 360 }
357 361
  362 + //第三方微博登录
  363 + public function wb_login(){
  364 + $code = $this->request->get('code');
  365 + $url = 'https://api.weibo.com/oauth2/access_token';
  366 + //要传的数据
  367 + $data = [
  368 + 'client_id' => $this->appkey,
  369 + 'client_secret'=>$this->appsecret,
  370 + 'grant_type'=>'authorization_code',
  371 + 'code'=>$code,
  372 + 'redirect_uri'=>$this->redirect_uri //回调地址
  373 + ];
  374 + $res = $this->http_post($url,$data);
  375 + $json_arr = json_decode($res,true);
  376 + //获取access_token
  377 + if(isset($json_arr['error_code'])&&!empty($json_arr['error_code'])){
  378 + //用户取消登录
  379 + $this->redirect('/portal/login/thirdLogin');
  380 + }
  381 + $token = $json_arr['access_token'];
  382 + //存token到session
  383 + session('token', $token);
  384 + $uid = $json_arr['uid'];
  385 + //发送get请求,获取登陆用户的信息
  386 + $info = $this->http_get('https://api.weibo.com/2/users/show.json?access_token='.$token.'&uid='.$uid);
  387 + $info = json_decode($info,true);
  388 +
  389 + //查询该微博用户是否存在
  390 + $where = ['wb_id'=>$info['id'],'source'=>'微博'];
  391 + $user = $this->findThird($where);
  392 + //获取微博id,昵称,头像
  393 + $userModel = new UserModel();
  394 + if($user){
  395 + $users['user_nickname'] = $info['screen_name'];
  396 + $users['avatar'] = $info['profile_image_url'];
  397 + $userModel->where(['wb_id'=>$info['id'],'source'=>'微博'])->update($users);
  398 + }else{
  399 + $users['wb_id'] = $info['id'];
  400 + $users['user_nickname'] = $info['screen_name'];
  401 + $users['avatar'] = $info['profile_image_url'];
  402 + $users['source'] = '微博';
  403 + $users['user_type'] = 2;
  404 + $users['create_time'] = time();
  405 + $userModel->create($users);
  406 + }
  407 + $userInfo = $this->findThird($where);
  408 + cmf_update_current_user($userInfo);
  409 + $this->redirect('/');
  410 + }
  411 +
  412 + //微博分享
  413 + public function wb_share(){
  414 + $token = session('token');
  415 + $url = 'https://api.weibo.com/2/statuses/share.json';
  416 + $data = [
  417 + 'access_token' => $token,
  418 + 'status' => URLencode('http://www.starplanet.cn/portal/enjoy/getEnjoyDetail?id=52')
  419 + ];
  420 + $res = $this->http_post($url,$data);
  421 + $json_arr = json_decode($res,true);
  422 + var_dump($json_arr);exit;
  423 + }
  424 +
  425 + //取消授权
  426 + public function wb_cancel(){
  427 + echo '取消了';
  428 +// $this->redirect('/');
  429 + }
  430 +
  431 + //查询第三方用户是否存在
  432 + public function findThird($where){
  433 + $info = Db::name('user')
  434 + ->where($where)
  435 + ->find();
  436 + return $info;
  437 + }
  438 +
358 //curl get请求 439 //curl get请求
359 public function http_get($url){ 440 public function http_get($url){
360 $curl = curl_init();//启动一个CURL会话 441 $curl = curl_init();//启动一个CURL会话
@@ -370,16 +451,16 @@ class LoginController extends HomeBaseController @@ -370,16 +451,16 @@ class LoginController extends HomeBaseController
370 } 451 }
371 452
372 //curl post请求 453 //curl post请求
373 - public function http_post($url,$data,$headers){ 454 + public function http_post($url,$data){
374 $curl = curl_init();//启动一个CURL会话 455 $curl = curl_init();//启动一个CURL会话
375 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 456 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
376 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 457 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
377 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 458 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
378 curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求 459 curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
379 - curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 460 + curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
380 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 461 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
381 - curl_setopt($curl, CURLOPT_HEADER, true); // 开启header  
382 - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部 462 + curl_setopt($curl, CURLOPT_HEADER, false); // 开启header
  463 + //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
383 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回 464 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
384 $result = curl_exec($curl); //执行操作 465 $result = curl_exec($curl); //执行操作
385 curl_close($curl); 466 curl_close($curl);
@@ -57,25 +57,28 @@ @@ -57,25 +57,28 @@
57 <script src="__TMPL__/public/assets/js/public.js"></script> 57 <script src="__TMPL__/public/assets/js/public.js"></script>
58 <script> 58 <script>
59 $(function() { 59 $(function() {
60 - $('.save_info').click(function() {  
61 - var nickname = $.trim($('#nickname').val());  
62 - $.ajax({  
63 - type: 'POST',  
64 - url: "/portal/login/updateNickname",  
65 - data: {  
66 - 'nickname': nickname,  
67 - },  
68 - dataType: 'json',  
69 - success: function(data) {  
70 - console.log(data);  
71 - if (data.code == 1) {  
72 - mask(data.msg);  
73 - window.location.href = '/';  
74 - } else {  
75 - mask(data.msg); 60 + var source = $('#source').val();
  61 + if(source == '本站'){
  62 + $('.save_info').click(function() {
  63 + var nickname = $.trim($('#nickname').val());
  64 + $.ajax({
  65 + type: 'POST',
  66 + url: "/portal/login/updateNickname",
  67 + data: {
  68 + 'nickname': nickname,
  69 + },
  70 + dataType: 'json',
  71 + success: function(data) {
  72 + console.log(data);
  73 + if (data.code == 1) {
  74 + mask(data.msg);
  75 + window.location.href = '/';
  76 + } else {
  77 + mask(data.msg);
  78 + }
76 } 79 }
77 - } 80 + });
78 }); 81 });
79 - }); 82 + }
80 }); 83 });
81 </script> 84 </script>
@@ -68,6 +68,10 @@ @@ -68,6 +68,10 @@
68 <script src="__TMPL__/public/assets/js/uploadfile.js"></script> 68 <script src="__TMPL__/public/assets/js/uploadfile.js"></script>
69 <script> 69 <script>
70 $(function() { 70 $(function() {
  71 + var source = $('#source').val();
  72 + if(source != '本站'){
  73 + $("#personal-photo").attr('disabled','disabled');
  74 + }
71 $("#personal-photo").on("change", function() { 75 $("#personal-photo").on("change", function() {
72 var img_size = $("input[type=file]").get(0).files[0].size; 76 var img_size = $("input[type=file]").get(0).files[0].size;
73 if (img_size > 1024000) { 77 if (img_size > 1024000) {
@@ -36,9 +36,13 @@ @@ -36,9 +36,13 @@
36 <div class="WeChat"> 36 <div class="WeChat">
37 <a href=""><img src="__TMPL__/public/assets/starImg/aicon_89.png" alt=""></a> 37 <a href=""><img src="__TMPL__/public/assets/starImg/aicon_89.png" alt=""></a>
38 </div> 38 </div>
39 - <div class="Sina">  
40 - <a href=""><img src="__TMPL__/public/assets/starImg/aicon_90.png" alt=""></a>  
41 - </div> 39 + <form action="https://api.weibo.com/oauth2/authorize" method="POST" id="wb_form">
  40 + <div class="Sina" id="wb_login">
  41 + <img src="__TMPL__/public/assets/starImg/aicon_90.png" alt="">
  42 + </div>
  43 + <input type="hidden" name="client_id" value="737607150">
  44 + <input type="hidden" name="redirect_uri" value="http://www.xingqiu.cn/portal/login/wb_login">
  45 + </form>
42 </div> 46 </div>
43 </div> 47 </div>
44 </div> 48 </div>
@@ -49,5 +53,6 @@ @@ -49,5 +53,6 @@
49 Copyright © 2004-2019 独角星球 版权所有&nbsp;&nbsp;&nbsp;京ICP备17035112号-2 53 Copyright © 2004-2019 独角星球 版权所有&nbsp;&nbsp;&nbsp;京ICP备17035112号-2
50 </div> 54 </div>
51 </body> 55 </body>
52 - 56 +<script src="__TMPL__/public/assets/js/jquery-3.2.1.min.js"></script>
  57 +<script src="__TMPL__/public/assets/js/public.js"></script>
53 </html> 58 </html>
@@ -109,6 +109,12 @@ $(function() { @@ -109,6 +109,12 @@ $(function() {
109 $('.home_nav_act').children('a').addClass('home_nav_act'); 109 $('.home_nav_act').children('a').addClass('home_nav_act');
110 } 110 }
111 111
  112 + var maxwidth = 4;//设置最多显示的字数
  113 + var user_nickname = $('#user_nickname').text();
  114 + if(user_nickname.length > maxwidth){
  115 + var user = user_nickname.substring(0,maxwidth);
  116 + $('#user_nickname').text(user+'...');
  117 + }
112 //改变分页图标 118 //改变分页图标
113 $('.page-item').eq(0).children().text('<'); 119 $('.page-item').eq(0).children().text('<');
114 $('.page-item:last').children().text('>'); 120 $('.page-item:last').children().text('>');
@@ -190,6 +196,11 @@ $(function() { @@ -190,6 +196,11 @@ $(function() {
190 } 196 }
191 window.open('http://v.t.sina.com.cn/share/share.php?' + temp.join('&')); 197 window.open('http://v.t.sina.com.cn/share/share.php?' + temp.join('&'));
192 }); 198 });
  199 +
  200 + //微博登录
  201 + $('#wb_login').click(function(){
  202 + $('#wb_form').submit();
  203 + });
193 }); 204 });
194 205
195 //点赞收藏 206 //点赞收藏
@@ -45,13 +45,14 @@ @@ -45,13 +45,14 @@
45 </form> 45 </form>
46 <notempty name="user"> 46 <notempty name="user">
47 <section class="home_login login_person"> 47 <section class="home_login login_person">
  48 + <input type="hidden" value="{$user.source}" id="source">
48 <notempty name="user.avatar"> 49 <notempty name="user.avatar">
49 <img src="{:cmf_get_image_url($user.avatar)}" alt=""> 50 <img src="{:cmf_get_image_url($user.avatar)}" alt="">
50 <else/> 51 <else/>
51 <img src="__TMPL__/public/assets/starImg/aicon_82.png" alt=""> 52 <img src="__TMPL__/public/assets/starImg/aicon_82.png" alt="">
52 </notempty> 53 </notempty>
53 <notempty name="user.user_nickname"> 54 <notempty name="user.user_nickname">
54 - <span>{$user.user_nickname}</span> 55 + <span id="user_nickname">{$user.user_nickname}</span>
55 <else/> 56 <else/>
56 <span></span> 57 <span></span>
57 </notempty> 58 </notempty>
@@ -59,26 +59,29 @@ @@ -59,26 +59,29 @@
59 <script src="__TMPL__/public/assets/js/public.js"></script> 59 <script src="__TMPL__/public/assets/js/public.js"></script>
60 <script> 60 <script>
61 $(function(){ 61 $(function(){
62 - $('.save_info').click(function(){  
63 - var nickname = $.trim($('#nickname').val());  
64 - $.ajax({  
65 - type: 'POST',  
66 - url: "/portal/login/updateNickname",  
67 - data: {  
68 - 'nickname': nickname,  
69 - },  
70 - dataType: 'json',  
71 - success: function (data) {  
72 - console.log(data);  
73 - if (data.code == 1) {  
74 - mask(data.msg);  
75 - window.location.href = '/';  
76 - } else {  
77 - mask(data.msg); 62 + var source = $('#source').val();
  63 + if(source == '本站'){
  64 + $('.save_info').click(function(){
  65 + var nickname = $.trim($('#nickname').val());
  66 + $.ajax({
  67 + type: 'POST',
  68 + url: "/portal/login/updateNickname",
  69 + data: {
  70 + 'nickname': nickname,
  71 + },
  72 + dataType: 'json',
  73 + success: function (data) {
  74 + console.log(data);
  75 + if (data.code == 1) {
  76 + mask(data.msg);
  77 + window.location.href = '/';
  78 + } else {
  79 + mask(data.msg);
  80 + }
78 } 81 }
79 - } 82 + });
80 }); 83 });
81 - }); 84 + }
82 }); 85 });
83 </script> 86 </script>
84 87
@@ -81,6 +81,10 @@ @@ -81,6 +81,10 @@
81 <script src="__TMPL__/public/assets/js/uploadfile.js"></script> 81 <script src="__TMPL__/public/assets/js/uploadfile.js"></script>
82 <script> 82 <script>
83 $(function(){ 83 $(function(){
  84 + var source = $('#source').val();
  85 + if(source != '本站'){
  86 + $("#personal-photo").attr('disabled','disabled');
  87 + }
84 $("#personal-photo").on("change",function(){ 88 $("#personal-photo").on("change",function(){
85 var img_size = $("input[type=file]").get(0).files[0].size; 89 var img_size = $("input[type=file]").get(0).files[0].size;
86 if (img_size > 1024000){ 90 if (img_size > 1024000){
@@ -51,14 +51,20 @@ @@ -51,14 +51,20 @@
51 微信登录 51 微信登录
52 </div> 52 </div>
53 </div> 53 </div>
54 - <div class="index_tenth_second_one">  
55 - <div class="index_tenth_second_img">  
56 - <img src="__TMPL__/public/assets/images/cicon_72@2x.png" alt="" />  
57 - </div>  
58 - <div class="index_tenth_second_title">  
59 - 微博登录 54 +
  55 + <form action="https://api.weibo.com/oauth2/authorize" method="POST" id="wb_form">
  56 + <div class="index_tenth_second_one">
  57 + <div class="index_tenth_second_img" id="wb_login">
  58 + <img src="__TMPL__/public/assets/images/cicon_72@2x.png" alt="" />
  59 + </div>
  60 + <div class="index_tenth_second_title">
  61 + 微博登录
  62 + </div>
  63 + <input type="hidden" name="client_id" value="737607150">
  64 + <input type="hidden" name="redirect_uri" value="http://www.xingqiu.cn/portal/login/wb_login">
60 </div> 65 </div>
61 - </div> 66 + </form>
  67 +
62 </div> 68 </div>
63 </div> 69 </div>
64 </div> 70 </div>
@@ -117,6 +117,14 @@ $(function() { @@ -117,6 +117,14 @@ $(function() {
117 } else { 117 } else {
118 $('.show_main_more').show(); 118 $('.show_main_more').show();
119 } 119 }
  120 +
  121 + var maxwidth = 4;//设置最多显示的字数
  122 + var user_nickname = $('#user_nickname').text();
  123 + if(user_nickname.length > maxwidth){
  124 + var user = user_nickname.substring(0,maxwidth);
  125 + $('#user_nickname').text(user+'...');
  126 + }
  127 +
120 //改变分页图标 128 //改变分页图标
121 $('.page-item').eq(0).children().text('<'); 129 $('.page-item').eq(0).children().text('<');
122 $('.page-item:last').children().text('>'); 130 $('.page-item:last').children().text('>');
@@ -224,6 +232,11 @@ $(function() { @@ -224,6 +232,11 @@ $(function() {
224 window.open('http://v.t.sina.com.cn/share/share.php?' + temp.join('&')); 232 window.open('http://v.t.sina.com.cn/share/share.php?' + temp.join('&'));
225 }); 233 });
226 234
  235 + //微博登录
  236 + $('#wb_login').click(function(){
  237 + $('#wb_form').submit();
  238 + });
  239 +
227 //分享到微信 240 //分享到微信
228 // $('.weixin').click(function(){ 241 // $('.weixin').click(function(){
229 // var target_url = "http://qr.liantu.com/api.php?text="+share_url; 242 // var target_url = "http://qr.liantu.com/api.php?text="+share_url;
@@ -23,6 +23,7 @@ @@ -23,6 +23,7 @@
23 <notempty name="user"> 23 <notempty name="user">
24 <!--登陆后--> 24 <!--登陆后-->
25 <div class="nav_downBtn login_person"> 25 <div class="nav_downBtn login_person">
  26 + <input type="hidden" value="{$user.source}" id="source">
26 <notempty name="user.avatar"> 27 <notempty name="user.avatar">
27 <div class="nav_downImg"> 28 <div class="nav_downImg">
28 <img src="{:cmf_get_image_url($user.avatar)}" alt="" /> 29 <img src="{:cmf_get_image_url($user.avatar)}" alt="" />
@@ -34,7 +35,7 @@ @@ -34,7 +35,7 @@
34 </notempty> 35 </notempty>
35 36
36 <notempty name="user.user_nickname"> 37 <notempty name="user.user_nickname">
37 - <p class="nav_downName">{$user.user_nickname}</p> 38 + <p class="nav_downName" id="user_nickname">{$user.user_nickname}</p>
38 <else/> 39 <else/>
39 <p class="nav_downName"></p> 40 <p class="nav_downName"></p>
40 </notempty> 41 </notempty>
@@ -95,5 +96,6 @@ @@ -95,5 +96,6 @@
95 <!--<img class="nav_downImg2" src="__TMPL__/public/assets/images/down.png" alt="" />--> 96 <!--<img class="nav_downImg2" src="__TMPL__/public/assets/images/down.png" alt="" />-->
96 </div> 97 </div>
97 </div> 98 </div>
  99 +
98 </div> 100 </div>
99 </div> 101 </div>