| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- namespace App\Servers\ESign;
- use App\Models\Firm;
- use App\Models\FirmSeal;
- use App\Models\Member;
- use App\Models\MemberSeal;
- use App\Models\SignContract;
- use App\Models\SignRecord;
- use App\Servers\ESign\comm\EsignHttpHelper;
- use App\Servers\ESign\comm\EsignLogHelper;
- use App\Servers\ESign\comm\EsignUtilHelper;
- use App\Servers\ESign\emun\HttpEmun;
- use App\Servers\System\CommonServer;
- use App\Servers\System\SmsServer;
- class ESignContractServer
- {
- 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 ESignContractServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) self::$server = new ESignContractServer();
- return self::$server;
- }
- function contentMd5($filePath){
- if(!file_exists($filePath)){
- EsignLogHelper::printMsg($filePath."文件不存在");exit;
- }
- return EsignUtilHelper::getContentBase64Md5($filePath);
- }
- /**
- * 上传合同文件至E签宝
- * @param $file_url
- * @return mixed
- */
- function fileUploadUrl($file_url){
- $config=$this->config;
- $file_name=CommonServer::creatServer()->getFileName($file_url);
- $filePath=public_path($file_url);
- $contentMd5=$this->contentMd5($filePath);
- $apiaddr="/v3/files/file-upload-url";
- $requestType = HttpEmun::POST;
- $data=[
- "contentMd5" =>$contentMd5,
- "contentType" =>"application/pdf",
- "convertToPDF" =>true,
- "fileName" =>$file_name,
- "fileSize" =>filesize($filePath)
- ];
- $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());
- $fileUploadUrl=json_decode($response->getBody())->data->fileUploadUrl;
- $fileId=json_decode($response->getBody())->data->fileId;
- //文件流put上传
- $response=EsignHttpHelper::upLoadFileHttp($fileUploadUrl,$filePath,"application/pdf");
- EsignLogHelper::printMsg($response->getStatus());
- EsignLogHelper::printMsg($response->getBody());
- return $fileId;
- }
- /**
- * 获取签署文件对应关键字坐标
- * @param $fileId
- * @param $keywords
- * @return array
- */
- function getKeywordPositions($fileId,$keywords)
- {
- $config=$this->config;
- $apiaddr="/v3/files/{$fileId}/keyword-positions";
- $requestType = HttpEmun::POST;
- $data=[
- "keywords" =>$keywords
- ];
- $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){
- return ['code'=>'1','msg'=>'ok','data'=>$ret['data']['keywordPositions']];
- }else{
- return ['code'=>'0','msg'=>$ret['message']];
- }
- }
- function setContractContract($sign_id,$fileId,$signJson,$file_name)
- {
- // $signJson = '[{"keyword":"fisher", "auth_type":"PSN", "id_no","AAAAA"}, {"keyword":"lily", "auth_type":"ORG", "id_no","AAAAA"}, {"keyword":"bob", "auth_type":"PSN", "id_no","AAAAA"}]';
- $signData = json_decode($signJson,true);
- $keywords=[];
- foreach ($signData as $k=>$v){
- $keywords[]=$v['keyword'];
- }
- //获取对应关键字定位
- $positionRet=ESignContractServer::creatServer()->getKeywordPositions($fileId,$keywords);
- if(empty($positionRet['code'])){
- return ['code'=>0,'msg'=>'签署区域定位失败'];
- }else{
- $positionData=$positionRet['data'];
- }
- $signers=[];
- foreach ($signData as $value){
- //对应关键字做xy坐标匹配
- foreach ($positionData as $item){
- if(count($item['positions'])<=0){
- continue;
- }
- if($item['keyword']==$value['keyword']){
- $position=end($item['positions']);
- $coordinate=end($position['coordinates']);
- $value['positionPage'] = $position['pageNum'];
- $value['positionX'] = $coordinate['positionX'];
- $value['positionY'] = $coordinate['positionY'];
- }
- }
- $signersData = [];
- //签署方是机构还是个人
- if ($value['auth_type'] == 'PSN'){
- $signerType = 0;
- $member = Member::where('psnId',$value['id_no'])->first();
- //获取企业签章
- $m_id = $member->{'id'};
- $seal=MemberSeal::where('m_id',$m_id)->select(['id','seal_id'])->first();
- //组装信息
- $signersData[] = [
- "psnSignerInfo" => [
- "psnAccount" => $member->{'phone'},
- 'psnInfo'=>[
- 'psnName'=>$member->{'name'},
- 'psnIDCardNum'=>$member->{'id_number'},
- 'psnIDCardType'=>'CRED_PSN_CH_IDCARD',
- ],
- ],
- ];
- }else{
- $signerType = 1;
- $firm = Firm::where('orgId',$value['id_no'])-first();
- //获取用户签章
- $firm_id = $firm->{'id'};
- $seal=FirmSeal::where('firm_id',$firm_id)->where('seal_type','CONTRACT')->select(['id','seal_id'])->first();
- $signersData[] = [
- "orgSignerInfo" => [
- "orgName" => $firm->{'firm_name'},
- 'orgInfo'=>[
- 'orgIDCardNum'=>$firm->{'license_number'},
- 'orgIDCardType'=>'CRED_ORG_USCC',
- ],
- "transactorInfo"=>[
- "psnAccount"=>$firm['phone'],
- 'psnInfo'=>[
- 'psnIDCardNum'=>$firm['legal_id_number'],
- 'psnIDCardType'=>'CRED_PSN_CH_IDCARD'
- ]
- ]
- ],
- ];
- }
- //印章ID
- $assignedSealId=$seal->{'seal_id'};
- $signersData[] = [
- "signFields" =>[
- [
- "fileId" =>$fileId,
- "normalSignFieldConfig" => [
- "autoSign" =>false,
- "signFieldStyle" =>1,
- "assignedSealId"=>$assignedSealId, // 在signFields中设置 assignedSealId为授权企业印章id
- "signFieldPosition" => [
- "positionPage" =>$value['pageNum'].'',
- "positionX" =>(int)$value['positionX'],
- "positionY" =>(int)$value['positionY'],
- ]
- ]
- ]
- ],
- "signerType" =>$signerType
- ];
- $signers[]=$signersData;
- //对应签署方做记录
- SignRecord::create([
- 'sign_id' => $sign_id,
- 'authType' => $value['auth_type'],
- 'id_no' => $value['id_no'],
- 'keyword' => $value['keyword'],
- 'page_num' => $value['pageNum'],
- 'position_x' => $value['positionX'],
- 'position_y' => $value['positionY'],
- ]);
- }
- if(empty($signers)){
- return ['code'=>0,'msg'=>'暂无可执行电签印章'];
- }
- $flowId= $this->createByFile($fileId,$file_name.'.pdf',$file_name,$signers);
- if($flowId){
- SignContract::where('id',$sign_id)->update(['flow_id'=>$flowId]);
- return ['code'=>1,'msg'=>'发起签署成功'];
- }else{
- return ['code'=>0,'msg'=>'发起签署失败'];
- }
- }
- /**
- * 合同签署
- * @param $fileId
- * @return false
- */
- function createByFile($fileId,$fileName,$signFlowTitle,$signers)
- {
- EsignLogHelper::printMsg("**********基于文件发起签署调用开始**********");
- $config=$this->config;
- $apiaddr="/v3/sign-flow/create-by-file";
- $requestType = HttpEmun::POST;
- $data = [
- "docs" =>[
- [
- "fileId" =>$fileId,
- "fileName" =>$fileName
- ]
- ],
- "signFlowConfig" => [
- "signFlowTitle" =>$signFlowTitle,
- "autoStart"=>true,
- "autoFinish"=>true,
- 'notifyUrl'=>route('notify.esignNotify')
- ],
- "signers"=>$signers
- ];
- // dd($data);
- $paramStr = json_encode($data);
- $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());
- $signFlowId = false;
- if($response->getStatus() == 200){
- $result = json_decode($response->getBody());
- if($result->code==0){
- $signFlowId = $result->data->signFlowId;
- EsignLogHelper::printMsg("基于文件发起签署接口调用成功,flowId: ".$signFlowId);
- }else{
- EsignLogHelper::printMsg("基于文件发起签署接口调用失败,错误信息: ".$result->message);
- }
- }else{
- EsignLogHelper::printMsg("基于文件发起签署接口调用失败,HTTP错误码".$response->getStatus());
- }
- EsignLogHelper::printMsg("**********基于文件发起签署调用结束**********");
- return $signFlowId;
- }
- }
|