'', //app id 'eSignAppSecret'=>'', // app key // 'eSignHost'=>'https://smlopenapi.esign.cn', //模拟环境 'eSignHost'=>'https://openapi.esign.cn' //正式环境 ]; function __construct() { $this->config['eSignAppId']=env('E_SIGN_APP_ID'); $this->config['eSignAppSecret']=env('E_SIGN_APP_SECRET'); $this->config['eSignPublicKey']=env('E_SIGN_PUBLIC_KEY'); $this->config['eSignHost']=env('E_SIGN_HOST'); } /** * 创建对象 * @return ESignServer */ static function creatServer() { if (empty(self::$server)) self::$server = new ESignServer(); return self::$server; } /** * @param $expireTime * @return float */ function expireTime($expireTime){ list($t1, $t2) = explode(' ', microtime()); $nowTime=(float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000); $num=$expireTime-$nowTime; return $num; } /** * 获取个人认证&授权页面链接 * @return bool */ function psnauthurl($m_id,$phone,$psnName,$id_number,$psnId='') { $authShortUrl=RedisDataServer::creatServer()->getData($m_id.'_memberUrl'); if($authShortUrl){ return true; } $config=$this->config; $apiaddr="/v3/psn-auth-url"; $requestType = HttpEmun::POST; $data=[ "psnAuthConfig"=>[ "psnAccount"=>$phone ?? '', "psnId"=>$psnId, "psnInfo" => [ "psnName" => $psnName, "psnIDCardNum" => $id_number, "psnIDCardType" => "CRED_PSN_CH_IDCARD", ], ], "authorizeConfig" =>[ "authorizedScopes" => [ "get_psn_identity_info","psn_initiate_sign","manage_psn_resource","psn_sign_file_storage" ], ], "redirectConfig"=>[ "redirectUrl"=>'', ], "notifyUrl"=>route('notify.esignNotify'), "clientType"=>"ALL", "appScheme"=>"" ]; $paramStr = json_encode($data); //生成签名验签+json体的header $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], $paramStr, $requestType, $apiaddr); //发起接口请求 $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, $paramStr); $ret=json_decode($response->getBody(),true); if(isset($ret['code']) && $ret['code']==0){ $authShortUrl=RedisDataServer::creatServer()->getData($m_id.'_memberUrl'); if($authShortUrl){ return true; } $signMember=Member::where('m_id',$m_id)->where('authType','PSN')->where([['updated_at','<=',date('Y-m-d H:i:s',time()-600),'or'],['authShortUrl','=','','or']])->first(); if($signMember){ $authUrl = $ret['data']['authUrl']; $authShortUrl = $ret['data']['authShortUrl']; RedisDataServer::creatServer()->setData($m_id.'_memberUrl',$authShortUrl,'str',600); $authFlowId = $ret['data']['authFlowId']; $content='尊敬的'.$signMember['name'].',您有待认证信息,点击认证 '.$authShortUrl; SmsServer::creatServer()->sendContent($signMember['phone'],$content); //存入数据库 $signMember->update([ 'authUrl'=>$authUrl, 'authShortUrl'=>$authShortUrl, 'authFlowId'=>$authFlowId, ]); } return true; }else{//认证授权错误 return false; } } /** * 查询个人认证信息 * @param $phone //手机号 * @param $psnName //认证人姓名 * @param $id_number //认证人身份证号 * @return SignMember|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null */ function personsidentityinfo($m_id,$phone,$psnName,$id_number) { $info = Member::where('id_number',$id_number)->where('id',$m_id)->where('authType','PSN')->first(); if($info->{'realnameStatus'}==1 && $info->{'psnId'}!=''){ return $info->{'realnameStatus'}; } $config=$this->config; $apiaddr="/v3/persons/identity-info?psnIDCardNum=".$id_number."&psnIDCardType=CRED_PSN_CH_IDCARD"; $requestType = HttpEmun::GET; $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], "", $requestType, $apiaddr); //发起接口请求 $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, ""); $ret=json_decode($response->getBody(),true); if(isset($ret['code']) && $ret['code']==0){ //realnameStatus 0 - 未实名,1 - 已实名 $realnameStatus = $ret['data']['realnameStatus']; //true - 已授权,false - 未授权 $authorizeUserInfo = $ret['data']['authorizeUserInfo'] ? 1 : 0; //个人账号ID $psnId = $ret['data']['psnId']; //更新认证信息 Member::where('id_number',$id_number)->update([ 'realnameStatus'=>$realnameStatus, 'authorizeUserInfo'=>$authorizeUserInfo, 'psnId'=>$psnId, ]); if (empty($realnameStatus) || empty($authorizeUserInfo)){//走个人认证授权流程 $this->psnauthurl($m_id,$phone,$psnName,$id_number,$psnId); } //未授权也注销 if(empty($authorizeUserInfo))$realnameStatus=0; }else{//证明从来没有在e签宝注册过 //realnameStatus 0 - 未实名,1 - 已实名 $realnameStatus = 0; //true - 已授权,false - 未授权 $authorizeUserInfo = 0; //个人账号ID $psnId = ''; //走认证授权流程 $this->psnauthurl($m_id,$phone,$psnName,$id_number); } return $realnameStatus; } }