123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use Firebase\JWT\JWT;
- use think\Controller;
- use think\Db;
- use think\Exception;
- use think\exception\HttpResponseException;
- use think\Request;
- use think\Response;
- use think\cache\driver\Redis;
- class Base extends Controller
- {
- protected $uid;
- protected $is_test = false;
- protected $page;
- protected $page_num;
- protected $off_set;
- public function initialize(){
- $this->page = input('page',1);
- $this->page_num = input('page_num',20);
- $this->off_set = $this->page * $this->page_num - $this->page_num;
- $this->is_test = input('test',0);
- $user_agent = app()->request->header('user-agent');
- if($user_agent){
- $agent_arr = explode(';',$user_agent);
- if(isset($agent_arr[1])){
- $check_version = explode('/',$agent_arr[1]);
- if(isset($check_version[0]) && $check_version[0] == 'app_version_code' && isset($check_version[1]) && $check_version[1] < 9) $this->error('请更新app',null,-1);
- }
- }
- }
-
- protected function check_login()
- {
-
- $authorization = app()->request->header('Authorization');
- if(empty($authorization) || $authorization == null){
- if($this->is_test == 1) {
- $this->uid = input('user_id') ? input('user_id') : 965;
- return true;
- }
- $this->error('Token不存在,拒绝访问',null,-1);
- }
- $key = md5(config('app.jwt'));
- try {
- $jwtAuth = json_encode(JWT::decode($authorization, $key, array('HS256')));
- $authInfo = json_decode($jwtAuth, true);
- if (!empty($authInfo['uid'])) {
- $member = Db::name('store_member')->field('status,lock_end')->where('id',$authInfo['uid'])->find();
- if(isset($member['lock_end']) && $member['lock_end'] > time()) $this->error('会员已锁定,'.date('Y-m-d H:i:s',$member['lock_end']).'解锁',null,-1);
-
- $redis = new Redis(['select'=>2]);
- $redis_key = 'request_limit_'.$authInfo['uid'];
- $limit_num = $redis->get($redis_key);
- if (!$limit_num){
- $redis->setex($redis_key, 10, 1);
- }else{
- if ($limit_num > 90){
- Db::name('store_member')->where('id',$authInfo['uid'])->update(['lock_st'=>time(),'lock_end'=>time()+7200]);
- $this->error('请求过快');
- }else{
- $ttl = $redis->ttl($redis_key);
- if($ttl < 0 && $ttl == '-1'){
- $redis->del($redis_key);
- } else if($ttl > 0){
- $redis->Incr($redis_key);
- }
- }
- }
-
- if($member['status']){
- $this->uid = $authInfo['uid'];
- return $this->uid;
- }else{
- $this->error('该会员已被禁用',null,-1);
- }
- } else {
- $this->error('Token验证不通过,用户不存在',null,-1);
- }
- } catch (\Firebase\JWT\SignatureInvalidException $e) {
- $this->error('Token无效',null,-1);
- } catch (\Firebase\JWT\ExpiredException $e) {
- $this->error('Token过期',null,-1);
- } catch (Exception $e) {
- return $e;
- }
- }
- protected function set_uid(){
- $authorization = app()->request->header('Authorization');
- $key = md5(config('app.jwt'));
- if(empty($authorization) || $authorization == null){
- return false;
- }
- $jwtAuth = json_encode(JWT::decode($authorization, $key, array('HS256')));
- $authInfo = json_decode($jwtAuth, true);
- if (!empty($authInfo['uid'])) {
- $member = Db::name('store_member')->field('status')->where('id',$authInfo['uid'])->find();
- if($member['status']){
- $this->uid = $authInfo['uid'];
- return $this->uid;
- }
- }
- }
-
- protected function success($msg = '', $data = null , $is_login = 1, $code = 1, $type = null, array $header = [])
- {
- $this->results($msg, $data, $is_login, $code, $type, $header);
- }
-
- protected function error($msg = '', $data = null, $is_login = 1, $code = 0, $type = null, array $header = [])
- {
- if(empty($this->uid) && $is_login != -1 ){
- $is_login = 0;
- }
- $this->results($msg, $data, $is_login, $code, $type, $header);
- }
-
- protected function results($msg, $data = null, $is_login, $code = 0, $type = null, array $header = [])
- {
- $result = [
- 'code' => $code,
- 'is_login' => $is_login,
- 'msg' => $msg,
- 'time' => \think\facade\Request::instance()->server('REQUEST_TIME'),
- 'data' => $data,
- ];
-
- $type = $type ? $type : 'json';
- if (isset($header['statuscode']))
- {
- $code = $header['statuscode'];
- unset($header['statuscode']);
- }
- else
- {
-
- $code = $code >= 1000 || $code < 200 ? 200 : $code;
- }
- $response = Response::create($result, $type, $code)->header($header);
- throw new HttpResponseException($response);
- }
- protected function get_uid(){
- $uid = 0;
- $authorization = app()->request->header('Authorization');
- if(!empty($authorization)){
- $key = md5(config('app.jwt'));
- $jwtAuth = json_encode(JWT::decode($authorization, $key, array('HS256')));
- $authInfo = json_decode($jwtAuth, true);
- if (!empty($authInfo['uid'])) {
- $uid = $authInfo['uid'];
- }
- }
- return $uid;
- }
- }
|