thinkphp第一次用,做一个app项目。为了循序渐进学习兼顾项目,所以后来决定用thinkcmf【基于thinkphp3.2开发的一个后台管理系统】。
******************************************************2016年4月7日11:24:44*****************************************************************
//授权回调地址
public function callback($type = null, $code = null){
//加载ThinkOauth类并实例化一个对象
import("ThinkOauth");
$sns = \ThinkOauth::getInstance($type);
<pre name="code" class="php"> $token = $sns->getAccessToken($code , $extend);
//部分内容省略<pre name="code" class="php"> $user_info = A('Type', 'Event')->$type($token);
if(!empty($_SESSION['oauth_bang'])){
$this->_bang_handle($user_info, $type, $token);
}else{
$this->_login_handle($user_info, $type, $token);
}
/**
* 获取access_token
* @param string $code 上一步请求到的code
*/
public function getAccessToken($code, $extend = null){
$this->config();
$params = array(
'client_id' => $this->AppKey,
'client_secret' => $this->AppSecret,
'grant_type' => $this->GrantType,
'code' => $code,
'redirect_uri' => $this->Callback,
);
$data = $this->http($this->GetAccessTokenURL, $params, 'POST');//step1
$this->Token = $this->parseToken($data, $extend);//step2
return $this->Token;
}
其中step1 和step2 表示 由上一步请求到的code获得返回数据保存成本地数据格式,parseToken 由各自的SDK 实现,各自需要的内容数据不同。比如qq只需要access_token 和expires_in sina需要access_token、expires_in、remind_in 、uid 其中remind_in貌似没了,改源码或者把expires_in 赋值给remind_in都可。
//登录地址
public function register($type){
$data = $_POST['data'];//var_dump($data);
$extend = I("post.extend");
//加载ThinkOauth类并实例化一个对象
import("ThinkOauth");
$sns = \ThinkOauth::getInstance($type);
$token = $sns->register_config($data,$extend);//
//下同
}
ThinkOauth.class.php 新建 函数 简化getAccessToken。 由 之前获得的data保存成本地数据格式
public function register_config($data,$extend = null){
$this->config();
$this->Token = $this->parseToken($data, $extend);
return $this->Token;
}
这样基本服务端 代码就OK了。剩下就是调试,各自SDK的call函数、数据格式、数据内容调整。
//登录成功,获取腾讯QQ用户信息
public function weixin($token){
//import("ORG.ThinkSDK.ThinkOauth");
$weixin = \ThinkOauth::getInstance('weixin', $token);
$data = $weixin->call('sns/userinfo');
if($data['ret'] == 0){
$userInfo['type'] = 'weixin';
$userInfo['name'] = $data['nickname'];
$userInfo['nick'] = $data['nickname'];
$userInfo['head'] = $data['headimgurl'];
return $userInfo;
} else {
throw_exception("获取微信用户信息失败:{$data['msg']}");
}
}