package com.yfd.platform.utils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class Test {

    public static void main(String[] args) throws IOException {
        // 创建一个测试数据Map


        String jsonString = "{\n" +
                "  \"corp\": \"圣爱眼科\",\n" +
                "  \"token\": \"Tbs3bRZsPkYAk39TUsbFxEx1T7m21f\",\n" +
                "  \"uuid\": \"\",\n" +
                "  \"student\": {\n" +
                "    \"school\": \"青岛大名路小学\",\n" +
                "    \"grade\": \"二年级\",\n" +
                "    \"class\": \"1班\",\n" +
                "    \"name\": \"张三\",\n" +
                "    \"identify\": \"370283201409270808\",\n" +
                "    \"sex\": 1,\n" +
                "    \"birthday\": \"20140911\",\n" +
                "    \"county\": \"市北区\"\n" +
                "  },\n" +
                "  \"vision\": {\n" +
                "    \"nakeLeft\": \"4.8\",\n" +
                "    \"nakeRight\": \"4.5\",\n" +
                "    \"glassLeft\": \"5.0\",\n" +
                "    \"glassRight\": \"5.0\",\n" +
                "    \"sphericLeft\": \"-3.25\",\n" +
                "    \"sphericRight\": \"0.75\",\n" +
                "    \"cylinderLeft\": \"-2.75\",\n" +
                "    \"cylinderRight\": \"0.50\",\n" +
                "    \"axialLeft\": \"175\",\n" +
                "    \"axialRight\": \"169\",\n" +
                "    \"glassType\": 1,\n" +
                "    \"inspectTime\": \"20240227102748\"\n" +
                "  }\n" +
                "}";
        Map<String, Object> dataMap = jsonToMap(jsonString);
        try {
            // 调用测试方法
            String encodedString = encodeData(dataMap);

            // 将Map对象转换为JSON字符串
            String jsonData = new ObjectMapper().writeValueAsString(dataMap);

            // 对JSON字符串进行Base64编码
            String base64Encoded = Base64.getEncoder().encodeToString(jsonData.getBytes(StandardCharsets.UTF_8));

            // 对Base64编码后的字符串进行URL编码
            String urlEncoded = URLEncoder.encode(base64Encoded, StandardCharsets.UTF_8.toString());
            System.out.println("Encoded String: " + urlEncoded);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static Map<String, Object> jsonToMap(String jsonString) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() {});
    }
    public static String encodeData(Map<String, Object> dataMap) throws UnsupportedEncodingException {
        // 将Map转换为JSON字符串
        String jsonString = JsonUtils.mapToJson(dataMap);

        // 将JSON字符串编码为Base64
        String base64 = new String(Base64Utils.encode(jsonString.getBytes()), Charset.forName("UTF-8"));

        // 对Base64字符串进行URL编码
        String encode = URLEncoder.encode(base64, "UTF-8");

        return encode;
    }
}

// 假设你已经有了JsonUtils和Base64Utils类
class JsonUtils {
    public static String mapToJson(Map<String, Object> map) {
        // 这里假设你有一个方法可以将Map转换为JSON字符串
        // 例如使用Jackson或Gson库
        return "{\"name\":\"John Doe\",\"age\":30,\"isActive\":true}";
    }
}

class Base64Utils {
    public static byte[] encode(byte[] bytes) {
        // 这里假设你有一个方法可以将字节数组编码为Base64字节数组
        // 例如使用java.util.Base64类
        return java.util.Base64.getEncoder().encode(bytes);
    }
}


