项目根目录composer下载
引入php-jwt包
composer require firebase/php-jwt
封装方法
<?php
namespace app\business;
use Firebase\JWT\ExpiredException;
use Firebase\JWT\JWT as JWTUtil;
class JWT
{
/**
* 根据json web token设置的规则生成token
* @param $user_id
* @return \think\response\Json
*/
public static function createJwt($user_id)
{
//jwt的签发密钥,验证token的时候需要用到
$key = md5(env("TOKEN.key",""));
//签发时间
$time = time();
//过期时间
$expire = $time + 14400;
$token = array(
//用户
"user_id" => $user_id,
//签发组织
"iss" => env("TOKEN.iss",""),
//签发作者
"aud" => env("TOKEN.aud",""),
//签发时间