<?php

namespace App\Http\Middleware;

use Closure;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;

use Illuminate\Contracts\Auth\Factory as Auths;

class CheckToken
{

    protected $auth;

    public function __construct(Auths $auth){
        $this->auth = $auth;
    }

    public function handle(Request $request, Closure $next)
    {
        try {
            if (! $user = JWTAuth::parseToken()->authenticate()) {  //获取到用户数据，并赋值给$user
                return response()->json([
                    'code' => 401,
                    'msg' => 'user not found'

                ]);
            }
            $user_id = (int) $this->auth->user()->id;
            $admin = Admin::find($user_id);
            if($admin->status == 0){
                $result = [
                    "code"=>'450001',
                    "msg"=>'账号已禁用'
                ];
                return response()->json($result);
            }
//            if(!in_array($user_id,['39516','49428', '32531'])){
//                log::info('用户没有权限访问');
//                $result = [
//                    "code"=>'403',
//                    "message"=>'用户没有权限访问'
//                ];
//                return response()->json($result);
//            }

            return $next($request);
        } catch (TokenExpiredException $e) {
            try {
                $token = JWTAuth::parseToken()->refresh();
                // 使用一次性登录以保证此次请求的成功
                JWTAuth::setToken($token);
                $request->user = JWTAuth::authenticate($token);
                $request->headers->set('Authorization','Bearer '.$token);
            } catch (TokenBlacklistedException $exception){
                // token 令牌已刷新
                return response()->json([
                    'code' => 401,
                    'msg' => 'token 无效,已刷新'  //token无效
                ]);
            } catch (JWTException $e){
                // token 令牌 刷新时间超时，或使用失效的 token 刷新
                return response()->json([
                    'code' => 401,
                    'msg' => '缺少token失效' , //token
                ]);
            }
        }catch (TokenBlacklistedException $exception){
            // token 令牌被拉黑
            return response()->json([
                'code' => 401,
                'msg' => 'token 无效,被拉黑'  //token无效
            ]);

        } catch (TokenInvalidException $e) {

            return response()->json([
                'code' => 401,
                'msg' => 'token 无效',  //token无效
            ]);

        } catch (JWTException $e) {

            return response()->json([
                'code' => 401,
                'msg' => '缺少token' , //token为空
            ]);

        }

        // 在响应头中返回新的 token
        return $next($request)->withHeaders([
            'Authorization'=> 'Bearer '.$token,
        ]);
    }
}
