panghu 1 tahun lalu
induk
melakukan
a0b5499d36

+ 485 - 0
app/Servers/ESign/ESignContractServer.php

@@ -0,0 +1,485 @@
+<?php
+
+
+namespace App\Servers\ESign;
+
+
+
+use App\Models\FactoringContract;
+use App\Models\FactoringContractFile;
+use App\Models\FactoringContractSign;
+use App\Models\FactoringContractText;
+use App\Models\FactoringProperty;
+use App\Models\Firm;
+use App\Models\Member;
+use App\Models\SignContract;
+use App\Models\SignFirm;
+use App\Models\SignMember;
+use App\Models\SignSeal;
+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\Financing\ContractServer;
+use App\Servers\Financing\FinancingServer;
+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 FactoringContractFile $file
+     * @return mixed
+     */
+    function fileUploadUrl(FactoringContractFile $file){
+        $config=$this->config;
+        $file_pdf=$file->{'file_pdf'};
+        $app_url=env('APP_URL');
+        $file_pdf=str_replace($app_url,'',$file_pdf);
+        $file_name=CommonServer::creatServer()->getFileName($file_pdf);
+        $filePath=public_path($file_pdf);
+        $contentMd5=$this->contentMd5($filePath);
+        $apiaddr="/v3/files/file-upload-url";
+        $requestType = HttpEmun::POST;
+        $data=[
+            "contentMd5" =>$contentMd5,
+            "contentType" =>"application/pdf",
+            "convertToPDF" =>false,
+            "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());
+        $file->update(['sign_file_id'=>$fileId]);
+        return $fileId;
+    }
+
+
+    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 getFileStatus($fileId)
+    {
+        $config=$this->config;
+        $apiaddr="/v3/files/{$fileId}";
+        $requestType = HttpEmun::GET;
+        //生成签名验签+json体的header
+        $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());
+        $ret=json_decode($response->getBody(),true);
+        if(isset($ret['code']) && $ret['code']==0){
+            return ['code'=>'1','msg'=>'ok','data'=>$ret['data']];
+        }else{
+            return ['code'=>'0','msg'=>$ret['message']];
+        }
+    }
+
+    /**
+     * 生成待电签记录
+     * @param $contractId
+     * @return void
+     */
+    function addSignContract($contractId)
+    {
+        $num=SignContract::where('contract_id',$contractId)->count();
+        if($num<=0){
+            SignContract::create(['contract_id'=>$contractId,'status' => 0,'file_id'=>'','msg'=>'']);
+        }
+    }
+
+    function setContractContract()
+    {
+        $list=SignContract::where('status',0)->limit(10)->get();
+        foreach ($list as $k=>$v){
+            $contract=FactoringContract::where('id',$v->{'contract_id'})->select(['id','file_no','name','status','signatory','property','factoring_id','firm_id','factor_id'])->first();
+            if(empty($contract)){
+                $v->update(['status' => 3,'msg'=>'合同信息不存在']);
+            }else{
+                $file=FactoringContractFile::where('file_no',$contract->{'file_no'})->where('type',2)->orderBy('id','desc')->select(['id','file_pdf','file_name','file_no','file_path','version'])->first();
+                if(empty($file)){
+                    sleep(1);
+                    continue;
+                }
+                if(empty($v['file_id'])){
+                    //上传E签宝文件模板
+                    $fileId=$this->fileUploadUrl($file);
+                    if(empty($fileId)){
+                        $v->update(['status' => 3,'msg'=>'合同模板上传失败']);
+                    }else{
+                        $v->update(['file_id' => $fileId,'contract_file_id'=>$file['id']]);
+                    }
+                    $file->update(['sign_file_id'=>$fileId]);
+                }else{
+                    $fileId=$v['file_id'];
+                }
+                //校验盖章区域
+                $signatory=$contract->{'signatory'};
+                $keywords=[];
+                if(in_array(1,$signatory)){
+                    $keywords[]=FactoringContractText::where('id',2)->value('custom');
+                }
+                if(in_array(5,$signatory)){
+                    $keywords[]=FactoringContractText::where('id',3)->value('custom');
+                }
+                if(empty($keywords)){
+                    $v->update(['status' => 3,'msg'=>'无需要电签']);
+                    continue;
+                }
+                $positionRet=ESignContractServer::creatServer()->getKeywordPositions($fileId,$keywords);
+                if(empty($positionRet['code'])){
+                    $v->update(['status' => 3,'msg'=>'签署区域定位失败']);
+                    continue;
+                }else{
+                    $positionData=$positionRet['data'];
+                }
+
+                $propertyIds=$contract->{'property'};
+                $propertyItem=FactoringProperty::whereIn('id',$propertyIds)->select(['id','firm_id','factor_id'])->first();
+                if(empty($propertyItem)){
+                    $v->update(['status' => 3,'msg'=>'资产信息异常']);
+                    continue;
+                }
+                $signers=[];
+                $isAuto=1;
+                $signUrl=[];
+                foreach ($positionData as $item){
+                    if(count($item['positions'])<=0){
+                        continue;
+                    }
+                    if($item['keyword']=='#转让方盖章处#'){
+                        $signType=1;
+                        $firmId=$contract['firm_id'];
+                    }else{
+                        $firmId=$contract['factor_id'];
+                        $signType=5;
+                    }
+                    $orgId=SignFirm::where('firm_id',$firmId)->value('org_id');
+                    $seal=SignSeal::where('firm_id',$firmId)->where('seal_type','CONTRACT')->select(['id','seal_id','is_auth'])->first();
+                    dump($seal);
+                    if(empty($orgId) || empty($seal)){
+                        continue;
+                    }
+                    $assignedSealId=$seal->{'seal_id'};
+                    $position=end($item['positions']);
+                    $coordinate=end($position['coordinates']);
+                    if($seal->{'is_auth'}){
+                        $sysOrgId=CommonServer::creatServer()->getConfigValue('sys_org_id');
+                        $signer=[
+                            "orgSignerInfo" => [
+                                "orgId" => $sysOrgId     //其他企业自动签署,设置orgId平台方id
+                            ],
+                            "signFields" =>[
+                                [
+                                    "fileId" =>$fileId,
+                                    "normalSignFieldConfig" => [
+                                        "autoSign" =>true,
+                                        "signFieldStyle" =>1,
+                                        "assignedSealId"=>$assignedSealId,    // 在signFields中设置 assignedSealId为授权企业印章id
+                                        "signFieldPosition" => [
+                                            "positionPage" =>$position['pageNum'].'',
+                                            "positionX" =>(int)$coordinate['positionX'],
+                                            "positionY" =>(int)$coordinate['positionY'],
+                                        ]
+                                    ]
+                                ]
+                            ],
+                            "signerType" =>1
+                        ];
+                    }else{
+                        $isAuto=0;
+                        $member=Member::where('firm_id',$firmId)->where('is_admin',1)->select(['id','phone','promissory_name','promissory_id_number'])->first();
+                        if(empty($member)){
+                            continue;
+                        }
+                        $firm=Firm::where('id',$firmId)->select(['id','firm_name','license_number'])->first();
+                        if(empty($firm)){
+                            continue;
+                        }
+                        $psnId=SignMember::where('m_id',$member->id)->value('psnId');
+                        if(empty($psnId)){
+                            continue;
+                        }
+                        //记录需要手动签署的
+                        $signUrl[]=[
+                            'promissoryName'=>$member['promissory_name'],
+                            'psnAccount'=>$member['phone'],
+                            'orgName'=>$firm->{'firm_name'},
+                            'type'=>$signType,
+                            'is_auto'=>0
+                        ];
+                        $signer=[
+                            "orgSignerInfo" => [
+                                "orgName" => $firm->{'firm_name'},
+                                'orgInfo'=>[
+                                    'orgIDCardNum'=>$firm->{'license_number'},
+                                    'orgIDCardType'=>'CRED_ORG_USCC',
+                                ],
+                                "transactorInfo"=>[
+                                    "psnAccount"=>$member['phone'],
+                                    'psnInfo'=>[
+                                        'psnIDCardNum'=>$member['promissory_id_number'],
+                                        'psnIDCardType'=>'CRED_PSN_CH_IDCARD'
+                                    ]
+                                ]
+
+                            ],
+                            "signFields" =>[
+                                [
+                                    "fileId" =>$fileId,
+                                    "normalSignFieldConfig" => [
+                                        "autoSign" =>false,
+                                        "signFieldStyle" =>1,
+                                        "assignedSealId"=>$assignedSealId,    // 在signFields中设置 assignedSealId为授权企业印章id
+                                        "signFieldPosition" => [
+                                            "positionPage" =>$position['pageNum'].'',
+                                            "positionX" =>(int)$coordinate['positionX'],
+                                            "positionY" =>(int)$coordinate['positionY'],
+                                        ]
+                                    ]
+                                ]
+                            ],
+                            "signerType" =>1
+                        ];
+                    }
+                    $signers[]=$signer;
+                }
+
+                if(empty($signers)){
+                    $v->update(['status' => 3,'msg'=>'暂无可执行电签印章']);
+                    continue;
+                }
+//                dd($signers);
+                $flowId=  $this->createByFile($fileId,$contract['name'].'.pdf',$contract['name'],$signers);
+                if($flowId){
+                    $v->update(['status' => $isAuto==1?2:1,'flow_id'=>$flowId]);
+                    if($signUrl){
+                        foreach ($signUrl as $item){
+//                        if($item['is_auto']){
+//                            FactoringContractSign::where('contract_id',$v['contract_id'])->where('signatory',$item['type'])->update(['sign_url'=>'','status' => 1]);
+//                            FinancingServer::creatServer()->signFinancingContract($v['factoring_id'],$v['id'],$item['type']);
+//                        }else{
+//
+//                        }
+                            $urlRet= $this->getSignUrl($flowId,$item['psnAccount'],$item['orgName']);
+                            if($urlRet){
+                                $content='尊敬的'.$item['promissoryName'].',您有待签署合同《'.$contract['name'].'》,点击签署 '.$urlRet;
+                                SmsServer::creatServer()->sendContent($item['psnAccount'],$content);
+                                FactoringContractSign::where('contract_id',$v['contract_id'])->where('signatory',$item['type'])->update(['sign_url'=>$urlRet]);
+                            }
+                        }
+                    }else{
+//                        $this->downloadFile($flowId);
+                        ContractServer::creatServer()->setContractFile($flowId);
+                    }
+
+                }else{
+                    $v->update(['status' => 3,'msg'=>'合同生成失败']);
+                }
+            }
+
+        }
+        return $list->count();
+    }
+
+
+
+
+    function getSignUrl($flowId,$psnAccount,$orgName){
+        EsignLogHelper::printMsg("**********获取合同文件签署链接开始**********");
+        $config=$this->config;
+
+        $apiaddr="/v3/sign-flow/%s/sign-url";
+        $apiaddr = sprintf($apiaddr,$flowId);
+        $requestType = HttpEmun::POST;
+        $data = [
+            "signFlowId" =>$flowId,
+            "clientType" =>"ALL",
+            "needLogin" =>false,
+            "operator" => [
+                "psnAccount" =>$psnAccount
+            ],
+            'organization'=>[
+                'orgName'=>$orgName
+            ],
+            "urlType" =>2
+        ];
+        $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());
+        $url= null;
+        if($response->getStatus() == 200){
+            $url =  json_decode($response->getBody())->data->shortUrl;
+            EsignLogHelper::printMsg("获取合同文件签署链接调用成功,url: ".$url);
+        }else{
+            EsignLogHelper::printMsg("获取合同文件签署链接接口调用失败,HTTP错误码".$response->getStatus());
+        }
+        EsignLogHelper::printMsg("**********获取合同文件签署链接调用结束**********");
+        return  $url;
+    }
+
+    /**
+     * 合同签署
+     * @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('wps.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;
+    }
+
+
+    function downloadFile($flowId){
+        $config=$this->config;
+
+        EsignLogHelper::printMsg("**********下载已签署文件及附属材料开始**********");
+        $apiaddr="/v3/sign-flow/%s/file-download-url";
+        $apiaddr = sprintf($apiaddr,$flowId);
+        $requestType = HttpEmun::GET;
+        $paramStr = null;
+        $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());
+        if($response->getStatus() == 200){
+            EsignLogHelper::printMsg("下载已签署文件及附属材料调用成功: ".$response->getBody());
+            return json_decode($response->getBody(),true);
+        }else{
+            EsignLogHelper::printMsg("下载已签署文件及附属材料调用失败,HTTP错误码".$response->getStatus());
+        }
+        EsignLogHelper::printMsg("**********下载已签署文件及附属材料调用结束**********");
+        return  false;
+    }
+
+
+
+
+
+}

+ 664 - 0
app/Servers/ESign/ESignFirmServer.php

@@ -0,0 +1,664 @@
+<?php
+
+
+namespace App\Servers\ESign;
+
+
+
+
+
+use App\Models\Firm;
+use App\Models\Member;
+use App\Models\SignFirm;
+use App\Models\SignMember;
+use App\Models\SignSeal;
+use App\Models\SignSealAuth;
+use App\Servers\ESign\comm\EsignHttpHelper;
+use App\Servers\ESign\comm\EsignLogHelper;
+use App\Servers\ESign\emun\HttpEmun;
+use App\Servers\Firm\FirmServer;
+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($m_id,$firm_id,$orgName){
+        $info =  SignMember::where('firm_id',$firm_id)->where('authType','ORG')->first();
+        if (empty($info)){
+            //入库
+            $info= SignMember::create([
+                'm_id' => $m_id,
+                'firm_id' => $firm_id,
+                'firm_name' => $orgName,
+                'phone' => '',
+                'id_number' => '',
+                'realnameStatus'=>0,
+                'authorizeUserInfo'=>0,
+                'psnId'=>'',
+                'orgId'=>'',
+                'authShortUrl'=>'',
+                'authType' => 'ORG',
+            ]);
+        }else{
+            if($info->{'realnameStatus'}==1 && $info->{'orgId'}!=''){
+                FirmServer::creatServer()->updateFirmStatus($firm_id);
+                $signFirm=SignFirm::where('firm_id',$firm_id)->first();
+                if(empty($signFirm)){
+                    SignFirm::create([
+                       'firm_id' => $firm_id,
+                       'org_id' => $info->{'orgId'},
+                        'realname_status'=>1
+                    ]);
+                }else{
+                    $signFirm->update([
+                        'org_id' => $info->{'orgId'},
+                        'realname_status'=>1
+                    ]);
+                }
+                return $info;
+            }
+        }
+        $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'];
+            //更新认证信息
+            SignMember::where('m_id',$m_id)->where('firm_id',$firm_id)->where('authType','ORG')->update([
+                'realnameStatus'=>$realnameStatus,
+                'authorizeUserInfo'=>$authorizeUserInfo,
+                'orgId'=>$orgId,
+            ]);
+
+            if ($realnameStatus == 1){//更新企业认证状态
+                FirmServer::creatServer()->updateFirmStatus($firm_id);
+            }
+            if (empty($realnameStatus) || empty($authorizeUserInfo)){//走认证授权流程
+                $this->orgAuthUrl($m_id,$firm_id,$orgName);
+            }
+        }else{
+            //走认证授权流程
+            $this->orgAuthUrl($m_id,$firm_id,$orgName);
+        }
+
+        return $info;
+    }
+
+    /**
+     * 企业实名
+     * @param $orgName
+     */
+    function orgAuthUrl($m_id,$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();
+        $member=Member::where('id',$m_id)->select(['id','promissory_name','promissory_id_number','phone'])->first();
+        $data=[
+            "orgAuthConfig"=>[
+                "orgName"=>$orgName,
+                "orgInfo"=>[
+                    "orgIDCardNum"=>$firm['license_number'],
+                    "legalRepName"=>$firm['legal_name'],
+                    "orgIDCardType"=>"CRED_ORG_USCC",
+
+                ],
+                "transactorInfo"=>[
+                    "psnAccount"=>$member['phone'],
+                    "psnInfo"=>[
+                        "psnIDCardNum"=>$member['promissory_id_number'],
+                        "psnName"=>$member['promissory_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('wps.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;
+            }
+            $signMember=  SignMember::where('m_id',$m_id)->where('firm_id',$firm_id)->where('authType','ORG')->first();
+            if(!empty($signMember)){
+                $authShortUrl = $ret['data']['authShortUrl'];
+                RedisDataServer::creatServer()->setData($firm_id.'_firmUrl',$authShortUrl,'str',600);
+                $authUrl = $ret['data']['authUrl'];
+                $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 $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('wps.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());
+    }
+
+
+    /**
+     * 记录E签宝审核基本信息
+     * @param $data
+     * @return array|void
+     */
+    function setSignFirm($data)
+    {
+        if(empty($data['orgName'])){
+            return ['code'=>0,'msg'=>'暂无企业信息'];
+        }
+        $firm=Firm::where('firm_name',$data['orgName'])->select(['id','firm_name','status'])->first();
+        if(empty($firm)){
+            return ['code'=>0,'msg'=>'企业暂未注册'];
+        }
+        if($data['realnameStatus']==1){
+            if($firm->{'status'}!=3){
+                $firm->update(['status' => 3]);
+            }
+        }
+        $signFirm=SignFirm::where('firm_id',$firm['id'])->first();
+        if(empty($signFirm)){
+            SignFirm::create([
+                'firm_id' => $firm['id'],
+                'org_id' => $data['orgId'],
+                'realname_status' => $data['realnameStatus']
+            ]);
+        }else{
+            $signFirm->update(['org_id' => $data['orgId'],'realname_status' => $data['realnameStatus']]);
+        }
+        //检测系统内部的企业印章信息
+        JobServer::creatServer()->createFirmSealJob($firm['id']);
+    }
+
+    /**
+     * 获取orgId
+     * @param $firmId
+     * @return false|mixed
+     */
+    function getFirmOrgId($firmId)
+    {
+        $orgId=SignFirm::where('firm_id',$firmId)->value('org_id');
+        if(empty($orgId)){
+            return false;
+        }
+        return $orgId;
+    }
+
+
+    /**
+     * 获取企业外部签章列表
+     * @param $firmId
+     * @param $pageNum
+     * @return array|string[]
+     */
+    function orgAuthorizedSealList($firmId,$pageNum=1){
+        $orgId=$this->getFirmOrgId($firmId);
+        if(empty($orgId)){
+            return ['code'=>'0','msg'=>'暂无企业授权信息'];
+        }
+        $config=$this->config;
+        $apiaddr="/v3/seals/org-authorized-seal-list?orgId=".$orgId."&pageNum={$pageNum}&pageSize=20";
+        $requestType = HttpEmun::GET;
+        //生成签名验签+json体的header
+        $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], "", $requestType, $apiaddr);
+        //发起接口请求头
+        //EsignLogHelper::printMsg($signAndBuildSignAndJsonHeader);
+        $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'],2);
+            return ['code'=>'1','msg'=>'ok','data'=>['total'=>$ret['data']['total'],'items'=>$ret['data']['seals']]];
+        }else{
+            return ['code'=>'0','msg'=>$ret['message']];
+        }
+    }
+
+    /**
+     * 获取企业内部使用签章列表
+     * @param $firmId
+     * @param $pageNum
+     * @return array|string[]
+     */
+    function internalAuthInfo($firmId,$pageNum=1){
+        $orgId=$this->getFirmOrgId($firmId);
+        if(empty($orgId)){
+            return ['code'=>'0','msg'=>'暂无企业授权信息'];
+        }
+        $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'],1);
+            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
+     * @param $useType
+     * @return void
+     */
+    public function setSealList($firmId,$list,$useType)
+    {
+        if($list){
+            foreach ($list as $item){
+                $sealRow=[
+                  'firm_id'=>$firmId,
+                    'seal_id'=>$item['sealId'],
+                    'seal_name'=>$item['sealName'],
+                    'seal_type'=>$item['sealBizType'],
+                    'seal_url'=>$item['sealImageDownloadUrl'],
+                    'use_type'=>$useType,
+                ];
+                $sealModel=SignSeal::where('firm_id',$firmId)->where('seal_id',$item['sealId'])->first();
+                if(empty($sealModel)){
+                    $sealModel=SignSeal::create($sealRow);
+                }else{
+                    $sealModel->update($sealRow);
+                }
+                $this->setSealImg($sealModel);
+            }
+        }
+    }
+
+
+    /**
+     * 保持印章图片
+     * @param SignSeal $signSeal
+     * @return void
+     */
+    function setSealImg(SignSeal  $signSeal)
+    {
+        $path=public_path('storage/seal/'.date('Ymd').'/');
+        if(!is_dir($path)){
+            mkdir($path,0777,true);
+        }
+        $name=$signSeal['seal_id'].'.png';
+        file_put_contents($path.$name,file_get_contents($signSeal['seal_url']));
+        $url=env('APP_URL').'/storage/seal/'.date('Ymd').'/'.$name;
+        $signSeal->update(['seal_url'=>$url]);
+    }
+
+
+    /**
+     * 创建企业合同印章
+     * @param $firmId
+     * @return string[]|void
+     */
+    function createOrgsealsbyTemplate($firmId){
+        $sealNum=SignSeal::where('firm_id',$firmId)->where('seal_type','CONTRACT')->count();
+        if($sealNum>0){
+            return ['code'=>'0','msg'=>'合同印章已创建'];
+        }
+        $orgId=$this->getFirmOrgId($firmId);
+        if(empty($orgId)){
+            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 - 已实名
+            SignSeal::create([
+                'firm_id' => $firmId,
+                'seal_id' => $ret['data']['sealId'],
+                'seal_name' => "合同印章",
+                'seal_type' => 'CONTRACT'
+            ]);
+            //获取完成印章以后重新拉取一下印章信息
+            $this->internalAuthInfo($firmId);
+            return ['code'=>'1','msg'=>'ok'];
+        }else{
+            return ['code'=>'0','msg'=>$ret['message']];
+        }
+    }
+
+
+    /**
+     * 查询企业对平台的授权
+     * @param $firmId
+     * @param $sealKey
+     * @return array|string[]
+     */
+    function selectOrgsealsExternalAuth($firmId,$sealKey)
+    {
+        $orgId=SignFirm::where('firm_id',$firmId)->value('org_id');
+        $sealId=SignSeal::where('id',$sealKey)->value('seal_id');
+        $config=$this->config;
+        $apiaddr="/v3/seals/org-seals/external-auth?orgId={$orgId}&pageNum=1&pageSize=20&sealId={$sealId}";
+        $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){
+           $status= $this->setSealAuth($firmId,$sealKey,$ret['data']['sealAuthorizedInfos']);
+           if(empty($status)){
+               return ['code'=>'0','msg'=>'暂无授权信息'];
+           }
+            return ['code'=>'1','msg'=>'ok','data'=>['total'=>$ret['data']['total'],'items'=>$ret['data']['sealAuthorizedInfos']]];
+        }else{
+            return ['code'=>'0','msg'=>$ret['message']];
+        }
+    }
+
+    function setSealAuth($firmId,$sealKey,$list)
+    {
+        $status=false;
+        if(empty($list)){
+            return false;
+        }
+        $sysOrgId=CommonServer::creatServer()->getConfigValue('sys_org_id');
+        if(empty($sysOrgId)){
+            return false;
+        }
+        foreach ($list as $value){
+            if($value['authorizedOrgId']==$sysOrgId &&$value['authorizeStatus']==1){
+                $seal= SignSeal::where('id',$sealKey)->first();
+                if($seal){
+                    $seal->update(['is_auth' => 1]);
+                    $member_id=SignMember::where('psnId',$value['authorizerPsnId'])->value('m_id');
+                    if(empty($member_id)){
+                        $member_id=0;
+                    }
+                    SignSealAuth::create([
+                        'firm_id' => $firmId,
+                        'seal_id' => $seal->{'seal_id'},
+                        'member_id' => $member_id,
+                        'end_at' => date('Y-m-d H:i:s',$value['expireTime']/1000),
+                        'status' => 1,
+                        'url' => '',
+                        'auth_biz_id'=>$value['sealAuthBizId']
+                    ]);
+                    $status=true;
+                    break;
+                }
+
+            }
+        }
+        return $status;
+    }
+
+
+    /**
+     * 跨企业授权
+     * @param $firmId
+     * @param $sealKey
+     * @param $memberId
+     * @return void
+     */
+    function orgsealsExternalAuth($firmId,$sealKey,$memberId){
+        $authShortUrl=RedisDataServer::creatServer()->getData($firmId.'_sealUrl'.$sealKey);
+        if($authShortUrl){
+            return ;
+        }
+        $orgId=SignFirm::where('firm_id',$firmId)->value('org_id');
+        $sealId=SignSeal::where('id',$sealKey)->value('seal_id');
+        $transactorPsnId=SignMember::where('m_id',$memberId)->where('authType','PSN')->where('psnId','<>','')->orderBy('id','desc')->value('psnId');
+        $config=$this->config;
+        $apiaddr="/v3/seals/org-seals/external-auth";
+        $requestType = HttpEmun::POST;
+        $firm_name=CommonServer::creatServer()->getConfigValue('firm_name');
+        $license_number=CommonServer::creatServer()->getConfigValue('license_number');
+        //生成签名验签+json体的header
+        $data=[
+            "orgId"=>$orgId,
+            "sealId"=>$sealId,//机构印章名称(用户自定义,且名称不可重复)
+            "transactorPsnId"=>$transactorPsnId,
+            "authorizedOrgInfo"=>[
+                "orgName"=>$firm_name,
+                "orgIDCardNum"=>$license_number
+            ],
+            "effectiveTime"=>time() * 1000,//授权生效时间(Unix 时间戳格式,单位:毫秒),示例中获取的当前时间为授权日期
+            "expireTime"=>strtotime("+365 day",time()) * 1000,//授权失效时间(Unix时间戳格式,单位:毫秒)示例中获取授权有效期为1年
+
+        ];
+        dump($data);
+        $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);
+        EsignLogHelper::printMsg($response->getStatus());
+        $data=json_decode($response->getBody(),true);
+        if($data['code']===0){
+            $authShortUrl=RedisDataServer::creatServer()->getData($firmId.'_sealUrl'.$sealKey);
+            if($authShortUrl){
+                return ;
+            }
+            $authNum=SignSealAuth::where('firm_id',$firmId)->where('seal_id',$sealId)->where('member_id',$memberId)->where('created_at','>=',date('Y-m-d H:i:s',time()-600))->count();
+            if($authNum<=0){
+                RedisDataServer::creatServer()->setData($firmId.'_sealUrl'.$sealKey,$data['data']['authorizationSignShortUrl'],'str',600);
+                $member=Member::where('id',$memberId)->select(['phone','promissory_name'])->first();
+                $content='尊敬的'.$member['promissory_name'].',您有待签署印章授权,点击签署 '.$data['data']['authorizationSignShortUrl'];
+                SmsServer::creatServer()->sendContent($member['phone'],$content);
+                SignSealAuth::create([
+                    'firm_id'=>$firmId,
+                    'seal_id'=>$sealId,
+                    'member_id'=>$memberId,
+                    'end_at'=>date('Y-m-d H:i;s',strtotime("+365 day",time())),
+                    'status'=>0,
+                    'url'=>$data['data']['authorizationSignShortUrl'],
+                    'auth_biz_id'=>$data['data']['sealAuthBizId'],
+                ]);
+            }
+            EsignLogHelper::printMsg("跨企业授权接口调用成功");
+            EsignLogHelper::printMsg($response->getBody());
+        }else{
+            EsignLogHelper::printMsg($response->getBody());
+        }
+    }
+
+
+    /**
+     * 添加企业机构成员
+     * @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());
+    }
+
+
+
+}

+ 343 - 0
app/Servers/ESign/ESignServer.php

@@ -0,0 +1,343 @@
+<?php
+
+
+namespace App\Servers\ESign;
+
+
+
+use App\Models\Member;
+use App\Models\SignMember;
+use App\Servers\ESign\comm\EsignHttpHelper;
+use App\Servers\ESign\comm\EsignLogHelper;
+use App\Servers\ESign\emun\HttpEmun;
+use App\Servers\Member\MemberServer;
+use App\Servers\System\RedisDataServer;
+use App\Servers\System\SmsServer;
+
+class ESignServer
+{
+    static private $server = null;
+
+    public  $config = [
+        'eSignAppId'=>'', //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());
+    }
+
+
+
+
+
+
+
+}

+ 102 - 0
app/Servers/ESign/comm/EsignHttpCfgHelper.php

@@ -0,0 +1,102 @@
+<?php
+namespace App\Servers\ESign\comm;
+
+/**
+ * 网络请求配置工具类
+ * @author  澄泓
+ * @date  2022/08/18 14:27
+ */
+class EsignHttpCfgHelper
+{
+    public static $connectTimeout = 15;//15 second
+    public static $readTimeout = 15;//15 second
+    public static $uploadReadTimeout = 60;
+    public static $uploadConnectTimeout = 60;
+    public static $enableHttpProxy=false;//是否需要代理
+    public static $httpProxyIp;//代理ip
+    public static $httpProxyPort;//代理端口
+
+    //常规请求类
+    public static function sendHttp($reqType, $url, $headers, $param=null)
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $reqType);
+        if (self::$enableHttpProxy) {
+            curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
+            curl_setopt($ch, CURLOPT_PROXY, self::$httpProxyIp);
+            curl_setopt($ch, CURLOPT_PROXYPORT, self::$httpProxyPort);
+            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
+        }
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_FAILONERROR, false);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        $postData = is_array($param) ? json_encode($param, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $param;
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
+        if (self::$readTimeout) {
+            curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout);
+        }
+        if (self::$connectTimeout) {
+            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
+        }
+        //https request
+        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
+            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+        }
+        if (is_array($headers) && 0 < count($headers)) {
+            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+        }
+        $curlRes = curl_exec($ch);
+
+        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+        curl_close($ch);
+        return new EsignResponse($httpCode,$curlRes);
+    }
+
+    /**
+     * 上传文件
+     * @param $uploadUrls
+     * @param $contentMd5
+     * @param $fileContent
+     * @param $ContenType
+     * @return EsignResponse
+     */
+        public static function upLoadFileHttp($uploadUrls, $contentMd5, $fileContent,$ContenType){
+            $header = array(
+                'Content-Type:'.$ContenType,
+                'Content-Md5:' . $contentMd5
+            );
+
+            $curl_handle = curl_init();
+            curl_setopt($curl_handle, CURLOPT_URL, $uploadUrls);
+            curl_setopt($curl_handle, CURLOPT_FILETIME, true);
+            curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, false);
+           // curl_setopt($curl_handle, CURLOPT_HEADER, true); // 输出HTTP头 true
+            curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
+
+            if (self::$uploadReadTimeout) {
+                curl_setopt($curl_handle, CURLOPT_TIMEOUT, self::$uploadReadTimeout);
+            }
+            if (self::$uploadConnectTimeout) {
+                curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, self::$uploadConnectTimeout);
+            }
+
+            curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
+            curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);
+
+            curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);
+            curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'PUT');
+
+            curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $fileContent);
+            if (is_array($header) && 0 < count($header)) {
+                curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);
+            }
+            $curlRes = curl_exec($curl_handle);
+            $httpCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
+
+            curl_close($curl_handle);
+            return new EsignResponse($httpCode,$curlRes);
+    }
+
+
+}

