package com.zhangmeng.online.exam.ui.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.zhangmeng.online.exam.ui.service.UserService;


import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zm
 * @date 2025/3/5 15:28
 * @version: 1.0
 */
public class HttpUtils<T> {


    public static final String Token = "eyJhbGciOiJIUzI1NiJ9.eyJMT0dJTl9VU0VSX0tFWSI6ImUwZWI0MDk2LTRjYzctNDAzOC1iZWI4LTY4OWEzMzZjN2FiNSJ9.BcM1VxIBXDgpu1fzSSRGFPeaOXEzdPJQPr8Mh8PL-UU";

    public static Map<String, Object> USER_INFO = new HashMap<>();

    public static <T> T GET(String url, Class<T> clazz) {
        String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
        return JSONObject.parseObject(response, clazz);
    }

    public static String GET(String url) {
        url = url + "?token=" + UserService.getCurrentUser().getToken();
        return HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
    }

    public static String GET(String url, Map<String, Object> params) {

        String token = UserService.getCurrentUser().getToken();
        params.put("token",token);
        return HttpUtil.get(url, params);
    }

    public static String POST(String url, Map<String, Object> params) {
        HashMap<String, Object> paramMap = new HashMap<>(params);
        String token = UserService.getCurrentUser().getToken();
        paramMap.put("token",token);
        return HttpUtil.post(url, paramMap);
    }

    public static <T> T POSTFileUPLOAD(String url, Map<String, Object> params, File file, Class<T> clazz) {
        HashMap<String, Object> paramMap = new HashMap<>();
        //文件上传只需将参数中的键指定（默认file），值设为文件对象即可，对于使用者来说，文件上传与普通表单提交并无区别
        paramMap.put("file", file);
        paramMap.putAll(params);
        String response = HttpUtil.post(url, paramMap);
        return JSONObject.parseObject(response, clazz);
    }
}
