瀏覽代碼

no message

panghu 1 年之前
父節點
當前提交
d8b46765c8

+ 7 - 5
app/Console/Commands/TestCommand.php

@@ -64,10 +64,12 @@ class TestCommand extends Command
      */
     public function handle()
     {
-        $file_path = 'storage/contract/测试.docx';
-
-        $json = '{"name": "江雨","text":"测试一下","phone":"13012341234"}';
-        $file = WordServer::creatServer()->replaceWordText($file_path,'123.docx',$json);
-        dd($file);
+//        $file_path = 'storage/contract/测试.docx';
+//
+//        $json = '{"name": "江雨","text":"测试一下","phone":"13012341234"}';
+//        $file = WordServer::creatServer()->replaceWordText($file_path,'123.docx',$json);
+//        dd($file);
+        ESignContractServer::creatServer()->setContractContract(111,11);
+        dd(111);
     }
 }

+ 25 - 7
app/Http/Controllers/AdminApi/SignContractController.php

@@ -6,6 +6,7 @@ use App\Http\Controllers\AdminController;
 use App\Models\Contract;
 use App\Models\Firm;
 use App\Models\Member;
+use App\Models\SignContract;
 use App\Servers\ESign\ESignContractServer;
 use App\Servers\Wps\WordServer;
 
@@ -81,19 +82,24 @@ class SignContractController extends AdminController
         ]);
     }
 
+    /**
+     * 创建合同签署
+     * @return \Illuminate\Http\JsonResponse
+     * @throws \PhpOffice\PhpWord\Exception\Exception
+     */
     function createSignContract()
     {
-        $contract_id=request()->input('contract_id');//合同id
-        $contract_no=request()->input('contract_no');//合同编号
-        $text_json=request()->input('textsJson');//替换文本json
-        $sign_json=request()->input('signJson');//签署区域信息
+        $contract_id=request()->input('contract_id','');//合同id
+        $contract_no=request()->input('contract_no','');//合同编号
+        $text_json=request()->input('textsJson','');//替换文本json
+        $sign_json=request()->input('signJson','');//签署区域信息
         if(empty($contract_id)){
             return $this->apiResponseError('请选择合同');
         }
-        if(empty($textsJson)){
+        if(empty($text_json)){
             return $this->apiResponseError('请填写合同信息');
         }
-        if(empty($signJson)){
+        if(empty($sign_json)){
             return $this->apiResponseError('请选择签署信息');
         }
         //获取合同信息
@@ -107,10 +113,22 @@ class SignContractController extends AdminController
         $pathInfo = pathinfo($file_path);
         $file_name = $pathInfo['filename'].$contract_no.'.'.$pathInfo['extension'];
         //替换合同信息
-        $local_file = WordServer::creatServer()->replaceWordText($file_path,$file_name,$textsJson);
+        $local_file = WordServer::creatServer()->replaceWordText($file_path,$file_name,$text_json);
         //上传文件到e签宝并获得文件id
         $file_id = ESignContractServer::creatServer()->fileUploadUrl($local_file);
 
         $data = compact('contract_id','contract_no','file_id','text_json','sign_json','local_file');
+        //存入数据库
+        $sign = SignContract::create($data);
+        $sign_id = $sign->{'id'};
+        if (empty($sign_id)){
+            return $this->apiResponseError('保存失败');
+        }
+        //上传e签宝进行签署
+        $result = ESignContractServer::creatServer()->setContractContract($sign_id,$file_id,$sign_json,$pathInfo['filename'].$contract_no);
+        if ($result['code'] == 0){
+            return $this->apiResponseError($result['msg']);
+        }
+        return $this->apiResponseSuccess($result['msg']);
     }
 }

+ 38 - 0
app/Models/SignRecord.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Models;
+
+use DateTimeInterface;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+/**
+ * App\Models\Contract
+ *
+ * @mixin \Eloquent
+ */
+class SignRecord extends Model
+{
+    use HasFactory;
+    protected $guarded = ['id', 'updated_at', 'created_at'];
+
+
+    /**
+     * The table associated with the model.
+     *
+     * @var string
+     */
+    protected $table = 'sign_records';
+
+
+    /**
+     * 为数组 / JSON 序列化准备日期。
+     *
+     * @param \DateTimeInterface $date
+     * @return string
+     */
+    protected function serializeDate(DateTimeInterface $date)
+    {
+        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
+    }
+}

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

@@ -6,7 +6,11 @@ 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;
@@ -123,4 +127,185 @@ class ESignContractServer
     }
 
 
+    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;
+    }
 }