123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace App\Http\Middleware;
- use Closure;
- use Illuminate\Contracts\Auth\Factory as Auth;
- use App\Models\Assit;
- use App\Models\User;
- 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){
- $user=User::where('id',$user_id)->select('id','deleted_at','status','freeze_status','level')->first();
- if(!empty($user)){
- if(!empty($user->deleted_at)){
- return response()->json([
- "code"=>451101,
- "msg"=>'账号已被删除,请核对后重新登录',
- "message"=>'账号已被删除,请核对后重新登录',
- "data"=>'账号已被删除,请核对后重新登录',
- ], 200);
- }
- }else{
- return response()->json([
- "code"=>451101,
- "msg"=>'账号信息不存在,请核对后重新登录',
- "message"=>'账号信息不存在,请核对后重新登录',
- "data"=>'账号信息不存在,请核对后重新登录',
- ], 200);
- }
- if($user->status==1){
- return response()->json([
- "code"=>451101,
- "msg"=>'该用户已被禁用,请核对后重新登录',
- "message"=>'该用户已被禁用,请核对后重新登录',
- "data"=>'该用户已被禁用,请核对后重新登录',
- ], 200);
- }
- if($user->freeze_status==1){
- return response()->json([
- "code"=>451101,
- "msg"=>'该用户已被冻结,请核对后重新登录',
- "message"=>'该用户已被冻结,请核对后重新登录',
- "data"=>'该用户已被冻结,请核对后重新登录',
- ], 200);
- }
- if($user->level!=3){
- return response()->json([
- "code"=>451101,
- "msg"=>'该用户不是批发商,请核对后重新登录',
- "message"=>'该用户不是批发商,请核对后重新登录',
- "data"=>'该用户不是批发商,请核对后重新登录',
- ], 200);
- }
- // 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);
- }
- }
|