<?php
namespace app\common\lib;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class Token{
    /**
     * 構造方法
     */
    public function __construct(){

    }

    public static function genToken($payload){
        $payload = array_merge($payload,[
            'aud' => '',
            'exp' => time() + (365 * 24 * 60 * 60),
            'iat' => time(),
            'iss' => '',
            'jti' => uniqid(mt_rand(), true),
            'nbf' => time(),
            'sub' => '',
        ]);

        return JWT::encode($payload, \think\facade\Config::get('jwt.secret'), 'HS256');        
    }

    public static function check($token){
        try{
            $decode = JWT::decode($token, new Key(\think\facade\Config::get('jwt.secret'), 'HS256'));
            return true;
        }catch(\Exception $e){
            return false;
        }
    }

}
