|
|
@@ -0,0 +1,410 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+
|
|
|
+namespace App\Servers\ESign;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+use App\Models\Firm;
|
|
|
+use App\Models\FirmSeal;
|
|
|
+use App\Models\Member;
|
|
|
+use App\Servers\ESign\comm\EsignHttpHelper;
|
|
|
+use App\Servers\ESign\comm\EsignLogHelper;
|
|
|
+use App\Servers\ESign\emun\HttpEmun;
|
|
|
+use App\Servers\System\CommonServer;
|
|
|
+use App\Servers\System\JobServer;
|
|
|
+use App\Servers\System\RedisDataServer;
|
|
|
+use App\Servers\System\SmsServer;
|
|
|
+
|
|
|
+class ESignFirmServer
|
|
|
+{
|
|
|
+ static private $server = null;
|
|
|
+
|
|
|
+ public $config = [
|
|
|
+ 'eSignAppId'=>'', //app id
|
|
|
+ 'eSignAppSecret'=>'', // app key
|
|
|
+ 'eSignHost'=>'https://smlopenapi.esign.cn', //模拟环境
|
|
|
+ // 'url'=>'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 ESignFirmServer
|
|
|
+ */
|
|
|
+ static function creatServer()
|
|
|
+ {
|
|
|
+ if (empty(self::$server)) self::$server = new ESignFirmServer();
|
|
|
+ return self::$server;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询企业授权详情
|
|
|
+ * @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($firm_id,$orgName){
|
|
|
+ $info = Firm::where('id',$firm_id)->where('authType','ORG')->select(['realnameStatus','authorizeUserInfo','orgId'])->first();
|
|
|
+ if($info->{'realnameStatus'}==1 && $info->{'authorizeUserInfo'} == 1 && $info->{'orgId'}!=''){
|
|
|
+ return $info->{'realnameStatus'};
|
|
|
+ }
|
|
|
+
|
|
|
+ $config=$this->config;
|
|
|
+ $firm=Firm::where('id',$firm_id)->select(['id','license_number','firm_name'])->first();
|
|
|
+ $apiaddr="/v3/organizations/identity-info?orgIDCardNum=".$firm['license_number'].'&orgIDCardType=CRED_ORG_USCC';
|
|
|
+ $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 - 已实名
|
|
|
+ $this->setSignFirm($ret['data']);
|
|
|
+
|
|
|
+ //realnameStatus 0 - 未实名,1 - 已实名
|
|
|
+ $realnameStatus = $ret['data']['realnameStatus'];
|
|
|
+ //true - 已授权,false - 未授权
|
|
|
+ $authorizeUserInfo = $ret['data']['authorizeUserInfo'] ? 1 : 0;
|
|
|
+ //个人账号ID
|
|
|
+ $orgId = $ret['data']['orgId'];
|
|
|
+ //更新认证信息
|
|
|
+ Firm::where('id',$firm_id)->where('authType','ORG')->update([
|
|
|
+ 'realnameStatus'=>$realnameStatus,
|
|
|
+ 'authorizeUserInfo'=>$authorizeUserInfo,
|
|
|
+ 'orgId'=>$orgId,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if (empty($realnameStatus) || empty($authorizeUserInfo)){//走认证授权流程
|
|
|
+ $this->orgAuthUrl($firm_id,$orgName);
|
|
|
+ }
|
|
|
+ }else{//证明从来没有在e签宝注册过
|
|
|
+ //realnameStatus 0 - 未实名,1 - 已实名
|
|
|
+ $realnameStatus = 0;
|
|
|
+ //true - 已授权,false - 未授权
|
|
|
+ $authorizeUserInfo = 0;
|
|
|
+ //个人账号ID
|
|
|
+ $orgId = '';
|
|
|
+ //走认证授权流程
|
|
|
+ $this->orgAuthUrl($firm_id,$orgName);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $realnameStatus;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 企业实名
|
|
|
+ * @param $orgName
|
|
|
+ */
|
|
|
+ function orgAuthUrl($firm_id,$orgName){
|
|
|
+ $authShortUrl=RedisDataServer::creatServer()->getData($firm_id.'_firmUrl');
|
|
|
+ if($authShortUrl){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ $config=$this->config;
|
|
|
+ $apiaddr="/v3/org-auth-url";
|
|
|
+ $requestType = HttpEmun::POST;
|
|
|
+ $firm=Firm::where('id',$firm_id)->first();
|
|
|
+ $data=[
|
|
|
+ "orgAuthConfig"=>[
|
|
|
+ "orgName"=>$orgName,
|
|
|
+ "orgInfo"=>[
|
|
|
+ "orgIDCardNum"=>$firm['license_number'],
|
|
|
+ "legalRepName"=>$firm['legal_name'],
|
|
|
+ "orgIDCardType"=>"CRED_ORG_USCC",
|
|
|
+
|
|
|
+ ],
|
|
|
+ "transactorInfo"=>[
|
|
|
+ "psnAccount"=>$firm['phone'],
|
|
|
+ "psnInfo"=>[
|
|
|
+ "psnIDCardNum"=>$firm['legal_id_number'],
|
|
|
+ "psnName"=>$firm['legal_name'],
|
|
|
+ "psnIDCardType"=>"CRED_PSN_CH_IDCARD"
|
|
|
+ ]
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ "authorizeConfig"=>[
|
|
|
+ "authorizedScopes"=>[
|
|
|
+ "get_org_identity_info","get_psn_identity_info","org_initiate_sign","psn_initiate_sign","manage_org_resource","manage_psn_resource","psn_sign_file_storage","org_sign_file_storage","org_approval_info","use_org_order"
|
|
|
+ ]
|
|
|
+ ],
|
|
|
+ "notifyUrl"=>route('adminApi.notify.esignNotify'),
|
|
|
+ "clientType"=>"ALL",
|
|
|
+ "appScheme"=>""
|
|
|
+ ];
|
|
|
+ if($firm['auth_bank']){
|
|
|
+ $data['orgAuthConfig']['orgInfo']['orgBankAccountNum']=$firm['auth_bank'];
|
|
|
+ $data['orgAuthConfig']['orgAuthPageConfig']['orgDefaultAuthMode']='ORG_BANK_TRANSFER';
|
|
|
+ $data['orgAuthConfig']['orgAuthPageConfig']['orgAvailableAuthModes']=['ORG_BANK_TRANSFER'];
|
|
|
+ }else{
|
|
|
+ $data['orgAuthConfig']['orgInfo']['legalRepIDCardNum']=$firm['legal_id_number'];
|
|
|
+ $data['orgAuthConfig']['orgInfo']['legalRepIDCardType']='CRED_PSN_CH_IDCARD';
|
|
|
+ }
|
|
|
+ $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());
|
|
|
+
|
|
|
+ $ret=json_decode($response->getBody(),true);
|
|
|
+ if(isset($ret['code']) && $ret['code']==0){
|
|
|
+ $authShortUrl=RedisDataServer::creatServer()->getData($firm_id.'_firmUrl');
|
|
|
+ if($authShortUrl){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ $authShortUrl = $ret['data']['authShortUrl'];
|
|
|
+ RedisDataServer::creatServer()->setData($firm_id.'_firmUrl',$authShortUrl,'str',600);
|
|
|
+ $authUrl = $ret['data']['authUrl'];
|
|
|
+ $authFlowId = $ret['data']['authFlowId'];
|
|
|
+
|
|
|
+ $content='尊敬的'.$firm['legal_name'].',您有关联的企业信息待认证,点击认证 '.$authShortUrl;
|
|
|
+ SmsServer::creatServer()->sendContent($firm['phone'],$content);
|
|
|
+
|
|
|
+ //存入数据库
|
|
|
+ Firm::where('id',$firm_id)->update([
|
|
|
+ 'authUrl'=>$authUrl,
|
|
|
+ 'authShortUrl'=>$authShortUrl,
|
|
|
+ 'authFlowId'=>$authFlowId,
|
|
|
+ ]);
|
|
|
+ return true;
|
|
|
+ }else{//认证授权错误
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 企业授权
|
|
|
+ * @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"
|
|
|
+ ]
|
|
|
+ ],
|
|
|
+ "notifyUrl"=>route('notify.esignNotify'),
|
|
|
+ "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 $firmId
|
|
|
+ * @param $orgId
|
|
|
+ * @param $pageNum
|
|
|
+ * @return array|string[]
|
|
|
+ */
|
|
|
+ function internalAuthInfo($firmId,$orgId,$pageNum=1){
|
|
|
+
|
|
|
+ $config=$this->config;
|
|
|
+ $apiaddr="/v3/seals/org-own-seal-list?orgId=".$orgId."&pageNum={$pageNum}&pageSize=20";
|
|
|
+ $requestType = HttpEmun::GET;
|
|
|
+ //生成签名验签+json体的header
|
|
|
+ $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){
|
|
|
+ //存入签章信息
|
|
|
+ $this->setSealList($firmId,$ret['data']['seals']);
|
|
|
+ return ['code'=>'1','msg'=>'ok','data'=>['total'=>$ret['data']['total'],'items'=>$ret['data']['seals']]];
|
|
|
+ }else{
|
|
|
+ return ['code'=>'0','msg'=>$ret['message']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置企业签章信息
|
|
|
+ * @param $firmId
|
|
|
+ * @param $list
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function setSealList($firmId,$list)
|
|
|
+ {
|
|
|
+ if($list){
|
|
|
+ foreach ($list as $item){
|
|
|
+ $sealRow=[
|
|
|
+ 'firm_id'=>$firmId,
|
|
|
+ 'seal_id'=>$item['sealId'],
|
|
|
+ 'seal_name'=>$item['sealName'],
|
|
|
+ 'seal_type'=>$item['sealBizType'],
|
|
|
+ 'seal_status'=>$item['sealStatus'],
|
|
|
+ 'seal_url'=>$item['sealImageDownloadUrl'],
|
|
|
+ ];
|
|
|
+ $sealModel=FirmSeal::where('firm_id',$firmId)->where('seal_id',$item['sealId'])->first();
|
|
|
+ if(empty($sealModel)){
|
|
|
+ $sealModel=FirmSeal::create($sealRow);
|
|
|
+ }else{
|
|
|
+ $sealModel->update($sealRow);
|
|
|
+ }
|
|
|
+ $this->setSealImg($sealModel);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保持印章图片
|
|
|
+ * @param FirmSeal $firmSeal
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ function setSealImg(FirmSeal $firmSeal)
|
|
|
+ {
|
|
|
+ $path=public_path('storage/seal/'.date('Ymd').'/');
|
|
|
+ if(!is_dir($path)){
|
|
|
+ mkdir($path,0777,true);
|
|
|
+ }
|
|
|
+ $name=$firmSeal['seal_id'].'.png';
|
|
|
+ file_put_contents($path.$name,file_get_contents($firmSeal['seal_url']));
|
|
|
+ $url=env('APP_URL').'/storage/seal/'.date('Ymd').'/'.$name;
|
|
|
+ $firmSeal->update(['seal_url'=>$url]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建企业合同印章
|
|
|
+ * @param $firmId
|
|
|
+ * @param $orgId
|
|
|
+ * @return string[]|void
|
|
|
+ */
|
|
|
+ function createOrgsealsbyTemplate($firmId,$orgId){
|
|
|
+ $sealNum=FirmSeal::where('firm_id',$firmId)->where('seal_type','CONTRACT')->count();
|
|
|
+ if($sealNum>0){
|
|
|
+ return ['code'=>'0','msg'=>'合同印章已创建'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $license_number=Firm::where('id',$firmId)->value('license_number');
|
|
|
+ $config=$this->config;
|
|
|
+ $apiaddr="/v3/seals/org-seals/create-by-template";
|
|
|
+ $requestType = HttpEmun::POST;
|
|
|
+//生成签名验签+json体的header
|
|
|
+ $data=[
|
|
|
+ "orgId"=>$orgId,
|
|
|
+ "sealName"=>"合同印章",//机构印章名称(用户自定义,且名称不可重复)
|
|
|
+ "sealTemplateStyle"=>"CONTRACT_ROUND_STAR",//机构模板印章样式
|
|
|
+ "sealSize"=>"38_38",
|
|
|
+ "sealColor"=>"RED",//机构印章颜色,默认是红色
|
|
|
+ "sealHorizontalText"=>"合同专用章",//自定义印章横向文,示例值:XX专用章
|
|
|
+ "sealBottomText" =>$license_number,//印章下弦文(实体印章防伪码),示例值:9133********1
|
|
|
+ "sealOpacity" =>80
|
|
|
+ ];
|
|
|
+ $paramStr = json_encode($data);
|
|
|
+ //请求参数
|
|
|
+ // EsignLogHelper::printMsg($paramStr);
|
|
|
+ $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], $paramStr, $requestType, $apiaddr);
|
|
|
+ //发起接口请求头
|
|
|
+// EsignLogHelper::printMsg($signAndBuildSignAndJsonHeader);
|
|
|
+ $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, $paramStr);
|
|
|
+ $ret=json_decode($response->getBody(),true);
|
|
|
+ if(isset($ret['code']) && $ret['code']==0){
|
|
|
+ //realnameStatus 0 - 未实名,1 - 已实名
|
|
|
+ FirmSeal::create([
|
|
|
+ 'firm_id' => $firmId,
|
|
|
+ 'seal_id' => $ret['data']['sealId'],
|
|
|
+ 'seal_name' => "合同印章",
|
|
|
+ 'seal_type' => 'CONTRACT'
|
|
|
+ ]);
|
|
|
+ //获取完成印章以后重新拉取一下印章信息
|
|
|
+ $this->internalAuthInfo($firmId,$orgId);
|
|
|
+ return ['code'=>'1','msg'=>'ok'];
|
|
|
+ }else{
|
|
|
+ return ['code'=>'0','msg'=>$ret['message']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加企业机构成员
|
|
|
+ * @param $orgId
|
|
|
+ * @param $psnId
|
|
|
+ * @param $name
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ function addMembers($orgId,$psnId,$name){
|
|
|
+ $config=$this->config;
|
|
|
+ $apiaddr="/v3/organizations/".$orgId."/members";
|
|
|
+ $requestType = HttpEmun::POST;
|
|
|
+ //生成签名验签+json体的header
|
|
|
+ $data=[
|
|
|
+ "members"=>[
|
|
|
+ [
|
|
|
+ "psnId"=>$psnId,
|
|
|
+ "memberName"=>$name,//员工昵称,用于区分企业成员
|
|
|
+ 'role'=>'4'
|
|
|
+ ]
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ $paramStr = json_encode($data);
|
|
|
+ EsignLogHelper::writeLog($paramStr);
|
|
|
+ $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], $paramStr, $requestType, $apiaddr);
|
|
|
+ //发起接口请求
|
|
|
+ //发起接口请求
|
|
|
+// EsignLogHelper::printMsg($signAndBuildSignAndJsonHeader);
|
|
|
+ $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, $paramStr);
|
|
|
+ EsignLogHelper::writeLog($response->getStatus());
|
|
|
+ EsignLogHelper::writeLog($response->getBody());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|