+ 140 - 0
app/Servers/ESign/comm/EsignHttpHelper.php

@@ -0,0 +1,140 @@
+<?php
+namespace App\Servers\ESign\comm;
+
+/**
+ * esignHttp请求类
+ * @author  澄泓
+ * @date  2022/08/18 15:10
+ */
+class EsignHttpHelper
+{
+    /**
+     * 常规请求
+     * @param $host
+     * @param $url
+     * @param $reqType
+     * @param $header
+     * @param $paramStr
+     * @return EsignResponse
+     */
+    public static function doCommHttp($host, $url, $reqType, $header, $paramStr)
+    {
+        //做encode编码防止中文乱码
+        $url = EsignHttpHelper::querySort($url, true);
+        return EsignHttpCfgHelper::sendHttp($reqType, $host . $url, $header, $paramStr);
+    }
+
+    /**
+     * 文件上传
+     * @param $uploadUrls
+     * @param $filePath
+     * @param $ContenType
+     * @return EsignResponse
+     */
+    public static function upLoadFileHttp($uploadUrls, $filePath, $ContenType)
+    {
+        $fileContent = file_get_contents($filePath);
+        $contentBase64Md5 = EsignUtilHelper::getContentBase64Md5($filePath);
+        return EsignHttpCfgHelper::upLoadFileHttp($uploadUrls, $contentBase64Md5, $fileContent, $ContenType);
+    }
+
+
+    /**
+     * 签名计算并且构建一个签名鉴权+json数据的esign请求头
+     * @param $projectId
+     * @param $secret
+     * @param $paramStr
+     * @param $reqType
+     * @param $url
+     * @param string $accept
+     * @param string $contentType
+     * @return array
+     */
+    public static function signAndBuildSignAndJsonHeader($projectId, $secret, $paramStr, $reqType, $url,$accept="*/*",$contentType="application/json; charset=UTF-8")
+    {
+        $contentMd5="";
+
+        //get和delete方式请求不能携带body体
+        if (strtoupper($reqType) == "GET" || strtoupper($reqType) == "DELETE") {
+            $paramStr=null;
+        }else{
+            $contentMd5 = EsignUtilHelper::getContentMd5($paramStr);
+        }
+        //对body体做md5摘要
+        //构造一个初步请求头
+        $buildSignAndJsonHeader = self::buildSignAndJsonHeader($projectId, $contentMd5, $accept, $contentType, "Signature");
+
+        //对url做排序
+        self::querySort($url);
+        //传入生成的bodyMd5,加上其他请求头部信息拼接成字符串,整体做sha256签名
+        $reqSignature = EsignUtilHelper::getSignature($secret, $reqType, $accept, $contentType, $contentMd5, "", "", $url);
+        array_push($buildSignAndJsonHeader,"X-Tsign-Open-Ca-Signature:".$reqSignature);
+        return $buildSignAndJsonHeader;
+}
+
+    /**
+     * 建一个签名鉴权+json数据的esign初步请求头
+     * @param $projectId
+     * @param $contentMD5
+     * @param $accept
+     * @param $contentType
+     * @param $authMode
+     * @return array
+     */
+    public static function buildSignAndJsonHeader($projectId, $contentMD5, $accept, $contentType, $authMode)
+    {
+        $headers = array(
+            'X-Tsign-Open-App-Id:'.$projectId,
+            'X-Tsign-Open-Ca-Timestamp:'.EsignUtilHelper::getMillisecond(),
+            'Accept:'.$accept,
+            'Content-Type:'.$contentType,
+            'X-Tsign-Open-Auth-Mode:'.$authMode,
+            'X-Tsign-Dns-App-Id:'.$projectId
+        );
+        if(!empty($contentMD5)){
+            array_push($headers,'Content-MD5:'.$contentMD5);
+        }
+        return $headers;
+    }
+
+
+    public static function querySort($url,$isEncode=false){
+        #存在query参数
+        if (!strpos($url, "?") === false) {
+            $query_arr = [];
+            $url_path = substr($url, 0, strpos($url, "?"));
+            $url_query = substr($url, strpos($url, "?") + 1, mb_strlen($url));
+            parse_str($url_query, $query_arr);
+            ksort($query_arr);
+            //reset()内部指针指向数组中的第一个元素
+            reset($query_arr);
+            $query_str = http_build_query($query_arr);
+            if(!$isEncode){
+                $query_str = urldecode($query_str);
+            }
+            $url = $url_path . '?' . $query_str;
+            return $url;
+        }
+        return $url;
+    }
+    /**
+     * 构造头部信息
+     * @param $contentMD5
+     * @param $reqSignature
+     * @return array
+     */
+    public static function buildCommHeader($projectId, $contentMD5, $reqSignature)
+    {
+        $headers = array(
+            'X-Tsign-Open-App-Id:' . $projectId,
+            'X-Tsign-Open-Ca-Timestamp:' . EsignUtilHelper::getMillisecond(),
+            'Accept:*/*',
+            'X-Tsign-Open-Ca-Signature:' . $reqSignature,
+            'Content-MD5:' . $contentMD5,
+            'Content-Type:application/json; charset=UTF-8',
+            'X-Tsign-Open-Auth-Mode:Signature'
+        );
+        return $headers;
+    }
+
+}

