package cn.pluss.platform.util;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import cn.pluss.platform.entity.UserInfo;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author DJH
 */
public class JwtUtils {

    public static String getToken(UserInfo userInfo) {
        Map<String, Object> map = new HashMap<String, Object>() {
            private static final long serialVersionUID = 1L;
            {
                put("uid", userInfo.getId());
                put("loginName", userInfo.getLoginName());
                put("expire_time", System.currentTimeMillis() + 1000 * 3600 * 24 * 3);
            }
        };
        return JWTUtil.createToken(map, userInfo.getId().toString().getBytes(StandardCharsets.UTF_8));
    }

    public static boolean verify(String token, String userId) {
        return JWTUtil.verify(token, userId.getBytes(StandardCharsets.UTF_8));
    }

    public static Object get(String key, String token) {
        final JWT jwt = JWTUtil.parseToken(token);

        return jwt.getPayload(key);
    }
}
