UserMiddleware.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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->header('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('用户登录凭证错误', 410000);
  34. }
  35. try{
  36. $memData = (new UserModel)
  37. ->alias("u")
  38. // ->field("u.*,IFNULL(wt.title,'') as work_type_title")
  39. ->field("u.*,wt.title as work_type_title")
  40. ->join("user_work_type wt","wt.id = u.work_type_id","left")
  41. ->where('u.token',$token)
  42. ->find();
  43. if(empty($memData)) {
  44. throw new AuthException('用户不存在', 410000);
  45. }
  46. if($memData['status']==0) {
  47. throw new AuthException('用户审核中', 410000);
  48. }
  49. if($memData['status']==-1) {
  50. throw new AuthException('用户已被禁用', 410000);
  51. }
  52. return $memData->toArray();
  53. }catch (\Throwable $e) {
  54. throw new AuthException('系统错误,请重新登录', 410000);
  55. }
  56. }
  57. }