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>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user