package com.njcn.auth.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.nimbusds.jose.JWSObject;
import com.njcn.common.pojo.constant.SecurityConstants;
import lombok.SneakyThrows;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年06月04日 14:00
 */
public class AuthPubUtil {

    public static String getKaptchaText(int codeLength) {
        StringBuilder code = new StringBuilder();
        int letterLength = RandomUtil.randomInt(codeLength - 1) + 1;
        code.append(RandomUtil.randomString(RandomUtil.BASE_CHAR, letterLength).toUpperCase(Locale.ROOT));
        int numberLength = codeLength - letterLength;
        code.append(RandomUtil.randomString(RandomUtil.BASE_NUMBER, numberLength));
        List<String> textList = Arrays.asList(code.toString().split(""));
        //填充完字符后，打乱顺序，返回字符串
        Collections.shuffle(textList);
        return String.join("", textList);
    }


    @SneakyThrows
    public static JSONObject getLoginByToken(String token){
        JWSObject jwsObject = JWSObject.parse(token);
        String payload = jwsObject.getPayload().toString();
        return JSONUtil.parseObj(payload);
    }

}
