package com.tiesheng.platform.config.wxmp.bean;

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

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

    private String appId;
    private String nonceStr;
    private long timestamp;
    private String url;
    private String signature;

    public static WxJsapiSignature create(String appId, String url, String ticket) {
        WxJsapiSignature wxJsapiSignature = new WxJsapiSignature();
        wxJsapiSignature.setAppId(appId);
        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.sha1(builder));

        return wxJsapiSignature;
    }


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

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    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;
    }
}
