关于google登陆参看:Fecshop Facebook Google登录
在 http://fecshop.appfront.fancyecommerce.com/customer/account/login
页面可以看到
<img onclick="googlelogin()" src="//img.fancyecommerce.com/images/google.jpg">
<script type="text/javascript">
var newwindow;
var intId;
function googlelogin(){
var screenX = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft,
screenY = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth,
outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22),
width = 800,
height = 650,
left = parseInt(screenX + ((outerWidth - width) / 2), 10),
top = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
features = (
'width=' + width +
',height=' + height +
',left=' + left +
',top=' + top
);
newwindow=window.open('https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http%3A%2F%2Ffecshop.appfront.fancyecommerce.com%2Fcustomer%2Fgoogle%2Floginv&client_id=380372364773-qdj1seag9bh2n0pgrhcv2r5uoc58ltp3.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&access_type=offline&approval_prompt=auto ','Login_by_facebook',features);
if (window.focus) {newwindow.focus()}
return false;
}
</script>
也就是点击后,弹出来一个框,跳转到google的登陆页面进行授权,可以看到传递的参数有授权成功后的url:
redirect_uri=http%3A%2F%2Ffecshop.appfront.fancyecommerce.com%2Fcustomer%2Fgoogle%2Floginv
也就是/customer/google/loginv
就是fecshop的
@fecshop/app/appfront/modules/Customer/controllers/GoogleController.php
/**
* google登录确认成功后,返回的url
* 通过下面,得到用户的email,first_name,last_name
* 然后登录。
* 由于阿里云是国内服务器,暂时还没有具体测试,这个需要
* 用国外的服务器才可以。因为需要服务器方面访问google的接口。国内服务器会被墙的。
*/
public function actionLoginv()
{
Yii::$service->session->set('logintype', 'google');
$thirdLogin = Yii::$service->store->thirdLogin;
global $googleapiinfo;
$googleapiinfo['GOOGLE_CLIENT_ID'] = isset($thirdLogin['google']['CLIENT_ID']) ? $thirdLogin['google']['CLIENT_ID'] : '';
$googleapiinfo['GOOGLE_CLIENT_SECRET'] = isset($thirdLogin['google']['CLIENT_SECRET']) ? $thirdLogin['google']['CLIENT_SECRET'] : '';
$lib_google_base = Yii::getAlias('@fecshop/lib/google');
include $lib_google_base.'/Social.php';
$urlKey = 'customer/google/loginv';
$redirectUrl = Yii::$service->url->getUrl($urlKey);
$Social_obj = new \Social($redirectUrl);
$user = $Social_obj->google();
// 服务器放到国外才行。不然上面无法返回数据。
if (is_array($user) && !empty($user)) {
$fullname = $user['name'];
$email = $user['email'];
if ($email) {
$this->accountLogin($fullname, $email);
}
}
}
在 /customer/google/loginv
部分,获取email 和user name,这里使用的是google的php sdk。
细节需要自己研究