thinkphp第一次用,做一个app项目。为了循序渐进学习兼顾项目,所以后来决定用thinkcmf【基于thinkphp3.2开发的一个后台管理系统】。
******************************************************2016年4月7日11:24:44*****************************************************************
url 重写规则:
http://serverName/模块/控制器/操作/[参数名/参数值...]
模块 application下,portal默认;控制器模块下controller内;
返回json格式数据:
$this->ajaxReturn(array("status"=>0,"info"=>"密码错误!"),'json');
日志记录,微信支付下接收回调通知写入日志,INFO级别。日志文件:data/runtime/Logs下
\Think\Log::write("【接收到的notify通知】:\n".$xml."\n",'INFO');
第三方登录:
自带微博、QQ登录,后台设置即可,回调url貌似得改文件config.php。
安卓、ios下服务器接口需调整。
***************thinkcmf 自带 第三方登录 源码解析******************************************
之前的是api/oauth/callback/type/[对应]/code/[CODE],与微博或者qq服务器交互的是服务器。
OauthController.class.php: 获得code继而加载ThinkOauth类并实例化一个对象,获得token;
}
调用 Lib/Extend/ThinkSDK/ThinkOauth.class.php 下 getAccessToken
之后就是调用TypeEvent.class.php 获得 info。
***************thinkcmf 自带 第三方源码解析 END******************************************
移动服务端 在 getAccessToken 时 step1 的工作由移动端完成,OauthController.class.php 新建移动端接口,get方式提交 weixin/sina/qq,post 提交 data,内容依照各自SDK所需。
新增微信第三方登录
依照qqSDK,config文件中添加,后台添加appkey的修改位,也可不添写死。
简单化 微信获取信息 https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
需要access_token和openid ,有移动端提交。 https://api.weixin.qq.com/ 设定为SDK文件中的$ApiBase
typeEvent.class.php中添加
总之,看懂既定写好的各文件间关系、追踪下函数调用,看懂即可。
2016年5月18日10:42:34 更新。
http://serverName/模块/控制器/操作/[参数名/参数值...]
模块 application下,portal默认;控制器模块下controller内;
返回json格式数据:
$this->ajaxReturn(array("status"=>0,"info"=>"密码错误!"),'json');
日志记录,微信支付下接收回调通知写入日志,INFO级别。日志文件:data/runtime/Logs下
\Think\Log::write("【接收到的notify通知】:\n".$xml."\n",'INFO');
第三方登录:
自带微博、QQ登录,后台设置即可,回调url貌似得改文件config.php。
安卓、ios下服务器接口需调整。
***************thinkcmf 自带 第三方登录 源码解析******************************************
之前的是api/oauth/callback/type/[对应]/code/[CODE],与微博或者qq服务器交互的是服务器。
OauthController.class.php: 获得code继而加载ThinkOauth类并实例化一个对象,获得token;
//授权回调地址
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);
}
}
调用 Lib/Extend/ThinkSDK/ThinkOauth.class.php 下 getAccessToken
/**
* 获取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都可。
之后就是调用TypeEvent.class.php 获得 info。
***************thinkcmf 自带 第三方源码解析 END******************************************
移动服务端 在 getAccessToken 时 step1 的工作由移动端完成,OauthController.class.php 新建移动端接口,get方式提交 weixin/sina/qq,post 提交 data,内容依照各自SDK所需。
//登录地址
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函数、数据格式、数据内容调整。
新增微信第三方登录
依照qqSDK,config文件中添加,后台添加appkey的修改位,也可不添写死。
简单化 微信获取信息 https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
需要access_token和openid ,有移动端提交。 https://api.weixin.qq.com/ 设定为SDK文件中的$ApiBase
typeEvent.class.php中添加
//登录成功,获取腾讯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']}");
}
}
总之,看懂既定写好的各文件间关系、追踪下函数调用,看懂即可。
2016年5月18日10:42:34 更新。