AssitMiddleware.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\User;
  7. use App\Models\AssitLog;
  8. use Illuminate\Database\Eloquent\Model;
  9. class AssitMiddleware
  10. {
  11. /**
  12. * The authentication guard factory instance.
  13. *
  14. * @var \Illuminate\Contracts\Auth\Factory
  15. */
  16. protected $auth;
  17. /**
  18. * Create a new middleware instance.
  19. *
  20. * @param \Illuminate\Contracts\Auth\Factory $auth
  21. * @return void
  22. */
  23. public function __construct(Auth $auth)
  24. {
  25. $this->auth = $auth;
  26. }
  27. /**
  28. * Handle an incoming request.
  29. *
  30. * @param \Illuminate\Http\Request $request
  31. * @param \Closure $next
  32. * @param string|null $guard
  33. * @return mixed
  34. */
  35. public function handle($request, Closure $next, $guard = null)
  36. {
  37. $token=$request->header('authorization');
  38. $helper=$request->header('helper',0);
  39. $token_end = explode('.',$token)[2];
  40. if($this->auth->check()) {
  41. $user_id = (int) $this->auth->user()->id;
  42. if($helper==1){
  43. $assit_id=Assit::where('token_end',$token_end)->where('agent_id',$user_id)->value('id');
  44. if($assit_id){
  45. $user=User::where('id',$user_id)->select('id','deleted_at','status','freeze_status','level')->first();
  46. if(!empty($user)){
  47. if(!empty($user->deleted_at)){
  48. return response()->json([
  49. "code"=>451101,
  50. "msg"=>'账号已被删除,请核对后重新登录',
  51. "message"=>'账号已被删除,请核对后重新登录',
  52. "data"=>'账号已被删除,请核对后重新登录',
  53. ], 200);
  54. }
  55. }else{
  56. return response()->json([
  57. "code"=>451101,
  58. "msg"=>'账号信息不存在,请核对后重新登录',
  59. "message"=>'账号信息不存在,请核对后重新登录',
  60. "data"=>'账号信息不存在,请核对后重新登录',
  61. ], 200);
  62. }
  63. if($user->status==1){
  64. return response()->json([
  65. "code"=>451101,
  66. "msg"=>'该用户已被禁用,请核对后重新登录',
  67. "message"=>'该用户已被禁用,请核对后重新登录',
  68. "data"=>'该用户已被禁用,请核对后重新登录',
  69. ], 200);
  70. }
  71. if($user->freeze_status==1){
  72. return response()->json([
  73. "code"=>451101,
  74. "msg"=>'该用户已被冻结,请核对后重新登录',
  75. "message"=>'该用户已被冻结,请核对后重新登录',
  76. "data"=>'该用户已被冻结,请核对后重新登录',
  77. ], 200);
  78. }
  79. if($user->level!=3){
  80. return response()->json([
  81. "code"=>451101,
  82. "msg"=>'该用户不是批发商,请核对后重新登录',
  83. "message"=>'该用户不是批发商,请核对后重新登录',
  84. "data"=>'该用户不是批发商,请核对后重新登录',
  85. ], 200);
  86. }
  87. // if('GET' != $request->method()){
  88. $log = new AssitLog(); # 提前创建表、model
  89. $log->agent_id = $user_id;
  90. $log->assit_id = $assit_id;
  91. $log->path = $request->path();
  92. $log->method = $request->method();
  93. $log->ip = $request->ip();
  94. $log->input = json_encode($request->all(), JSON_UNESCAPED_UNICODE);
  95. $log->save(); # 记录日志
  96. // }
  97. }else{
  98. return response()->json([
  99. "code"=>451101,
  100. "msg"=>'登录状态异常,请重新登录',
  101. "message"=>'登录状态异常,请重新登录',
  102. "data"=>'登录状态异常,请重新登录',
  103. ], 200);
  104. }
  105. }
  106. }
  107. return $next($request);
  108. }
  109. }