2023-11-10 18:09:36 +08:00
|
|
|
|
<?php
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
|
|
|
|
// | 开源版本可自由商用,可去除界面版权logo
|
|
|
|
|
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
|
|
|
|
// | github下载:https://github.com/likeshop-github/likeadmin
|
|
|
|
|
// | 访问官网:https://www.likeadmin.cn
|
|
|
|
|
// | likeadmin团队 版权所有 拥有最终解释权
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | author: likeadminTeam
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\api\service;
|
|
|
|
|
|
|
|
|
|
use Firebase\JWT\JWT;
|
|
|
|
|
use Firebase\JWT\Key;
|
|
|
|
|
use Firebase\JWT\SignatureInvalidException;
|
|
|
|
|
use Firebase\JWT\BeforeValidException;
|
|
|
|
|
use Firebase\JWT\ExpiredException;
|
|
|
|
|
use think\facade\Config;
|
|
|
|
|
|
|
|
|
|
class JwtTokenService
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @param $exp
|
|
|
|
|
* @param array $params
|
|
|
|
|
* @return array
|
|
|
|
|
* @author xaboy
|
|
|
|
|
* @day 2020/10/13
|
|
|
|
|
*/
|
2023-11-13 15:18:31 +08:00
|
|
|
|
public static function createToken(int $id, array $userinfo, array $params = [])
|
2023-11-10 18:09:36 +08:00
|
|
|
|
{
|
|
|
|
|
$time = time();
|
|
|
|
|
$host = app('request')->host();
|
|
|
|
|
$params += [
|
2023-11-10 18:13:00 +08:00
|
|
|
|
'iss' => $host, //签发者
|
|
|
|
|
'aud' => $host, //签发时间
|
2023-11-10 18:09:36 +08:00
|
|
|
|
'iat' => $time,
|
2023-11-10 18:13:00 +08:00
|
|
|
|
'nbf' => $time, //生效时间
|
2023-11-10 18:09:36 +08:00
|
|
|
|
'exp' => $time + 7 * 24 * 3600,
|
|
|
|
|
];
|
|
|
|
|
$params['data'] = [
|
|
|
|
|
'uid' => $id,
|
2023-11-13 15:18:31 +08:00
|
|
|
|
'phone' => $userinfo['phone'],
|
|
|
|
|
'avatar' => $userinfo['avatar'],
|
2023-11-20 13:58:42 +08:00
|
|
|
|
'nickname' => $userinfo['nickname']
|
2023-11-10 18:09:36 +08:00
|
|
|
|
];
|
2023-11-20 16:50:20 +08:00
|
|
|
|
$token = JWT::encode($params, env('app.app_key', ''), 'HS256');
|
2023-11-10 18:09:36 +08:00
|
|
|
|
$tokenInfo = [
|
|
|
|
|
'uid' => $id,
|
2023-11-13 15:18:31 +08:00
|
|
|
|
'phone' => $userinfo['phone'],
|
2023-11-10 18:09:36 +08:00
|
|
|
|
'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秒,时间留点余地
|
2023-11-20 16:50:20 +08:00
|
|
|
|
$decoded = JWT::decode($token, new Key(env('app.app_key', ''), 'HS256'));
|
2023-11-10 18:09:36 +08:00
|
|
|
|
$decodedArray = json_decode(json_encode($decoded), true);
|
2023-11-20 16:50:20 +08:00
|
|
|
|
$jwtData = $decodedArray['data'] ?? [];
|
2023-11-10 18:09:36 +08:00
|
|
|
|
return $jwtData;
|
|
|
|
|
} catch(\Firebase\JWT\SignatureInvalidException $e) {
|
|
|
|
|
throw new \think\Exception('签名错误');
|
2023-11-10 18:10:37 +08:00
|
|
|
|
} catch(\Firebase\JWT\BeforeValidException $e) {
|
2023-11-10 18:09:36 +08:00
|
|
|
|
throw new \think\Exception('token无效');
|
2023-11-10 18:10:37 +08:00
|
|
|
|
} catch(\Firebase\JWT\ExpiredException $e) {
|
2023-11-10 18:09:36 +08:00
|
|
|
|
throw new \think\Exception('token已过期');
|
2023-11-10 18:10:37 +08:00
|
|
|
|
} catch(\Exception $e) {
|
2023-11-10 18:09:36 +08:00
|
|
|
|
throw new \think\Exception('非法请求');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|