1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace App\Http\Middleware;
- use Closure;
- use Illuminate\Contracts\Auth\Factory as Auth;
- use App\Models\Assit;
- use App\Models\AssitLog;
- use Illuminate\Database\Eloquent\Model;
- class AssitMiddleware
- {
- /**
- * The authentication guard factory instance.
- *
- * @var \Illuminate\Contracts\Auth\Factory
- */
- protected $auth;
- /**
- * Create a new middleware instance.
- *
- * @param \Illuminate\Contracts\Auth\Factory $auth
- * @return void
- */
- public function __construct(Auth $auth)
- {
- $this->auth = $auth;
- }
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @param string|null $guard
- * @return mixed
- */
- public function handle($request, Closure $next, $guard = null)
- {
- $token=$request->header('authorization');
- $helper=$request->header('helper',0);
- $token_end = explode('.',$token)[2];
- if($this->auth->check()) {
- $user_id = (int) $this->auth->user()->id;
- if($helper==1){
- $assit_id=Assit::where('token_end',$token_end)->where('agent_id',$user_id)->value('id');
- if($assit_id){
- // if('GET' != $request->method()){
- $log = new AssitLog(); # 提前创建表、model
- $log->agent_id = $user_id;
- $log->assit_id = $assit_id;
- $log->path = $request->path();
- $log->method = $request->method();
- $log->ip = $request->ip();
- $log->input = json_encode($request->all(), JSON_UNESCAPED_UNICODE);
- $log->save(); # 记录日志
- // }
- }else{
- return response()->json([
- "code"=>451101,
- "msg"=>'登录状态异常,请重新登录',
- "message"=>'登录状态异常,请重新登录',
- "data"=>'登录状态异常,请重新登录',
- ], 200);
- }
- }
- }
- return $next($request);
- }
- }
|