+ 32 - 0
app/Servers/ESign/comm/EsignLogHelper.php

@@ -0,0 +1,32 @@
+<?php
+namespace App\Servers\ESign\comm;
+
+/**
+ * esign日志类
+ * @author  澄泓
+ * @date  2022/08/18 15:10
+ */
+class EsignLogHelper
+{
+    static function writeLog($text,$type=1) {
+        if(is_array($text) || is_object($text)){
+            $text = json_encode($text);
+        }
+        $logDir = public_path('/storage/e-sign/log/'.$type.'/'.date('Ymd').'/')  ;
+        if(!is_dir($logDir)){
+            mkdir($logDir,0777,true);
+        }
+        file_put_contents ( $logDir.date("Y-m-d").".log", date("Y-m-d H:i:s") . "  日志" . $text . "\r\n------------------\r\n", FILE_APPEND );
+    }
+
+    static function  printMsg($msg)
+    {
+        echo "<pre/>";
+        if (is_array($msg) || is_object($msg)) {
+            var_dump($msg);
+        } else {
+            echo $msg . "\n";
+        }
+    }
+
+}

+ 62 - 0
app/Servers/ESign/comm/EsignResponse.php

@@ -0,0 +1,62 @@
+<?php
+
+
+namespace App\Servers\ESign\comm;
+/**
+ * esign响应
+ * @author  澄泓
+ * @date  2022/08/18 9:51
+ */
+
+class EsignResponse
+{
+    private $status;
+    private $body;
+
+    /**
+     * EsignResponse constructor.
+     * @param $status
+     * @param $body
+     */
+    public function __construct($status, $body)
+    {
+        $this->status = $status;
+        $this->body = $body;
+    }
+
+
+
+    /**
+     * @return mixed
+     */
+
+    public function getStatus()
+    {
+        return $this->status;
+    }
+
+    /**
+     * @param mixed $status
+     */
+    public function setStatus($status)
+    {
+        $this->status = $status;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getBody()
+    {
+        return $this->body;
+    }
+
+    /**
+     * @param mixed $body
+     */
+    public function setBody($body)
+    {
+        $this->body = $body;
+    }
+
+}

+ 94 - 0
app/Servers/ESign/comm/EsignUtilHelper.php

@@ -0,0 +1,94 @@
+<?php
+namespace App\Servers\ESign\comm;
+
+/**
+ * 常用工具类
+ * @author  澄泓
+ * @date  2022/08/18 15:40
+ */
+class EsignUtilHelper
+{
+    /**
+     * 获取MD5
+     * @param $bodyData
+     * @return string
+     */
+    public static function getContentMd5($bodyData){
+        return  base64_encode(md5($bodyData,true));
+    }
+
+    /**
+     * 生成签名
+     * @param $projectScert
+     * @param $httpMethod
+     * @param $accept
+     * @param $contentType
+     * @param $contentMd5
+     * @param $date
+     * @param $headers
+     * @param $url
+     * @return string
+     */
+    public static function getSignature($projectScert,$httpMethod,$accept,$contentType,$contentMd5,$date,$headers,$url){
+
+        $stringToSign = $httpMethod . "\n" . $accept . "\n"  . $contentMd5 . "\n" . $contentType . "\n"  . $date . "\n" .$headers  ;
+        if($headers != ''){
+            $stringToSign .= "\n".$url;
+        }else{
+            $stringToSign .= $url;
+        }
+//        EsignLogHelper::printMsg($stringToSign);
+        $signature = hash_hmac("sha256",$stringToSign, $projectScert, true);
+        $signature = base64_encode($signature);
+        return $signature;
+    }
+
+
+    // 判断是网络路径还是文件路径
+    public static function isUrl($url){
+        $parse = parse_url($url);
+        $scheme = $parse['scheme'];
+        $scheme = strtolower($scheme);
+        if("http" == $scheme){
+            return true;
+        }
+
+        if("https" == $scheme){
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * 生成13位时间戳
+     * @return float
+     */
+    public static function getMillisecond() {
+        list($t1, $t2) = explode(' ', microtime());
+        return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
+    }
+
+    public static function jsonSer($obj){
+    $json = array();
+    foreach ($obj as $key => $value) {
+        if($value==null) {
+            continue;
+        }
+        $json[$key] = $value;
+    }
+    return $json;
+    }
+
+    /**
+    获取文件的Content-MD5
+    原理:1.先计算MD5加密的二进制数组(128位)
+    2.再对这个二进制进行base64编码(而不是对32位字符串编码)
+     */
+    public static function getContentBase64Md5($filePath){
+        //获取文件MD5的128位二进制数组
+        $md5file = md5_file($filePath,true);
+        //计算文件的Content-MD5
+        $contentBase64Md5 = base64_encode($md5file);
+        return $contentBase64Md5;
+    }
+}

+ 14 - 0
app/Servers/ESign/emun/HttpEmun.php

@@ -0,0 +1,14 @@
+<?php
+namespace App\Servers\ESign\emun;
+/**
+ * 请求方法枚举
+ * @author  澄泓
+ * @date  2020/11/19 17:34
+ */
+class HttpEmun
+{
+    const PUT="PUT";
+    const POST="POST";
+    const GET="GET";
+    const DELETE="DELETE";
+}