host(); $params += [ 'iss' => $host, //签发者 'aud' => $host, //签发时间 'iat' => $time, 'nbf' => $time, //生效时间 'exp' => $time + 7 * 24 * 3600, ]; $params['data'] = [ 'uid' => $id, 'phone' => $userinfo['phone'], 'avatar' => $userinfo['avatar'], 'nickname' => $userinfo['nickname'] ]; $token = JWT::encode($params, env('app.app_key', ''), 'HS256'); $tokenInfo = [ 'uid' => $id, 'phone' => $userinfo['phone'], 'token' => $token ]; return $tokenInfo; } /** * @param string $token * @return object * @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed * @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf' * @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat' * @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim * @throws UnexpectedValueException Provided JWT was invalid * @author xaboy * @day 2020-04-09 */ public static function parseToken(string $token) { try { JWT::$leeway = 10; //当前时间减去10秒,时间留点余地 $decoded = JWT::decode($token, new Key(env('app.app_key', ''), 'HS256')); $decodedArray = json_decode(json_encode($decoded), true); $jwtData = $decodedArray['data'] ?? []; return $jwtData; } catch(\Firebase\JWT\SignatureInvalidException $e) { throw new \think\Exception('签名错误'); } catch(\Firebase\JWT\BeforeValidException $e) { throw new \think\Exception('token无效'); } catch(\Firebase\JWT\ExpiredException $e) { throw new \think\Exception('token已过期'); } catch(\Exception $e) { throw new \think\Exception('非法请求'); } } }