package com.ruoyi.common.valid.face;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.http.AliHttpUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

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

/**
 * @author wjh
 * 2024/9/20
 */
public class FaceUtils {


    public static final String APP_KEY = SpringUtils.getRequiredProperty("face.appKey");
    public static final String APP_CODE = SpringUtils.getRequiredProperty("face.appCode");
    public static final String APP_SECRET = SpringUtils.getRequiredProperty("face.appSecret");


    /**
     * 人证比对
     */
    public static FaceResponseData getFaceIdb(String idcard, String name, String image, String url) {

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + APP_CODE);
        Map<String, String> bodys = new HashMap<>();
        bodys.put("idcard", idcard);
        bodys.put("name", name);
        bodys.put("url", url);
        bodys.put("image", image);

        try {
            HttpResponse res = AliHttpUtils.doPost("https://faceidcardb.shumaidata.com", "/getfaceidb", "POST", headers, null, bodys);
            FaceResponseBody body = JSON.parseObject(EntityUtils.toString(res.getEntity()), FaceResponseBody.class);
            ServiceUtil.assertion(body.getCode() == null || !body.getCode().equals("200"), body.getMsg());
            return body.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
