package com.chaozhanggui.admin.system.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author admin
 * @date 2020/8/6 13:04
 */
public class TokenUtil {

    /**
     * 设置过期时间
     */
    public static final long EXPIRE_DATE=24*60*60*1000*365;
    /**
     * token秘钥
     */
    private static final String TOKEN_SECRET = "BBDFSDFHFGHSGSRTRESDFSDFS";

    /**
     * 初始化生成token的参数
     * @param userId
     * @param phone
     * @return
     */
    public static String generateToken(String userId,String loginName,String userType) throws Exception {
        Map<String, Object> claims = new HashMap<>(1);
        claims.put("userId", userId);
        claims.put("loginName",loginName);
        claims.put("userType",userType);
        return generateToken(claims);
    }

    /**
     * 生成token
     * @param claims
     * @return String
     */
    private  static String generateToken(Map<String, Object> claims) throws Exception {
        return Jwts.builder()
                .setClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis()+EXPIRE_DATE))
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256,TOKEN_SECRET)
                .compact();
    }

    public static String refreshToken(String token) {
        String refreshedToken;
        try {
            final Claims claims = Jwts.parser()
                    .setSigningKey(TOKEN_SECRET)
                    .parseClaimsJws(token)
                    .getBody();
            refreshedToken = generateToken(claims);
        } catch (Exception e) {
            refreshedToken = null;
        }
        return refreshedToken;
    }

    public static String verifyToken(String token) {
        String result = "";
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey(TOKEN_SECRET)
                    .parseClaimsJws(token)
                    .getBody();
            result = "1";
        } catch (Exception e) {
            result = "0";
        }
        return result;
    }

    /**
     * 从token获取用户信息
     * @param token token
     * @return 用户Id
     */
    public static Object parseParamFromToken(final String token, final Object...paramName ) {
        Claims claims = Jwts.parser()
                .setSigningKey(TOKEN_SECRET)
                .parseClaimsJws(token)
                .getBody();
        return claims.get(paramName);
    }
    public static void main(String[] args){
        System.out.println(refreshToken("eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE1OTY4Nzc5MjEsInN1YiI6ImRkZGRkIiwiaWF0IjoxNTk2Njk3OTIxfQ.lrg3KF9h9izbmyD2q5onqnZIKBqanWy9xCcroFpjxPKmJz6kz27G9lVlFpVanrL1I4SFf3Dz3q3Xu01DX2T_dw"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cld = Calendar.getInstance();
        cld.setTime(new Date());
        cld.set(Calendar.DATE, cld.get(Calendar.DATE)-1);
        System.out.println(sdf.format(cld.getTime()));
    }

}
