package com.ruoyi.common.valid.towEle;

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

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.valid.towEle.vo.IDResponse;

import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TwoEleUtil {

    public static final String HOST = "https://zidv2.market.alicloudapi.com/idcheck/Post";

    public static final String APPCODE = SpringUtils.getRequiredProperty("twoEle.appCode");

    /**
     * 校验身份二要素是否一致
     * @param host  请求地址
     * @param appcode  appcode
     * @param idCard  身份证号
     * @param realName  姓名
     * @return Boolean
     */
    public static IDResponse verifyIdentity(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);
            if (result == null) {
                return null;
            }
            return JSONObject.parseObject(result, IDResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
