'', //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,$firm_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('wps.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=SignMember::where('m_id',$m_id)->where('firm_id',$firm_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']; $member=Member::where('id',$m_id)->select(['phone','promissory_name'])->first(); $content='尊敬的'.$member['promissory_name'].',您有待认证信息,点击认证 '.$authShortUrl; SmsServer::creatServer()->sendContent($member['phone'],$content); //存入数据库 $signMember->update([ 'authUrl'=>$authUrl, 'authShortUrl'=>$authShortUrl, 'authFlowId'=>$authFlowId, ]); } return true; }else{//认证授权错误 return false; } } /** * 查询个人认证信息 * @param $m_id //用户id * @param $firm_id //企业id * @param $firm_name //企业名称 * @param $phone //手机号 * @param $psnName //认证人姓名 * @param $id_number //认证人身份证号 * @return SignMember|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null */ function personsidentityinfo($m_id,$firm_id,$firm_name,$phone,$psnName,$id_number) { $info = SignMember::where('id_number',$id_number)->where('firm_id',$firm_id)->where('authType','PSN')->first(); if (empty($info)){ //入库 SignMember::create([ 'm_id' => $m_id, 'firm_id' => $firm_id, 'firm_name' => $firm_name, 'phone' => $phone, 'name' => $psnName, 'id_number' => $id_number, 'realnameStatus'=>0, 'authorizeUserInfo'=>0, 'psnId'=>'', 'authShortUrl'=>'', 'authType' => 'PSN', ]); }else{ 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']; //更新认证信息 SignMember::where('id_number',$id_number)->where('firm_id',$firm_id)->where('authType','PSN')->update([ 'realnameStatus'=>$realnameStatus, 'authorizeUserInfo'=>$authorizeUserInfo, 'psnId'=>$psnId, ]); if ($realnameStatus == 1){//更新用户认证状态 MemberServer::creatServer()->updateMemberStatus($m_id,$firm_id); } if (empty($realnameStatus) || empty($authorizeUserInfo)){//走个人认证授权流程 $this->psnauthurl($m_id,$firm_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,$firm_id,$phone,$psnName,$id_number); } return $realnameStatus; } /** * 查询企业授权详情 * @param $orgId * @return \App\Servers\ESign\comm\EsignResponse */ function organizationsAuthorizedInfo($orgId){ $config=$this->config ; $apiaddr="/v3/organizations/".$orgId."/authorized-info"; $requestType = HttpEmun::GET; $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], "", $requestType, $apiaddr); //发起接口请求 EsignLogHelper::printMsg($signAndBuildSignAndJsonHeader); $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, ""); EsignLogHelper::printMsg($response->getStatus()); EsignLogHelper::printMsg($response->getBody()); return $response; } /** * 查询企业实名状态 * @param $orgName * @return mixed */ function organizationsIdentityInfo($orgName){ $config=$this->config; $apiaddr="/v3/organizations/identity-info?orgName=".$orgName; $requestType = HttpEmun::GET; $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], "", $requestType, $apiaddr); //发起接口请求 // EsignLogHelper::printMsg($signAndBuildSignAndJsonHeader); $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, ""); // EsignLogHelper::writeLog($response->getStatus()); // EsignLogHelper::printMsg($response->getBody()); $ret=json_decode($response->getBody(),true); if(isset($ret['code']) && $ret['code']==0){ //realnameStatus 0 - 未实名,1 - 已实名 return $ret['data']; }else{ return ['realnameStatus'=>-1,'orgId'=>'','authorizeUserInfo'=>false,'orgName'=>$orgName]; } } /** * 获取机构认证&授权页面链接 * @param $orgName */ function orgAuthUrl($orgName){ $config=$this->config; $apiaddr="/v3/org-auth-url"; $requestType = HttpEmun::POST; $data=[ "orgAuthConfig"=>[ "orgName"=>$orgName, "orgInfo"=>[ "orgIDCardNum"=>"", "orgIDCardType"=>"CRED_ORG_USCC" ], "transactorInfo"=>[ "psnAccount"=>"", "psnInfo"=>[ "psnIDCardNum"=>"", "psnName"=>"" ] ], ], "redirectConfig"=>[ "redirectUrl"=>"https://www.baidu.com/" ], "notifyUrl"=>"", "clientType"=>"ALL", "appScheme"=>"" ]; $paramStr = json_encode($data); //生成签名验签+json体的header $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], $paramStr, $requestType, $apiaddr); //发起接口请求 // EsignLogHelper::printMsg($signAndBuildSignAndJsonHeader); $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, $paramStr); // EsignLogHelper::printMsg($response->getStatus()); // EsignLogHelper::printMsg($response->getBody()); } /** * 企业授权 * @param $orgName */ function orgAuthorizehUrl($orgName){ $config=$this->config; $apiaddr="/v3/org-auth-url"; $requestType = HttpEmun::POST; $data=[ "orgAuthConfig"=>[ "orgName"=>$orgName, "orgInfo"=>[ "orgIDCardNum"=>"", "orgIDCardType"=>" " ], "transactorInfo"=>[ "psnAccount"=>"", "psnInfo"=>[ "psnIDCardNum"=>"", "psnName"=>"" ] ], ], "authorizeConfig"=>[ "authorizedScopes"=>[ "get_org_identity_info","get_psn_identity_info","org_initiate_sign","psn_initiate_sign","manage_org_resource","manage_psn_resource","use_org_order" ] ], "redirectConfig"=>[ "redirectUrl"=>"https://www.baidu.com/" ], "notifyUrl"=>"", "clientType"=>"ALL", "appScheme"=>"" ]; $paramStr = json_encode($data); //生成签名验签+json体的header $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], $paramStr, $requestType, $apiaddr); //发起接口请求 EsignLogHelper::printMsg($signAndBuildSignAndJsonHeader); $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, $paramStr); EsignLogHelper::printMsg($response->getStatus()); EsignLogHelper::printMsg($response->getBody()); } }