Skip to content

Commit 42aba41

Browse files
committed
更新QQ微波登录代码
1 parent dbe3038 commit 42aba41

File tree

6 files changed

+142
-50
lines changed

6 files changed

+142
-50
lines changed

server/apps/configs/oauth.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
<?php
22
$oauth['weibo']['appid'] = '1418646107';
33
$oauth['weibo']['skey'] = '8b1fed32df42548d71acac00dddb05bb';
4+
$oauth['weibo']['callback'] = WEBROOT.'/page/callback_weibo/';
5+
6+
$oauth['qq']['appid'] = '221403';
7+
$oauth['qq']['skey'] = 'f3f2490a725f75e154bf2a37773213b8';
8+
$oauth['qq']['callback'] = WEBROOT.'/page/callback_qq/';
9+
$oauth['qq']['scope'] = 'get_user_info';
10+
411
return $oauth;

server/apps/controllers/Page.php

Lines changed: 123 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
namespace App\Controller;
33
use App;
44
use Swoole;
5+
use ZenAPI\QqClient;
6+
use ZenAPI\QqOAuth2;
57

6-
require_once APPSPATH.'/classes/WeiboOAuth.php';
7-
require_once APPSPATH.'/classes/qqoauth.func.php';
8+
require_once APPSPATH.'/include/libweibo/saetv2.ex.class.php';
89

910
class Page extends App\FrontPage
1011
{
@@ -23,62 +24,119 @@ function verify()
2324
return $verifyCode['image'];
2425
}
2526

26-
function oauth()
27-
{
27+
function callback_weibo()
28+
{
2829
session();
29-
if (empty($_GET['s']) or $_GET['s'] == 'sina')
30+
if (empty($_GET['code']))
3031
{
31-
$conf = $this->config['oauth']['weibo'];
32-
$oauth = new \WeiboOAuth($conf['appid'], $conf['skey']);
33-
$keys = $oauth->getRequestToken();
34-
$_SESSION['oauth_keys'] = $keys;
35-
$_SESSION['oauth_serv'] = 'sina';
36-
$login_url = $oauth->getAuthorizeURL($keys['oauth_token'], false, $conf['callback']);
37-
$this->swoole->http->redirect($login_url);
32+
$this->http->redirect('/page/login/');
33+
return;
3834
}
39-
else
35+
36+
$conf = $this->config['oauth']['weibo'];
37+
$oauth = new \SaeTOAuthV2($conf['appid'], $conf['skey']);
38+
$keys['code'] = $_GET['code'];
39+
$keys['redirect_uri'] = $conf['callback'];
40+
41+
$token = $oauth->getAccessToken('code', $keys);
42+
if ($token)
4043
{
41-
return "不支持等OAuth类型。";
44+
$_SESSION['weibo_token'] = $token;
45+
$client = new \SaeTClientV2($conf['appid'], $conf['skey'], $token['access_token']);
46+
$uid = $client->get_uid();
47+
$userinfo = $client->show_user_by_id($uid['uid']);
48+
if (!isset($userinfo['id']))
49+
{
50+
return "请求错误.";
51+
}
52+
$model = createModel('UserInfo');
53+
$username = 'sina_' . $userinfo['id'];
54+
$u = $model->get($username, 'username');
55+
//不存在,则插入数据库
56+
if (!$u->exist())
57+
{
58+
$u['username'] = $username;
59+
$u['nickname'] = $userinfo['name'];
60+
$u['avatar'] = $userinfo['avatar_large'];
61+
$u['blog'] = $userinfo['url'];
62+
list($u['province'], $u['city']) = explode(' ', $userinfo['location']);
63+
//插入到表中
64+
$u['id'] = $model->put($u);
65+
}
66+
else
67+
{
68+
$u->nickname = $userinfo['name'];
69+
$u->avatar = $userinfo['avatar_large'];
70+
$u->blog = $userinfo['url'];
71+
$u->save();
72+
}
73+
//写入SESSION
74+
$_SESSION['isLogin'] = 1;
75+
$_SESSION['user_id'] = $u['id'];
76+
$_SESSION['user'] = $u;
77+
$this->setLoginStat();
78+
$this->http->redirect(WEBROOT."/person/index/");
4279
}
43-
}
80+
}
4481

