-
web端基本的三方登录逻辑官方文档都写了,没必要赘述。我就聊聊一些坑:
-
三方的回调域名只能填写一个,那么就需要判断是pc,还是手机访问。(大多数网站的m站和pc站是2个域名)
- 解决办法:用一个域名,然后程序里判断是手机访问还是电脑访问
- 工具代码
//如果检测到访问的浏览器为下列一个指定的移动浏览器 则返回true
function is_mobile()
{
$regex_match = "/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
$regex_match .= "htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
$regex_match .= "blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match .= "symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
$regex_match .= "jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
$regex_match .= ")/i";
return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}
- 使用代码
//回调成功,重定向到个人中心
$is_mobile = $this->is_mobile();
if ($is_mobile) {
$this->redirect("/");
} else {
$this->redirect("http://ucenter.xxxx.com/");
}
-
用QQ的三方登录举个例子:
-
pc的三方登录也是复用的下面的代码,但是不用做pc还是mobile的判断。拿到code,请求个人信息存到session中就ok了!
public function qqAction()
{
$code = $this->_request->getQuery('code');
$is_mobile = $this->is_mobile();
if ($is_mobile) {//手机端
Yaf_Loader::import('ApiLogin/QQ.php');
$qq = new QQ();
$token = $qq->getAccessToken($code);
$bind_id = $qq->getOpenId($token);
$dataSource = $qq->getUserInfo($token);
$userinfo = json_decode($dataSource, true);
$result = Service_User_InfoModel::checkIsFirst(TYPE_QQ, $bind_id);
if (empty($result)) {
$userInfo['real_name'] = $userinfo['nickname'];
$userInfo['avatar'] = $this->dealAvatar($userinfo['figureurl_qq_2']);
$userInfo['sex'] = $userinfo['gender'] == '男' ? 1 : 2;
$userInfo['token'] = $this->getToken();
$this->renderLogin($userInfo, TYPE_QQ, $bind_id);
} else {
$userbind = Service_User_InfoModel::getUserBind($result);
$this->_loginUserId = $userbind['user_id'];
}
$this->saveSession($this->_loginUserId);
} else {//pc端 这部分是核心
$this->redirect("http://ucenter.shiqc.com/?c=login&a=qq&code=" . $code);
}
}
一个敲代码,写文字的人,我在这里!

网友评论