<?php

namespace App\Services;

use App\Traits\EasyCtid;

class EasyCtidService
{
    use EasyCtid;

    public function verify(string $realName, string $idNo, string $photoData)
    {
        // 签名私钥
        $pri = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJtxSZ+l/BxsPQtzdVW+RMeGpjMJGoGXzSNQiPsXQdL0Apo72aXpRLizn6BNNexrO12WwfIKUlUzZ5WrUKvyQzKTwbKL2PWL7FCWdcqgi1KJ9tygfolGuSqLaY1lTB1HubSuLaTbpmiS5Khjdwkpg5VAZH2sFhz8hKfc1ir5tBFfAgMBAAECgYBo7pwFwtYjFcpLvaBaxnvFuZbeYunay+/2Q/qEjhmoX4KWPYranlL+4HsEirq5I1ZLAmdgjikwTOhDOAfYW02Se3+sproTyqAy3/qimk/MqbW/bT6whfYocZlZbnCdgeaPK+xyhRDYPDrRGCmiYLbuDdflSX3aNkbgFMNhRAuIKQJBANrxMQUMxiNe15zqq9b9Owm6hFWCQvFzBlHBebaQUge+lK4g6WIm0L7mfeXuo2Fc0M8j++hukxsmFhSpTDXv/JUCQQC1wKTy+MXaX3YXkoYKrfywbdMJMr0OdfboJ7KcEmaOZ6N6XGXrHS437lOUkIk6VCEEFB+v1b1YOS5O+vwVFyUjAkBG5NSPowxBhcNhXyeXbNYCF3AkVBKjJjDEplZoBCFbup1gKO4JRzJOrknT+0rSCwDeIfO+1pcxskpyFEPGL1hlAkBOduV/M6EO/mlYEdymXaiabj9OWEZAKPM4FHNKZ7M4xUtRNvSVBRr/8HbKy39C3DEItrtbSNYecUkptbcAT+arAkEAlCqVfwydy7oM7+lPU39jmPv6Mkj1I0qc/x+j2jp8gkJj91pogQOzQQd/wpNywYDCORD2r84lPyK+JrP+OjODQw==';

        // 加密公钥
        $pub = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCl23X1jCttl9A93NT1Uev8/5rXZezm83BwbJKQQFi8onP7lBRTBQh8/ahyhMX8Qc2QKEq4/t58Q0NVZE2/Yb8OqZk4LEu1pCt2ra57DSvgMQ41oxDere4kyTYAqFuMJoQQgEmyYpzpHQu1GlSYRtbJ3Oxd/JqHfOSjPVYJaDEBswIDAQAB';

        // 基本信息
        $orgCode = '29fb7b4673014b0da0833dafbcec9848';
        $appId = '1212';
        $userId = '123';
        $appName = 'dddd';
        $readCardCtrlVer = '0400.0001.0101.0006';
        $authCodeCtrlVer = '0400.0001.0101.0006';
        $photoCtrlVer = '0400.0001.0101.0006';

        // 请求模式
        $authMode = '0x42';
        $bizType = 6000;
        $applyData = '';

        $bizData = array('authMode' => $authMode, 'applyData' => $applyData, 'appName' => $appName
        , 'readCardCtrlVer' => $readCardCtrlVer, 'authCodeCtrlVer' => $authCodeCtrlVer, 'photoCtrlVer' => $photoCtrlVer);

        // 填充bizPckage
        $bizPackage = array('orgCode' => $orgCode, 'appId' => $appId, 'userId' => $userId, 'bizType' => $bizType, 'bizData' => $bizData);

        // 生成bizPackage的json字符串
        $bizPackage = json_encode($bizPackage,JSON_UNESCAPED_SLASHES);

        // 对bizPackage进行签名，并使用base64编码
        $sign = $this->getSign($bizPackage, $pri);
        //echo $sign;
        $sign = base64_encode($sign);

        // 生成完整请求参数
        $reqContent = array('bizPackage' => $bizPackage, 'sign' => $sign);
        $reqContent = json_encode($reqContent,JSON_UNESCAPED_SLASHES);

//        echo "======================== 申请一包：========================\n" . $reqContent . "\n";
        $result = $this->request_by_curl("http://api.easyctid.cn/v1/apply", $reqContent);
//        echo "结果：\n" . $result . "\n";
//        echo "======================== 请求结束 ========================\n";

        // 将一包返回json字符串转为普通参数
        $result = json_decode($result, true);
        // 获取bsn、randomNumber
        $bizPackage = json_decode($result['bizPackage'], true);
        $bizData = $bizPackage['bizData'];
        $bsn = $bizData['bsn'];
        $randomNumber = $bizData['randomNumber'];

//        echo "获取到参数：\n" . "bsn: " . $bsn . "\n" . "随机数: " . $randomNumber . "\n";

//        echo "======================== 一包结束 ========================\n\n\n";

        // ------------------------------------ 二包 --------------------------------------
        // 两项信息
//        $realName = '张三';
//        $idNo = '110101200001011297';

        $authApplyRetainData = array('name' => $realName, 'idNo' => $idNo);
        // 转为json
        $authApplyRetainData = json_encode($authApplyRetainData,JSON_UNESCAPED_SLASHES);
        // 加密
        $authApplyRetainData = $this->encrypt($authApplyRetainData, $pub);
        // 使用base64编码
        $authApplyRetainData = base64_encode($authApplyRetainData);

        // 填充bizData
        $bizData = array('authMode' => $authMode, 'appName' => $appName, 'authApplyRetainData' => $authApplyRetainData, 'photoData' => $photoData);
        // 填充bizPckage
        $bizPackage = array('orgCode' => $orgCode, 'appId' => $appId, 'userId' => $userId, 'bizType' => $bizType, 'bizData' => $bizData
        , 'bsn' => $bsn, 'randomNumber' => $randomNumber);

        // 生成bizPackage的json字符串
        $bizPackage = json_encode($bizPackage,JSON_UNESCAPED_SLASHES);

        // 对bizPackage进行签名，并使用base64编码
        $sign = $this->getSign($bizPackage, $pri);
        $sign = base64_encode($sign);

        // 生成完整请求参数
        $reqContent = array('bizPackage' => $bizPackage, 'sign' => $sign);
        $reqContent = json_encode($reqContent,JSON_UNESCAPED_SLASHES);

//        echo "======================== 请求二包：========================\n" . $reqContent . "\n";
        $result = $this->request_by_curl("http://api.easyctid.cn/ctid/v1/verification", $reqContent);
        $result = json_decode($result, TRUE);
        $result = json_decode($result['bizPackage'],TRUE);
        return $result;
    }
}