45-
function oauth_callback()
46-
{
82+
function callback_qq()
83+
{
4784
session();
48-
if ($_SESSION['oauth_serv'] == 'sina')
85+
if (empty($_GET['code']))
4986
{
50-
$conf = $this->config['oauth']['weibo'];
51-
$oauth = new \WeiboOAuth($conf['appid'], $conf['skey'], $_SESSION['oauth_keys']['oauth_token'], $_SESSION['oauth_keys']['oauth_token_secret']);
52-
$_SESSION['last_key'] = $oauth->getAccessToken($_REQUEST['oauth_verifier']);
87+
$this->http->redirect('/page/login/');
88+
return;
89+
}
5390

54-
$client = new \WeiboClient($conf['appid'], $conf['skey'], $_SESSION['last_key']['oauth_token'],
55-
$_SESSION['last_key']['oauth_token_secret']);
56-
$userinfo = $client->verify_credentials();
57-
if (!isset($userinfo['id']))
91+
$conf = $this->config['oauth']['qq'];
92+
Swoole\Loader::addNameSpace('ZenAPI', APPSPATH . '/include/zenapi');
93+
$oauth = new QqOAuth2($conf['appid'], $conf['skey']);
94+
$keys['code'] = $_GET['code'];
95+
$keys['redirect_uri'] = $conf['callback'];
96+
97+
$token = $oauth->getAccessToken('code', $keys);
98+
if ($token)
99+
{
100+
$openid = $oauth->getOpenid($token['access_token']);
101+
$_SESSION['qq_token'] = $token;
102+
$client = new QqClient($token['access_token'], $conf['appid'], $openid['openid']);
103+
$userinfo = $client->get('user/get_user_info');
104+
if (!isset($userinfo['ret']) and $userinfo['ret'] != 0)
58105
{
59-
return "请求错误:" . var_export($userinfo, $conf, $_SESSION, true);
106+
return "请求错误. 错误码:{$userinfo['ret']}\n";
60107
}
61-
$model = createModel('UserInfo');
62-
$username = 'sina_'.$userinfo['id'];
63-
$u = $model->get($username,'username')->get();
64-
//不存在,则插入数据库
65-
if(empty($u))
66-
{
67-
$u['username'] = $username;
68-
$u['nickname'] = $userinfo['name'];
69-
$u['avatar'] = $userinfo['profile_image_url'];
70-
list($u['province'],$u['city']) = explode(' ',$userinfo['location']);
71-
//插入到表中
72-
$u['id'] = $model->put($u);
73-
}
74-
//写入SESSION
75-
$_SESSION['isLogin'] = 1;
76-
$_SESSION['user_id'] = $u['id'];
77-
$_SESSION['user'] = $u;
78-
$this->setLoginStat();
108+
$model = createModel('UserInfo');
109+
$username = $openid['openid'];
110+
$u = $model->get($username, 'username');
111+
//不存在,则插入数据库
112+
if (!$u->exist())
113+
{
114+
$u['username'] = $username;
115+
$u['nickname'] = $userinfo['nickname'];
116+
$u['avatar'] = $userinfo['figureurl_2'];
117+
$u['birth_year'] = $userinfo['year'];
118+
$u['province'] = $userinfo['province'];
119+
$u['city'] = $userinfo['city'];
120+
$u['sex'] = $userinfo['gender'] == '' ? 1 : 2;
121+
//插入到表中
122+
$u['id'] = $model->put($u);
123+
}
124+
else
125+
{
126+
$u->nickname = $userinfo['nickname'];
127+
$u->avatar = $userinfo['figureurl_2'];
128+
$u->province = $userinfo['province'];
129+
$u->city = $userinfo['city'];
130+
$u->save();
131+
}
132+
//写入SESSION
133+
$_SESSION['isLogin'] = 1;
134+
$_SESSION['user_id'] = $u->_current_id;
135+
$_SESSION['user'] = $u;
136+
$this->setLoginStat();
79137
$this->http->redirect(WEBROOT."/person/index/");
80138
}
81-
}
139+
}
82140

83141
function flist()
84142
{
@@ -233,12 +291,29 @@ function login()
233291
}
234292
else
235293
{
236-
$this->swoole->tpl->display();
294+
$conf = $this->config['oauth']['weibo'];
295+
$weibo_oauth = new \SaeTOAuthV2($conf['appid'], $conf['skey']);
296+
$weibo_login_url = $weibo_oauth->getAuthorizeURL($conf['callback']);
297+
298+
Swoole\Loader::addNameSpace('ZenAPI', APPSPATH . '/include/zenapi');
299+
$conf = $this->config['oauth']['qq'];
300+
$qq_oauth = new QqOAuth2($conf['appid'], $conf['skey']);
301+
$qq_login_url = $qq_oauth->getAuthorizeURL(array(
302+
'client_id' => $conf['appid'],
303+
'redirect_uri' => $conf['callback'],
304+
'response_type' => 'code',
305+
'display' => null,
306+
'scope' => $conf['scope'],
307+
));
308+
$this->tpl->assign('weibo_login_url', $weibo_login_url);
309+
$this->tpl->assign('qq_login_url', $qq_login_url);
310+
$this->tpl->display();
237311
}
238312
}
239313

240314
function logout()
241315
{
316+
$this->http->setcookie('uname', '', null, '/', 'swoole.com');
242317
$this->user->logout();
243318
$this->swoole->http->redirect('/page/login/');
244319
}
@@ -451,7 +526,7 @@ function user()
451526
private function setLoginStat()
452527
{
453528
$tm = time();
454-
Swoole\Cookie::set('uname', $_SESSION['user']['nickname'], $tm+86400*30,'/');
455-
Swoole\Cookie::set('uid', $_SESSION['user_id'], $tm+86400*30,'/');
529+
Swoole\Cookie::set('uname', $_SESSION['user']['nickname'], $tm + 86400 * 30, '/');
530+
Swoole\Cookie::set('uid', $_SESSION['user_id'], $tm + 86400 * 30, '/');
456531
}
457532
}

server/apps/templates/page_login.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ <h2>会员登录</h2>
7575
<tr>
7676
<td height="50"></td>
7777
<td height="50" colspan="2">
78-
<a class="snssina" title="新浪微博帐号登录" href="/page/oauth/?s=sina" height="24"></a>
79-
</td>
78+
<a title="新浪微博帐号登录" href="{{$qq_login_url}}">
79+
<img src="/static/image/qq_login.png" height="35" alt=""/>
80+
</a>
81+
<a title="新浪微博帐号登录" href="{{$weibo_login_url}}">
82+
<img src="/static/image/weibo_login.png" height="35" alt=""/>
83+
</a>
84+
</td>
8085
</tr>
8186
</table>
8287
</div>

web/static/image/qq_login.png

9.89 KB
Loading

web/static/image/weibo_login.png

15.8 KB
Loading

web/static/js/deep-diff-0.3.4.min.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)