<?php

namespace Extend\VerifyDun;

use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use App\Utils\HttpRequest;
use App\Utils\Log;
use GuzzleHttp\Exception\GuzzleException;

/**
 * 实证认证
 */
class IdCard extends Base
{
    /**
     * 实证认证
     * @param array $params
     * @return string
     * @throws GuzzleException
     */
    public function checkIdCard(array $params): string
    {
        try {
            // 组合请求地址
            $api_url = $this->api_url . $this->version . '/idcard/check';

            $this->params['businessId'] = "45a8fd254b4649e9bd25d773ac7ab666";

            $this->params = array_merge($this->params,$params);

            // 获取签名
            $this->params['signature'] = $this->gen_signature();

            $this->options["form_params"] = $this->params;

            $result = $this->httpRuest->postRequest($api_url,$this->options);
            if (empty($result)){
                return "身份证认证失败";
            }

            if ($result['code'] != "200"){
                throw new BusinessException("姓名与身份证号不一致");
            }

            if (empty($result['result'])){
                return "身份证认证失败";
            }

            // 处理不通过情况
            if ($result['result']['status'] == 2){
                switch ($result['result']['reasonType']) {
                    case 2:
                        return "输入姓名和身份证号不一致";
                        break;
                    case 3:
                        return "查无此身份证";
                        break;
                    case 4:
                        return "身份证照片信息与输入信息不一致";
                        break;

                    default:
                        return "身份证认证失败";
                        break;
                }
            }

            // 处理status为其他情况
            if ($result['result']['status'] != 1){
                return "身份证认证失败";
            }

            return "";
        } catch (\Exception $e) {
            throw new BusinessException($e->getMessage());
        }
    }
}