package com.jiayue.insu.inclientqn.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.jiayue.insu.inclientqn.entity.Record;
import com.jiayue.insu.inclientqn.inenum.StatusEnum;

public class RequestUtils {
    /**
     * 回传数据post请求
     * @param url 请求路径
     * @param body 请求体
     * @param token token
     * @return 结果
     */
    public static String post(String url,String body,String token){
        String response = "";
        try{
            HttpRequest httpRequest = HttpRequest.post(url)
                    .header("Content-Type", "application/json")
                    .header("Authorization", token);
            httpRequest.setGlobalTimeout(20000);
            response = httpRequest
                    .body(body)
                    .execute().body();
        }catch (Exception e){
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 校验请求返回结果
     * @param response  返回数据
     * @param record 记录
     * @return 是否返回数据正常
     */
    public static boolean checkResponse(String response,Record record){
        boolean result = false;
        if (StrUtil.isNotEmpty(response)) {
            result = JSONUtil.isJsonObj(response);
            //如果不是json格式
            if(!result){
                record.setState(StatusEnum.CONNECT_RESPONSE_FORMAT_ERROR.getSign());
                record.setStateContent(StatusEnum.CONNECT_RESPONSE_FORMAT_ERROR.getMsg());
            }
        }else{
            record.setState(StatusEnum.CONNECT_RESPONSE_CONTENT_NULL.getSign());
            record.setStateContent(StatusEnum.CONNECT_RESPONSE_CONTENT_NULL.getMsg());
        }
        return result;
    }

}
