<?php

namespace app\admin\api;

use OneSm\Sm4;

class RsApi {

    private $key;

    private $iv;

    private $clientId;

    private $sjhjLog;

    private $sm4;

    public function __construct()
    {
        $this->key = 'b123d075924b4224';
        $this->iv = '8a003e84b5be7b6e';
        $this->clientId = 'jjrzzb';
        $this->sjhjLog = [
            'requestIpAddr' => '10.41.18.18',
            'requestMacAddr' => '28:6e:d4:88:cb:98',
            'requestReason' => '申报系统审核',
            'operaterName' => '系统',
            'operaterIdcard' => '123456789'
        ];
        $this->sm4 = new Sm4($this->key);

    }

    protected function check(){
        $deny_list = ["jjrcw.test", "jjrcw.test:8080","report.com","report.jinjianghc.com"]; //拒绝短信名单
        if (in_array($_SERVER["HTTP_HOST"], $deny_list))
            return false; //测试不请求数据
        else
            return true;
    }

    protected function getInfo($url,$data){
        $encrypt_str = date("YmdHis",time()) . json_encode($data,JSON_UNESCAPED_UNICODE);
        $encrypt_data = $this->sm4->enDataCbc($encrypt_str, $this->iv);
        $res = $this->RsCurl('http://172.29.133.70:58080/service-proxy/proxy/'.$url,base64_encode($encrypt_data));
        $decrypt_data = $this->sm4->deDataCbc(base64_decode($res), $this->iv);
        $decrypt_data = mb_substr($decrypt_data, 14, strlen($decrypt_data), 'UTF-8');
        return $decrypt_data;
    }

    private function RsCurl($url, $postFields){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',   //json版本需要填写  Content-Type: application/json;
                'clientId: jjrzzb',
                'sysClientCache: false',
                'serviceProxyCache: false',
                'resultCache: false'
            )
        );
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec($ch);
        if (false == $ret) {
            $result = curl_error($ch);
        } else {
            $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 " . $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
        curl_close($ch);
        return $result;
    }

    /**
     * 根据身份证号码查询身份证信息
     * @param $card
     */
    public function I010902($card){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'userIDCard' => '聚才网',
            'queryCause' => '聚才网人员信息核查',
            'reqIp' => '10.41.18.18',
            'QQF_MAC' => '28:6e:d4:88:cb:98',
            'userName' => '聚才网',
            'approvePhone' => '18060002035',
            'BHCR_SFZH' => $card,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('I010902',$param);

        return json_decode($res,true);

    }

    /**
     * 根据身份证号码查询个人婚姻信息
     * @param $card
     * @return array|mixed
     */
    public function I030101($card){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'aac002' => $card,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('I030101',$param);

        return json_decode($res,true);
    }

    /**
     * 根据证件号码、社保缴交年月查询个人社保缴费信息
     * @param $card
     * @param $time
     * @return array|mixed
     */
    public function I110202($card,$time){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'zjhm' => $card,
            'ny' => $time,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('I110202',$param);

        return json_decode($res,true);
    }

    /**
     * 根据证件号码查询失信被执行人 编码S0503
     * @param $card
     * @return array|mixed
     */
    public function S0503($card){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'aac002' => $card,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('S0503',$param);

        return json_decode($res,true);
    }

    /**
     * 查询晋江养老缴费记录
     * @param $card
     * @param $start
     * @param $end
     * @return array|mixed
     */
    public function YL00018($card,$start,$end){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'aac147' => $card,
            'aae030' => $start,
            'aae031' => $end,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('YL00018',$param);

        return json_decode($res,true);
    }

    /**
     * 人社部协同平台-专业技术人员职业资格证书查询
     * @param $realname
     * @param $card
     * @return array|mixed
     */
    public function SBJ0007($realname,$card){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'ksxm' => $realname,
            'zjhm' => $card,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('SBJ0007',$param);

        return json_decode($res,true);
    }

    /**
     * 教育部-根据身份证及姓名查询高校学历信息
     * @param $realname
     * @param $card
     * @return array|mixed
     */
    public function SBJ0004($realname,$card){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'xm' => $realname,
            'zjhm' => $card,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('SBJ0004',$param);

        return json_decode($res,true);
    }

    /**
     * 根据统一社会信用代码查询企业基本信息
     * @param $idCard
     */
    public function I040102($idCard){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'USCC' => $idCard,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('I040102',$param);

        return json_decode($res,true);
    }

    /**
     * 根据统一社会信用代码查询事业单位基本信息
     * @param $idCard
     */
    public function I080101($idCard){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'tyshxydm' => $idCard,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('I080101',$param);

        return json_decode($res,true);
    }

    /**
     * 根据民办非企业全称和统一社会信用代码查询民办非企业完整信息
     * @param $name
     * @param $idCard
     * @return array|mixed
     */
    public function I030501($name, $idCard){
        $check_res = $this->check();
        if(!$check_res) return ['code' => '403','message' => '测试环境'];
        $param = [
            'aab004' => $name,
            'aae136' => $idCard,
            'sjhjLog' => $this->sjhjLog
        ];

        $res = $this->getInfo('I030501',$param);

        return json_decode($res,true);
    }


}