package com.tiesheng.login.config.token.bean;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.jwt.JWT;
import com.tiesheng.login.config.token.TsTokenConfig;

/**
 * @author hao
 */
public class TokenBean {

    private String id;
    private String environmentType;
    private String service;
    private String extra;

    public TokenBean() {
    }

    public TokenBean(String id, String environmentType, String service) {
        this.id = id;
        this.environmentType = environmentType;
        this.service = service;
        this.extra = "";
    }


    /**
     * 设置token
     */
    public String toToken() {
        TsTokenConfig tsTokenConfig = SpringUtil.getBean(TsTokenConfig.class);
        return JWT.create()
                .setPayload("id", getId())
                .setPayload("environmentType", getEnvironmentType())
                .setPayload("service", getService())
                .setPayload("extra", getExtra())
                .setPayload("time", System.currentTimeMillis() + tsTokenConfig.getExpireHours() * 1000 * 60 * 60)
                .setKey(tsTokenConfig.getEncryptKey().getBytes())
                .sign();
    }

    ///////////////////////////////////////////////////////////////////////////
    // setter\getter
    ///////////////////////////////////////////////////////////////////////////

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEnvironmentType() {
        return environmentType;
    }

    public void setEnvironmentType(String environmentType) {
        this.environmentType = environmentType;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }
}
