芝麻认证

class ZhimaController extends Yaf_Controller_Abstract
{
    /**
     * @StartUp         芝麻认证初始化
     * ===========================
     * @POST
     */
    private $biz_no;

    public function InitializeAction($name = "Stringer")
    {
        //查看是否已登录

        $UserID = GetAppUserID();
        $UserReal = new MySqlModel("SELECT `ProofWay` FROM `YunHash`WHERE `UserID` = $UserID");
        $UserReal = $UserReal->WrOneAssoc();
        if((int)$UserReal['UserReal'] == 1){
            ReturnEcho(208);
        }
        spl_autoload_register('AliPayAutoload');
        $identity = $_POST["identity"];
        $name = $_POST["name"];
        //身份证正则表达式(15位)
        $isIDCard1 = "/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/";
        //身份证正则表达式(18位)
        $isIDCard2 = "/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/";
        if (!isset($name) && !empty($name) && (mb_strlen($name, 'UTF-8')) < 5) {
            exit(json_encode(array("Status" => "true", "Content" => "姓名长度不正确")));
        }
        if ($identity && (preg_match($isIDCard1, $identity, $identitys) || preg_match($isIDCard2, $identity, $identitys))) {

            $AppID = Yaconf::get("TaskYun.AliPay");
            $aop = new AopClient;
            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
            $aop->appId = $AppID['AppID'];
            $aop->rsaPrivateKey = $AppID['PrivateKey'];
            $aop->format = "json";
            $aop->charset = "UTF-8";
            $aop->signType = "RSA2";
            $aop->alipayrsaPublicKey = $AppID['PublicKey'];


            $request = new ZhimaCustomerCertificationInitializeRequest ();
            $bizcontent = json_encode([
                'transaction_id' => $this->trade_no(),
                'product_code' => "w1010100000000002978",
                'biz_code' => "FACE",
                'identity_param' => array("identity_type" => "CERT_INFO", "cert_type" => "IDENTITY_CARD", "cert_name" => "$name", "cert_no" => "$identity"),//保留两位小数
            ]);
            $request->setBizContent($bizcontent);
            $result = $aop->execute($request);
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
            if (!empty($resultCode) && $resultCode == 10000) {
                $this->biz_no = $result->$responseNode->biz_no;

                $request = new ZhimaCustomerCertificationCertifyRequest();
                $content = json_encode([
                    'biz_no' => $this->biz_no,
                ]);
                //开启Yaf_Session
                $Session = Yaf_Session::getInstance();
                $Session->biz_no = $this->biz_no;
                $request->setBizContent($content);
                $Common = $aop->pageExecute($request, "GET");
                ReturnEcho("true", $Common);
            } else {
                echo "失败";
            }
        } else {
            exit(json_encode(array("Status" => "true", "Content" => "身份证号码有误!")));
        }

    }


    //芝麻认证开始
    public function StartVerifyAction($name = "Stringer")
    {
        $UserID = GetAppUserID();
        if ($UserID) {
            $Path = Yaconf::get("TaskYun.Home") . "AppButll/library/AliPay/";
            require_once $Path . "AopClient.php";
            require_once $Path . "request/ZhimaCustomerCertificationQueryRequest.php";
            require_once $Path . "SignData.php";
            $AppID = Yaconf::get("TaskYun.AliPay");
            $aop = new AopClient;
            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
            $aop->appId = $AppID['AppID'];
            $aop->rsaPrivateKey = $AppID['PrivateKey'];
            $aop->format = "json";
            $aop->charset = "UTF-8";
            $aop->signType = "RSA2";
            $aop->alipayrsaPublicKey = $AppID['PublicKey'];

            $request = new ZhimaCustomerCertificationQueryRequest();
            $Session = Yaf_Session::getInstance();
            $content = json_encode([
                //'biz_no' => "ZM201708253000000525200651194415",
                'biz_no' =>  $Session->biz_no,
            ]);
            $request->setBizContent($content);
            $result = $aop->execute($request);
            // 清除相关数据

            //unset($Session->biz_no);
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $data=$result->$responseNode;

            $resultCode = $data->code;
            if (!empty($resultCode) && $resultCode == 10000) {
                $UserInfo=json_decode($data->identity_info,true);
                $UserName=$UserInfo['cert_name'];
                $LicenseID=$UserInfo['cert_no'];
                $this->UpdateData($UserID, $UserName, $LicenseID);
                ReturnEcho("true",1);
            } else {
                ReturnEcho("true",2);
            }
        } else {
            ReturnEcho(206);
        }

    }

    //实名认证写入数据库
    public function UpdateData($UserID, $UserName, $LicenseID)
    {
        $YunHash = new MySqlModel("SELECT `UserID`,`UserKey` FROM `YunHash`WHERE `UserID` = $UserID");
        $YunHash = $YunHash->WrOneAssoc();

        if ($YunHash['UserID']) {
            $UserKey = $YunHash['UserKey'];
            $LicenseID = MDEncryption($LicenseID, $UserKey);
            $UserName = MDEncryption($UserName, $UserKey);
            $NewUpdate = new MySqlModel("UPDATE `YunHash` SET `UserName` = '<{[Username]}>',`LicenseID`='<{[LicenseID]}>',`ProofWay`='2' WHERE `UserID` = '$UserID'", array(
                "Username" => $UserName,
                "LicenseID" => $LicenseID
            ));
            $result = $NewUpdate->WrRrows();
        } else {
            ReturnEcho(0);
        }
    }


//生成唯一订单号
    public function trade_no()
    {
        list($usec, $sec) = explode(" ", microtime());
        $usec = substr(str_replace('0.', '', $usec), 0, 4);
        $str = rand(10, 99);
        return date("YmdHis") . $usec . $str;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值