package com.ruoyi.api.base;

import com.ruoyi.common.enums.HttpStatusEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.AuthRsaUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * 获取系统token 请求参数
 *
 * @author: JiangYuQi
 * @date: 2024/01/13 10:17
 */
@Data
@Slf4j
public class TokenReqVo {

    /**
     * AppId
     */
    @NotBlank(message = "AppId不能为空")
    @Size(min = 1, max = 64, message = "AppId格式异常")
    private String appId;

    /**
     * 签名
     * RSA(用户账号 + 时间戳)加密
     */
    @NotBlank(message = "签名不能为空")
    private String sign;

    /**
     * 时间戳
     */
    @NotNull(message = "时间戳不能为空")
    private Long timestamp;

    /**
     * 获取登录签名
     *
     * @author: JiangYuQi
     * @date: 2024/01/13 10:17
     */
    public Boolean getLoginSign(String privateKey) {
        boolean signFlag = false;
        try {
            String decryptByPrivateKey = AuthRsaUtils.decryptByPrivateKey(privateKey,sign);
            if (StringUtils.equals(appId + timestamp, decryptByPrivateKey)) {
                signFlag = true;
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return signFlag;
    }

    /**
     * 效验时间签名[3分钟有效期]
     */
    public Boolean checkTimestamp() {
        boolean timestampFlag = false;
        long timePoor = Math.abs(timestamp - System.currentTimeMillis());
        if (timePoor < 1000 * 60 * 3) {
            timestampFlag = true;
        }
        return timestampFlag;
    }
}
