package com.ruoyi.common.utils.verify;

import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.verify.vo.IDResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class VerifyIdentityUtil {

    /**
     * 校验身份二要素是否一致
     * @param host  请求地址
     * @param appcode  appcode
     * @param idCard  身份证号
     * @param realName  姓名
     * @return Boolean
     */
    public static Object verifyIdentity(String host,String appcode,String idCard,String realName){
        try {
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", "APPCODE " + appcode);
            //根据API的要求，定义相对应的Content-Type
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            Map<String, Object> bodys = new HashMap<>();
            bodys.put("cardNo", idCard);
            bodys.put("realName", realName);
            String result= HttpUtil.createRequest(Method.POST, host).addHeaders(headers).form(bodys).execute().body();
            log.info("实名认证请求返回==================="+result);
            IDResponse idResponse = JSONObject.parseObject(result, IDResponse.class);
            if("成功".equals(idResponse.getReason())){
                IDResponse.Result result1 = idResponse.getResult();
                return result1;
            }else{
                return idResponse.getReason();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
