*/ namespace App\Kernel; use function Hyperf\Config\config; use Firebase\JWT\JWT; use Firebase\JWT\Key; class Token { /** * 生成token * Author: cfn * @param $data * @param $expireIn * @return string */ static function buildToken($data, $expireIn): string { $payload = [ 'iss' => config("app.iss"), 'aud' => config("app.aud"), 'iat' => time(), 'exp' => time() + $expireIn, 'data' => $data ]; $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap(config('app.private'), 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----"; return JWT::encode($payload, $privateKey, 'RS256'); } /** * 解析token * Author: cfn * @param $token * @return array|string */ static function parseToken($token) { try { $publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap(config('app.public'), 64, "\n", true) . "\n-----END PUBLIC KEY-----"; $decoded = JWT::decode($token, new Key($publicKey, 'RS256')); $decoded_array = (array)$decoded; return (array)$decoded_array['data']; } catch (\Exception $exception){} return ""; } }