package cn.pluss.platform.util;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.*;

/**
 * AES加密工具类
 *
 * @author ACGkaka
 * @since 2021-06-18 19:11:03
 */
public class TokenUtil {

    //APPID
    private static final String APP_ID = "ZF544";
    //USERCODE
    private static final String USER_CODE = "ZF544";
    //APPSECRET
    private static final String APP_SECRET = "2022bsjZF544GAH";
    //sn
    private static final String SN = "BSJQG01YJ0001";

    /**
     * 获取TOKEN值
     * @param timestamp 时间戳，13位
     * @param requestId 请求ID，自定义
     * @return
     */
    public static Map<String, String> getToken(String timestamp, String requestId) {
        String token = "";
        String encode = "";
        SortedMap<String, Object> map = new TreeMap();
        map.put("appId", APP_ID);
        map.put("timestamp", timestamp);
        map.put("requestId", requestId);
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            String key = next.getKey();
            Object value = next.getValue();
            token += key + value;
            encode += key + "=" + value + "&";
        }
        System.out.println(token);
        Map<String, String> finalMap = new HashMap<>();
        finalMap.put("ENCODE",encode);
        System.out.println(token + APP_SECRET);
        finalMap.put("TOKEN", MD5Util.md5(token + APP_SECRET).toUpperCase());
        return finalMap;
    }

    public static void main(String[] args) {
        String s = String.valueOf(System.currentTimeMillis());
        System.out.println(s);
        String s1 = UUID.randomUUID().toString();
        System.out.println(s1);
        Map<String, String> token = getToken(s, s1);
        System.out.println(token);
    }

}
