AssitMiddleware.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Contracts\Auth\Factory as Auth;
  5. use App\Models\Assit;
  6. use App\Models\AssitLog;
  7. use Illuminate\Database\Eloquent\Model;
  8. class AssitMiddleware
  9. {
  10. /**
  11. * The authentication guard factory instance.
  12. *
  13. * @var \Illuminate\Contracts\Auth\Factory
  14. */
  15. protected $auth;
  16. /**
  17. * Create a new middleware instance.
  18. *
  19. * @param \Illuminate\Contracts\Auth\Factory $auth
  20. * @return void
  21. */
  22. public function __construct(Auth $auth)
  23. {
  24. $this->auth = $auth;
  25. }
  26. /**
  27. * Handle an incoming request.
  28. *
  29. * @param \Illuminate\Http\Request $request
  30. * @param \Closure $next
  31. * @param string|null $guard
  32. * @return mixed
  33. */
  34. public function handle($request, Closure $next, $guard = null)
  35. {
  36. $token=$request->header('authorization');
  37. $helper=$request->header('helper',0);
  38. $token_end = explode('.',$token)[2];
  39. if($this->auth->check()) {
  40. $user_id = (int) $this->auth->user()->id;
  41. if($helper==1){
  42. $assit_id=Assit::where('token_end',$token_end)->where('agent_id',$user_id)->value('id');
  43. if($assit_id){
  44. // if('GET' != $request->method()){
  45. $log = new AssitLog(); # 提前创建表、model
  46. $log->agent_id = $user_id;
  47. $log->assit_id = $assit_id;
  48. $log->path = $request->path();
  49. $log->method = $request->method();
  50. $log->ip = $request->ip();
  51. $log->input = json_encode($request->all(), JSON_UNESCAPED_UNICODE);
  52. $log->save(); # 记录日志
  53. // }
  54. }else{
  55. return response()->json([
  56. "code"=>451101,
  57. "msg"=>'登录状态异常,请重新登录',
  58. "message"=>'登录状态异常,请重新登录',
  59. "data"=>'登录状态异常,请重新登录',
  60. ], 200);
  61. }
  62. }
  63. }
  64. return $next($request);
  65. }
  66. }