Files
x-server-mobile/backend/app/Util/JwtUtil.php
T
root 8d78e18267 Initial commit: X-Server Mobile enterprise tool system
- Frontend: React 18 + TypeScript + Vite + Ant Design Mobile
- Backend: PHP 8.5 + Webman 2.x + MySQL + Redis
- i18n support: Chinese, English, Russian, Spanish, French
- Approval workflow engine
- Docker development environment
- Feishu integration

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 17:57:25 +08:00

48 lines
981 B
PHP

<?php
namespace App\Util;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Exception;
class JwtUtil
{
private static string $secret = 'your-secret-key-change-in-production';
private static int $expire = 86400 * 7; // 7 days
/**
* 生成 Token
*/
public static function encode(array $data): string
{
$payload = array_merge($data, [
'iat' => time(),
'exp' => time() + self::$expire,
]);
return JWT::encode($payload, self::$secret, 'HS256');
}
/**
* 解析 Token
*/
public static function decode(string $token): ?array
{
try {
$decoded = JWT::decode($token, new Key(self::$secret, 'HS256'));
return (array) $decoded;
} catch (Exception $e) {
return null;
}
}
/**
* 验证 Token
*/
public static function verify(string $token): bool
{
return self::decode($token) !== null;
}
}