mirror of
https://gitee.com/technical-laohu/mpay_v2_webman.git
synced 2026-04-21 17:44:27 +08:00
更新后端基础
This commit is contained in:
61
app/common/utils/JwtUtil.php
Normal file
61
app/common/utils/JwtUtil.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\utils;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
|
||||
class JwtUtil
|
||||
{
|
||||
/**
|
||||
* 生成 JWT
|
||||
*/
|
||||
public static function generateToken(array $payloadBase): string
|
||||
{
|
||||
$config = config('jwt', []);
|
||||
$secret = $config['secret'] ?? 'mpay-secret';
|
||||
$ttl = (int)($config['ttl'] ?? 7200);
|
||||
$alg = $config['alg'] ?? 'HS256';
|
||||
|
||||
$now = time();
|
||||
$payload = array_merge($payloadBase, [
|
||||
'iat' => $now,
|
||||
'exp' => $now + $ttl,
|
||||
]);
|
||||
|
||||
return JWT::encode($payload, $secret, $alg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 JWT
|
||||
*/
|
||||
public static function parseToken(string $token): array
|
||||
{
|
||||
$config = config('jwt', []);
|
||||
$secret = $config['secret'] ?? 'mpay-secret';
|
||||
$alg = $config['alg'] ?? 'HS256';
|
||||
|
||||
$decoded = JWT::decode($token, new Key($secret, $alg));
|
||||
return json_decode(json_encode($decoded, JSON_UNESCAPED_UNICODE), true) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 ttl(秒)
|
||||
*/
|
||||
public static function getTtl(): int
|
||||
{
|
||||
$config = config('jwt', []);
|
||||
return (int)($config['ttl'] ?? 7200);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存前缀
|
||||
*/
|
||||
public static function getCachePrefix(): string
|
||||
{
|
||||
$config = config('jwt', []);
|
||||
return $config['cache_prefix'] ?? 'token_';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user