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

微信登录

@@ -21,7 +21,7 @@ class LoginController extends HomeBaseController @@ -21,7 +21,7 @@ class LoginController extends HomeBaseController
21 private $limit = 8;//收藏,搜索分页 21 private $limit = 8;//收藏,搜索分页
22 private $appkey = 737607150;//微博appkey 22 private $appkey = 737607150;//微博appkey
23 private $appsecret = 'd80b43a1e74e8ba095590b36a3459480';//微博appsecret 23 private $appsecret = 'd80b43a1e74e8ba095590b36a3459480';//微博appsecret
24 - private $redirect_uri = 'http://www.starplanet.cn/portal/login/wb_login';//回调地址 24 + private $redirect_uri = 'http://www.xingqiu.cn/portal/login/wb_login';//回调地址
25 25
26 private $appkey1 = 'wx9cfa880272f186bf';//微信appkey 26 private $appkey1 = 'wx9cfa880272f186bf';//微信appkey
27 private $appsecret1 = '11b643393b1e54d6ef5eaa984ba4e545';//微信appsecret 27 private $appsecret1 = '11b643393b1e54d6ef5eaa984ba4e545';//微信appsecret
@@ -352,19 +352,44 @@ class LoginController extends HomeBaseController @@ -352,19 +352,44 @@ class LoginController extends HomeBaseController
352 //第三方微信登录 352 //第三方微信登录
353 public function wx_login(){ 353 public function wx_login(){
354 $code = $this->request->get('code'); 354 $code = $this->request->get('code');
355 - $code = '021HoCq91Re1wP1g5Eo91Vgnq91HoCqd';  
356 $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appkey1.'&secret='.$this->appsecret1.'&code='.$code.'&grant_type=authorization_code'; 355 $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appkey1.'&secret='.$this->appsecret1.'&code='.$code.'&grant_type=authorization_code';
357 $res = $this->http_get($url); 356 $res = $this->http_get($url);
358 $json_arr = json_decode($res,true); 357 $json_arr = json_decode($res,true);
359 -// $token = $json_arr['access_token'];  
360 - $token = '22_LFufsKgmxT1jCtIuDJ7CLLwAOI7mOPlmNYHRHECuScIwrNZ3-Bmxj8pOjr2NZs7qdzH7iBkPddFtBdyVoBd7zas_gkQhDw3YJgm5iYa6d5w';  
361 -// $openid = $json_arr['openid'];  
362 - $openid = 'oYOYl5hbULoKimG5R8Uk-Paha0d8'; 358 +
  359 + if(!isset($json_arr['access_token'])&&empty($json_arr['access_token'])){
  360 + //用户取消登录
  361 + $this->redirect('/portal/login/thirdLogin');
  362 + }
  363 + $token = $json_arr['access_token'];
  364 + $openid = $json_arr['openid'];
363 //通过access_token获取用户信息 365 //通过access_token获取用户信息
364 $url1 = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid; 366 $url1 = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid;
365 $res1 = $this->http_get($url1); 367 $res1 = $this->http_get($url1);
366 - $json_arr1 = json_decode($res1,true);  
367 - var_dump($json_arr1);exit; 368 + $info = json_decode($res1,true);
  369 + //查询该微信用户是否存在
  370 + $where = ['wb_id'=>$info['openid'],'source'=>'微信'];
  371 + $user = $this->findThird($where);
  372 + //获取微博id,昵称,头像
  373 + $userModel = new UserModel();
  374 + $ip = get_client_ip();
  375 + $users['last_login_time'] = time();
  376 + $users['last_login_ip'] = $ip;
  377 + if($user){
  378 + $users['user_nickname'] = $info['nickname'];
  379 + $users['avatar'] = $info['headimgurl'];
  380 + $userModel->where(['wb_id'=>$info['openid'],'source'=>'微信'])->update($users);
  381 + }else{
  382 + $users['wb_id'] = $info['openid'];
  383 + $users['user_nickname'] = $info['nickname'];
  384 + $users['avatar'] = $info['headimgurl'];
  385 + $users['source'] = '微信';
  386 + $users['user_type'] = 2;
  387 + $users['create_time'] = time();
  388 + $userModel->create($users);
  389 + }
  390 + $userInfo = $this->findThird($where);
  391 + cmf_update_current_user($userInfo);
  392 + $this->redirect('/');
368 } 393 }
369 394
370 //第三方微博登录 395 //第三方微博登录
@@ -387,15 +412,6 @@ class LoginController extends HomeBaseController @@ -387,15 +412,6 @@ class LoginController extends HomeBaseController
387 $this->redirect('/portal/login/thirdLogin'); 412 $this->redirect('/portal/login/thirdLogin');
388 } 413 }
389 $token = $json_arr['access_token']; 414 $token = $json_arr['access_token'];
390 -  
391 - $url = 'https://api.weibo.com/2/statuses/share.json';  
392 - $data = [  
393 - 'access_token' => $token,  
394 - 'status' => URLencode('http://www.starplanet.cn/portal/enjoy/getEnjoyDetail?id=52')  
395 - ];  
396 - $res = $this->http_post($url,$data);  
397 - $json_arr = json_decode($res,true);  
398 - var_dump($json_arr);exit;  
399 //存token到session 415 //存token到session
400 session('token', $token); 416 session('token', $token);
401 $uid = $json_arr['uid']; 417 $uid = $json_arr['uid'];
@@ -426,8 +442,7 @@ class LoginController extends HomeBaseController @@ -426,8 +442,7 @@ class LoginController extends HomeBaseController
426 } 442 }
427 $userInfo = $this->findThird($where); 443 $userInfo = $this->findThird($where);
428 cmf_update_current_user($userInfo); 444 cmf_update_current_user($userInfo);
429 - $this->wb_share();  
430 -// $this->redirect('/'); 445 + $this->redirect('/');
431 } 446 }
432 447
433 //微博分享 448 //微博分享
@@ -41,7 +41,7 @@ @@ -41,7 +41,7 @@
41 <img src="__TMPL__/public/assets/starImg/aicon_90.png" alt=""> 41 <img src="__TMPL__/public/assets/starImg/aicon_90.png" alt="">
42 </div> 42 </div>
43 <input type="hidden" name="client_id" value="737607150"> 43 <input type="hidden" name="client_id" value="737607150">
44 - <input type="hidden" name="redirect_uri" value="http://www.starplanet.cn/portal/login/wb_login"> 44 + <input type="hidden" name="redirect_uri" value="http://www.xingqiu.cn/portal/login/wb_login">
45 </form> 45 </form>
46 </div> 46 </div>
47 </div> 47 </div>
@@ -28,7 +28,7 @@ @@ -28,7 +28,7 @@
28 <p>168</p> 28 <p>168</p>
29 </div> 29 </div>
30 <input type="hidden" name="client_id" value="737607150"> 30 <input type="hidden" name="client_id" value="737607150">
31 - <input type="hidden" name="redirect_uri" value="http://www.starplanet.cn/portal/login/wb_login"> 31 + <input type="hidden" name="redirect_uri" value="http://www.xingqiu.cn/portal/login/wb_login">
32 <input type="hidden" name="flag" value="1"> 32 <input type="hidden" name="flag" value="1">
33 </form> 33 </form>
34 34
@@ -44,9 +44,11 @@ @@ -44,9 +44,11 @@
44 <div class="index_tenth_seconds"> 44 <div class="index_tenth_seconds">
45 <div class="index_tenth_second"> 45 <div class="index_tenth_second">
46 <div class="index_tenth_second_one"> 46 <div class="index_tenth_second_one">
  47 + <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">
47 <div class="index_tenth_second_img"> 48 <div class="index_tenth_second_img">
48 <img src="__TMPL__/public/assets/images/cicon_71@2x.png" alt="" /> 49 <img src="__TMPL__/public/assets/images/cicon_71@2x.png" alt="" />
49 </div> 50 </div>
  51 + </a>
50 <div class="index_tenth_second_title"> 52 <div class="index_tenth_second_title">
51 微信登录 53 微信登录
52 </div> 54 </div>