package cn.exploring.engine.server.core.tencent;

import cn.exploring.engine.server.core.util.ResponseUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import org.springframework.stereotype.Service;

/**
 * 腾讯云 相关，如：身份证识别
 */
@Service
public class TencentCloudService {
    /**
     * 通过身份证照片识别出姓名，地址等
     * @param idCardImgUrl  身份证照片
     * @return
     */
    public Object  checkIdCardImg(String idCardImgUrl) {
        try{

            Credential cred = new Credential("AKIDIOIzR49jj0194msUKwTlrnV1HNE4azf7", "PY6QVMtDIGzmKejW8WfYWGn98tNdcfS1");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");


            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            String params = "{\"Action\":\"IDCardOCR\",\"ImageUrl\":\""+idCardImgUrl+"\",\"CardSide\":\"FRONT\",\"CardSide\":\"FRONT\",\"Version\":\"2018-11-19\"}";
            IDCardOCRRequest req = IDCardOCRRequest.fromJsonString(params, IDCardOCRRequest.class);
            IDCardOCRResponse resp = client.IDCardOCR(req);

            return ResponseUtil.ok(IDCardOCRRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return ResponseUtil.fail(0,"验证失败");
    }

    /*public static void main(String args[]){
        TencentCloudService tcs = new TencentCloudService();
        System.out.println(tcs.checkIdCardImg("https://test2.exploring.cn/wx/storage/fetch/8pyzwxscgmcead0hz2r3.jpg"));
    }*/
}
