Base.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\common\controller\Api;
  16. use Firebase\JWT\JWT;
  17. use think\Controller;
  18. use think\Db;
  19. use think\Exception;
  20. use think\exception\HttpResponseException;
  21. use think\Request;
  22. use think\Response;
  23. /**
  24. * 会员管理基类
  25. * Class Member
  26. * @package app\store\controller\api
  27. */
  28. class Base extends Controller
  29. {
  30. protected $uid;
  31. public function initialize()
  32. {
  33. }
  34. //校验jwt权限API
  35. protected function check_login()
  36. {
  37. $authorization = app()->request->header('Authorization');
  38. //var_dump($authorization.'111');exit();
  39. if(empty($authorization) || $authorization == null){
  40. $this->error('Token不存在,拒绝访问','',0);
  41. }
  42. $key = md5(config('app.jwt'));
  43. try {
  44. $jwtAuth = json_encode(JWT::decode($authorization, $key, array('HS256')));
  45. $authInfo = json_decode($jwtAuth, true);
  46. if (!empty($authInfo['uid'])) {
  47. $member = Db::name('store_member')->field('status')->where('id',$authInfo['uid'])->find();
  48. if($member['status']){
  49. $this->uid = $authInfo['uid'];
  50. return $this->uid;
  51. }else{
  52. $this->error('该会员已被禁用','',0);
  53. }
  54. } else {
  55. $this->error('Token验证不通过,用户不存在','',0);
  56. }
  57. } catch (\Firebase\JWT\SignatureInvalidException $e) {
  58. $this->error('Token无效','',0);
  59. } catch (\Firebase\JWT\ExpiredException $e) {
  60. $this->error('Token过期','',0);
  61. } catch (Exception $e) {
  62. return $e;
  63. }
  64. }
  65. /**
  66. * 操作成功返回的数据
  67. * @param string $msg 提示信息
  68. * @param mixed $data 要返回的数据
  69. * @param int $code 错误码,默认为1
  70. * @param string $type 输出类型
  71. * @param array $header 发送的 Header 信息
  72. */
  73. protected function success($msg = '', $data = null , $is_login = 1, $code = 1, $type = null, array $header = [])
  74. {
  75. $this->results($msg, $data, $is_login, $code, $type, $header);
  76. }
  77. /**
  78. * 操作失败返回的数据
  79. * @param string $msg 提示信息
  80. * @param mixed $data 要返回的数据
  81. * @param int $code 错误码,默认为0
  82. * @param string $type 输出类型
  83. * @param array $header 发送的 Header 信息
  84. */
  85. protected function error($msg = '', $data = null, $is_login = 1, $code = 0, $type = null, array $header = [])
  86. {
  87. if(empty($this->uid)){
  88. $is_login = 0;
  89. }
  90. $this->results($msg, $data, $is_login, $code, $type, $header);
  91. }
  92. /**
  93. * 返回封装后的 API 数据到客户端
  94. * @access protected
  95. * @param mixed $msg 提示信息
  96. * @param mixed $data 要返回的数据
  97. * @param int $code 错误码,默认为0
  98. * @param string $type 输出类型,支持json/xml/jsonp
  99. * @param array $header 发送的 Header 信息
  100. * @return void
  101. * @throws HttpResponseException
  102. */
  103. protected function results($msg, $data = null, $is_login, $code = 0, $type = null, array $header = [])
  104. {
  105. $result = [
  106. 'code' => $code,
  107. 'is_login' => $is_login,
  108. 'msg' => $msg,
  109. 'time' => \think\facade\Request::instance()->server('REQUEST_TIME'),
  110. 'data' => $data,
  111. ];
  112. // 如果未设置类型则自动判断
  113. $type = $type ? $type : 'json';
  114. if (isset($header['statuscode']))
  115. {
  116. $code = $header['statuscode'];
  117. unset($header['statuscode']);
  118. }
  119. else
  120. {
  121. //未设置状态码,根据code值判断
  122. $code = $code >= 1000 || $code < 200 ? 200 : $code;
  123. }
  124. $response = Response::create($result, $type, $code)->header($header);
  125. throw new HttpResponseException($response);
  126. }
  127. protected function get_uid(){
  128. $uid = 0;
  129. $authorization = app()->request->header('Authorization');
  130. if(!empty($authorization)){
  131. $key = md5(config('app.jwt'));
  132. $jwtAuth = json_encode(JWT::decode($authorization, $key, array('HS256')));
  133. $authInfo = json_decode($jwtAuth, true);
  134. if (!empty($authInfo['uid'])) {
  135. $uid = $authInfo['uid'];
  136. }
  137. }
  138. return $uid;
  139. }
  140. }