UserMiddleware.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-30 14:59
  10. // +----------------------------------------------------------------------
  11. namespace app\api\middleware;
  12. use app\Request;
  13. use app\model\api\User as UserModel;
  14. use Firebase\JWT\JWT;
  15. use library\exceptions\AuthException;
  16. use library\interfaces\MiddlewareInterface;
  17. use think\facade\Env;
  18. class UserMiddleware implements MiddlewareInterface
  19. {
  20. public function handle(Request $request, \Closure $next)
  21. {
  22. $token =$request->request('token');
  23. $userData = $this->checkUser($token);
  24. $request->user = $userData;
  25. return $next($request);
  26. }
  27. /**
  28. * 检查数据是否正常
  29. * @param $secret_key
  30. */
  31. private function checkUser($token) {
  32. if(empty($token)) {
  33. throw new AuthException('用户登录凭证错误', -1);
  34. }
  35. try{
  36. $memData = (new UserModel)
  37. ->where('token',$token)
  38. ->find();
  39. if(empty($memData)) {
  40. throw new AuthException('用户不存在', -1);
  41. }
  42. if($memData['status']==0) {
  43. throw new AuthException('用户审核中', -1);
  44. }
  45. if($memData['status']==-1) {
  46. throw new AuthException('用户已被禁用', -1);
  47. }
  48. return $memData->toArray();
  49. }catch (\Throwable $e) {
  50. throw new AuthException('系统错误,请重新登录', -1);
  51. }
  52. }
  53. }