TinkPHP 自定义API基控制器类
来源:互联网 发布:程序员算法是什么 编辑:程序博客网 时间:2024/06/11 09:42
经过几次重构后,得到的精华版本,主要实现了API的token验证,如果接口带了token,则去身份认证,没有带token分两种,一是必须验证,二是无需验证。说这么多是不是有点晕,直接上代码吧!
<?phpnamespace common\controller;use think\Cache;use think\Controller;use think\Request;use tools\HttpClient;class ApiBaseController extends Controller{ protected $accessToken; protected $user; protected $userId; protected $loginAuth; protected function _initialize() { $this->accessToken = Request::instance()->header('access-token'); if($this->accessToken){ //有token时授权 if(strlen($this->accessToken)<24){ api(102,"access-token无效!"); } $this->authenticate(); }else{ //无token时判断授权 if($this->loginAuth){ $this->beforeActionList['loginAuth'] = $this->loginAuth; //定义是否登录 }else{ $this->beforeActionList['loginAuth'] = null;//没有定义时,默认需要登录 } } } /** * 登录 * @param $user * @return bool */ protected function doLogin($user){ $this->accessToken = $user['access_token']; $this->user = $user; $this->userId = $user['yunsu_id']; $rt = Cache::set($this->accessToken,$user,10*24*60*60);//token有效期两个小时 调试时间为10天有效 if(!$rt)die($rt); return true; } /** * 登录权限 */ protected function loginAuth(){ if($this->accessToken==null){ api(101,"该接口需要登录权限!"); } } /** * 鉴定身份 */ protected function authenticate(){ $loginUser = Cache::get($this->accessToken); //当本系统无登录时,去总用户系统认证(实现单点登录) if($loginUser==null){ $params = config('thirdaccount.users_sys'); $loginUser = $this->requestUserServer("/api/user/identityAuth",$params,false); $this->doLogin($loginUser); } if($loginUser){ $this->user = $loginUser; $this->userId = $loginUser['yunsu_id']; } } /** * 重写 表单验证 */ protected function validate($data, $validate, $message = [], $batch = false, $callback = null) { $result = parent::validate($data, $validate, $message, $batch, $callback); // TODO: Change the autogenerated stub if (true !== $result) { error($result); } } /** * 请求用户系统 * @param $path 服务器资源路径 例如:/api/user/login * @param null $params 参数 * @param bool $isVerify 是否严格验证 * @return mixed */ protected function requestUserServer($path,$params=null,$isVerify=true){ $url = $userHost.$path; if($this->accessToken){ if(strpos($url,'?')){ $url = $url."&access-token=".$this->accessToken; }else{ $url = $url."?access-token=".$this->accessToken; } } if($params){ $rt = HttpClient::post($url,$params); }else{ $rt = HttpClient::get($url); } $info = json_decode($rt,true); if($info){ if($info['code']==100){ return $info['data']; }else{ if($isVerify){ api($info['code'],"From Users System. ".$info['msg'],$info['data']); } } } error("用户系统异常!".$url); }}
阅读全文
1 0
- TinkPHP 自定义API基控制器类
- TinkPHP 自定义API基控制器类
- ThinkPHP5.0 控制器继承基类和自定义类
- ThinkPHP5.0 控制器继承基类和自定义类
- 自定义Cell(完成设置界面)并且自定义一个基类控制器(ZZSettingViewController)(自定义Cell4⃣️)
- tinkphp $_auto
- UITabBar自定义方式二(容器类控制器)
- Yii2 利用controllerMap自定义控制器类
- 视图控制器切换API
- 控制器基类
- 基类控制器(BaseViewController)
- 自定义UINavigationController(导航控制器)
- 视频播放自定义控制器
- 导航控制器push自定义
- 自定义视图控制器容器
- 自定义tabBar控制器
- 自定义导航控制器
- UI_自定义标签控制器
- CSS字体垂直、水平居中
- Shell 正则表达式
- Android模拟器Genymotion使用详解
- 网络服务器select
- Codeforces 588B Duff in Love【暴力】水题
- TinkPHP 自定义API基控制器类
- vue.js,基础用法实例
- Sublime 个人配置+Python环境
- Android 后台运行那些事
- 进程同步3 补充:数据的加锁操作
- libc++abi.dylib`__cxa_throw: 使用[AVAudioPlayer play]会产生__cxa_throw异常
- 信息安全(ssl、数字证书、数字签名、加密算法)
- iOS 应用的签名原理是什么?
- android别人的面试题汇总(不断更新中....)