| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace App\Servers\ESign;
- use App\Models\Firm;
- use App\Models\Member;
- 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']];
- }
- }
- }
|