package com.jeequan.jeepay.core.jwt;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
import com.jeequan.jeepay.core.utils.DateKit;
import lombok.Data;

import java.util.Map;

/*
* JWT payload 载体
* 格式：
    {
        "sysUserId": "10001",
        "created": "1568250147846",
        "cacheKey": "KEYKEYKEYKEY",
    }
* @author terrfly
* @date 2021/6/8 18:01
*/
@Data
public class JWTPayload {

    // 登录页面（终端）类型
    // WEB - 浏览器页面登录,  APP- APP登录,  LITE-小程序, FACEAPP-刷脸设备
    public interface LOGIN_PAGE_TYPE {
        String WEB = "WEB";
        String APP = "APP";
        String LITE = "LITE";
        String FACE_APP = "FACEAPP";
    }

    /** 认证类型: 区分哪种方式登录的 **/
    public interface CREDENTIAL_AUTH_TYPE{
        String PASSWD = "PWD"; // 密码认证方式  直接写password被检测为高危漏洞（密码硬编码）。。
        String TEL_CODE = "TELCODE"; // 手机验证码认证
        String QR_CODE = "QRCODE"; // 扫码认证
    }

    private Long sysUserId;       //登录用户ID
    private Long created;         //创建时间, 格式：13位时间戳
    private String cacheKey;      //redis保存的key
    private String loginType;     // 登录页面（终端）类型

    protected JWTPayload(){}

    public JWTPayload(JeeUserDetails jeeUserDetails){

        this.setSysUserId(jeeUserDetails.getSysUser().getSysUserId());
        this.setCreated(DateKit.currentTimeMillis());
        this.setCacheKey(jeeUserDetails.getCacheKey());
        this.setLoginType(jeeUserDetails.getLoginType());

    }


    /** toMap **/
    public Map<String, Object> toMap(){
        JSONObject json = (JSONObject)JSONObject.toJSON(this);
        return json.toJavaObject(Map.class);
    }

    // 判断是否app登录
    public boolean isAppLogin(){
        return LOGIN_PAGE_TYPE.APP.equals(getLoginType()) || LOGIN_PAGE_TYPE.LITE.equals(getLoginType()) || LOGIN_PAGE_TYPE.FACE_APP.equals(getLoginType());
    }

}
