123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?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;
- class Base extends Controller
- {
- protected $uid = 2;
- 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;
- }
-
- protected function check_login()
- {
- $authorization = app()->request->header('Authorization');
-
- if(empty($authorization) || $authorization == null){
- return true;
- $this->error('Token不存在,拒绝访问','',0);
- }
- $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')->where('id',$authInfo['uid'])->find();
- if($member['status']){
- $this->uid = $authInfo['uid'];
- return $this->uid;
- }else{
- $this->error('该会员已被禁用','',0);
- }
- } else {
- $this->error('Token验证不通过,用户不存在','',0);
- }
- } catch (\Firebase\JWT\SignatureInvalidException $e) {
- $this->error('Token无效','',0);
- } catch (\Firebase\JWT\ExpiredException $e) {
- $this->error('Token过期','',0);
- } catch (Exception $e) {
- return $e;
- }
- }
-
- 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 = 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;
- }
- }
|