CheckToken.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Auth\GenericUser;
  5. use Illuminate\Support\Facades\Auth;
  6. use App\Facades\UserFacade;
  7. class CheckToken
  8. {
  9. /**
  10. * Handle an incoming request.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @param \Closure $next
  14. * @return mixed
  15. */
  16. public function handle($request, Closure $next)
  17. {
  18. $typeName = 'api用户登陆验证 ';
  19. $startTime = microtime(TRUE);
  20. UserFacade::setLog($typeName.'开始', $startTime, ['token'=>$request->header('token')]);
  21. $token = $request->header('token');
  22. try {
  23. $user = UserFacade::findLoginBy($token);
  24. Auth::login(new GenericUser(
  25. array(
  26. 'id'=>$user->getData()->data->uid,
  27. 'password'=>"123456",
  28. 'remember_token'=>$token
  29. )
  30. ));
  31. UserFacade::setLog($typeName.'查找用户结果', $startTime, [$user->getData()]);
  32. } catch (\Exception $e) {
  33. $auth = [
  34. 'code' => 1006,
  35. 'data' => [
  36. 'ip' => $request->ip(),
  37. 'token' => $request->header('token')
  38. ],
  39. 'msg' => '登陆验证失败,重新登陆'
  40. ];
  41. UserFacade::setLog($typeName.'失败', $startTime, [$auth]);
  42. return response()->json($auth);
  43. }
  44. return $next($request);
  45. }
  46. }