<?php
/**
 *
 *User:Administrator
 *Date:2021/9/29
 */

namespace app\common\lib\auth;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\ValidationData;

/**
 *  * 单例 一次请求中所有出现使用jwt的地方都是一个用户
 */

class JwtAuth
{
    private $qydm;//企业代码
    private $rybh;//人员编号
    private $rymc;//人员名称
    private $groupname;//组名
    private $uid;//t_ry表的id

    private $token;
    private $decodeToken;

    /**
     * 单例模式 jwtAuth句柄
     * @var
     */
    private static $instance;

    /**
     * 获取jwtAuth的句柄
     * @return JwtAuth
     */
    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    /**
     * 私有化构造函数 防止new
     */
    private function __construct(){}

    /**
     * 私有化 clone函数 防止clone
     */
    private function __clone(){}

    //Token
    public function getToken()
    {
        return (string)$this->token;
    }
    public function setToken($token)
    {
        $this->token=$token;
        return $this;
    }

    //Uid
    public function getUid()
    {
        return (string)$this->uid;
    }
    public function setUid($uid)
    {
        $this->uid=$uid;
        return $this;
    }

    //人员编号
    public function setRybh($rybh)
    {
        $this->rybh=$rybh;
        return $this;
    }
    public function getRybh()
    {
        return $this->rybh;
    }

    //$groupname
    public function setGroupname($groupname)
    {
        $this->groupname=$groupname;
        return $this;
    }
    public function getGroupname()
    {
        return $this->groupname;
    }

    //name
    public function setName($name)
    {
        $this->name=$name;
        return $this;
    }
    public function getName()
    {
        return $this->name;
    }

    //人员名称
    public function setRymc($rymc)
    {
        $this->rymc=$rymc;
        return $this;
    }
    public function getRymc()
    {
        return $this->rymc;
    }

    //企业代码
    public function setQydm($qydm)
    {
        $this->qydm=$qydm;
        return $this;
    }
    public function getQydm()
    {
        return $this->qydm;
    }



    /**
     * @return $this
     */
    public function encode(): JwtAuth
    {
        //设置签发人、接收人、唯一标识、签发时间、立即生效、过期时间、用户id、签名
        $time=time();
        $this->token = (new Builder())
            ->setHeader('alg', 'HS256')
            ->setIssuer(config('jwt.issuer'))
            ->setAudience(config('jwt.audience'))
            ->setExpiration($time + config('jwt.expiration'))

            ->set('uid',$this->uid)
            ->set('name', $this->name)
            ->set('rymc', $this->rymc)
            ->set('groupname', $this->groupname)
            ->set('rybh',$this->rybh)
            ->set('qydm',$this->qydm)
            ->sign(new Sha256(),config('jwt.secret'))
            ->getToken();
    return $this;
    }

    /**
     * @return Token|void
     */
    public function decode()
    {
        if (!$this->decodeToken) {
            $this->decodeToken = (new Parser())->parse((string)$this->token);
            $this->uid = $this->decodeToken->getClaim('uid');
            $this->rybh = $this->decodeToken->getClaim('rybh');
            $this->qydm = $this->decodeToken->getClaim('qydm');
            $this->name = $this->decodeToken->getClaim('name');
            $this->rymc = $this->decodeToken->getClaim('rymc');
            $this->groupname = $this->decodeToken->getClaim('groupname');
        }
        return $this->decodeToken;
    }

    /**
     * verify token
     * @return bool
     */
    public function verify(): bool
    {
        $result = $this->decode()->verify(new Sha256(), config('jwt.secret'));
        return $result;
    }

    /**
     * validate
     * @return bool
     */
    public function validate(): bool
    {
        $data = new ValidationData();
        $data->setIssuer(config('jwt.issuer'));
        $data->setAudience(config('jwt.audience'));

        return $this->decode()->validate($data);
    }

}