package com.tiesheng.ding.pojos;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;

/**
 * jssdk配置
 *
 * @author hao
 */
public class DingJsapiSignature {

    private String corpId;
    private String agentId;
    private String nonceStr;
    private long timestamp;
    private String url;
    private String signature;

    public static DingJsapiSignature create(String corpId, String agentId, String url, String ticket) {
        DingJsapiSignature wxJsapiSignature = new DingJsapiSignature();
        wxJsapiSignature.setCorpId(corpId);
        wxJsapiSignature.setAgentId(agentId);
        wxJsapiSignature.setUrl(url);
        wxJsapiSignature.setTimestamp(DateUtil.currentSeconds());
        wxJsapiSignature.setNonceStr(RandomUtil.randomString(10));

        // 生成签名
        String builder = "jsapi_ticket=" + ticket +
                "&noncestr=" + wxJsapiSignature.getNonceStr() +
                "&timestamp=" + wxJsapiSignature.getTimestamp() +
                "&url=" + wxJsapiSignature.getUrl();
        wxJsapiSignature.setSignature(SecureUtil.sha256(builder));

        return wxJsapiSignature;
    }


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


    public String getCorpId() {
        return corpId;
    }

    public void setCorpId(String corpId) {
        this.corpId = corpId;
    }

    public String getAgentId() {
        return agentId;
    }

    public void setAgentId(String agentId) {
        this.agentId = agentId;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }
}
