ESignContractServer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Servers\ESign;
  3. use App\Models\Firm;
  4. use App\Models\Member;
  5. use App\Servers\ESign\comm\EsignHttpHelper;
  6. use App\Servers\ESign\comm\EsignLogHelper;
  7. use App\Servers\ESign\comm\EsignUtilHelper;
  8. use App\Servers\ESign\emun\HttpEmun;
  9. use App\Servers\System\CommonServer;
  10. use App\Servers\System\SmsServer;
  11. class ESignContractServer
  12. {
  13. static private $server = null;
  14. public $config = [
  15. 'eSignAppId'=>'', //app id
  16. 'eSignAppSecret'=>'', // app key
  17. 'eSignHost'=>'https://smlopenapi.esign.cn', //模拟环境
  18. // 'url'=>'https://openapi.esign.cn' //正式环境
  19. ];
  20. function __construct()
  21. {
  22. $this->config['eSignAppId']=env('E_SIGN_APP_ID');
  23. $this->config['eSignAppSecret']=env('E_SIGN_APP_SECRET');
  24. $this->config['eSignPublicKey']=env('E_SIGN_PUBLIC_KEY');
  25. $this->config['eSignHost']=env('E_SIGN_HOST');
  26. }
  27. /**
  28. * 创建对象
  29. * @return ESignContractServer
  30. */
  31. static function creatServer()
  32. {
  33. if (empty(self::$server)) self::$server = new ESignContractServer();
  34. return self::$server;
  35. }
  36. function contentMd5($filePath){
  37. if(!file_exists($filePath)){
  38. EsignLogHelper::printMsg($filePath."文件不存在");exit;
  39. }
  40. return EsignUtilHelper::getContentBase64Md5($filePath);
  41. }
  42. /**
  43. * 上传合同文件至E签宝
  44. * @param $file_url
  45. * @return mixed
  46. */
  47. function fileUploadUrl($file_url){
  48. $config=$this->config;
  49. $file_name=CommonServer::creatServer()->getFileName($file_url);
  50. $filePath=public_path($file_url);
  51. $contentMd5=$this->contentMd5($filePath);
  52. $apiaddr="/v3/files/file-upload-url";
  53. $requestType = HttpEmun::POST;
  54. $data=[
  55. "contentMd5" =>$contentMd5,
  56. "contentType" =>"application/pdf",
  57. "convertToPDF" =>true,
  58. "fileName" =>$file_name,
  59. "fileSize" =>filesize($filePath)
  60. ];
  61. $paramStr = json_encode($data);
  62. //生成签名验签+json体的header
  63. $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], $paramStr, $requestType, $apiaddr);
  64. //获取文件上传地址
  65. EsignLogHelper::printMsg($signAndBuildSignAndJsonHeader);
  66. $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, $paramStr);
  67. EsignLogHelper::printMsg($response->getStatus());
  68. EsignLogHelper::printMsg($response->getBody());
  69. $fileUploadUrl=json_decode($response->getBody())->data->fileUploadUrl;
  70. $fileId=json_decode($response->getBody())->data->fileId;
  71. //文件流put上传
  72. $response=EsignHttpHelper::upLoadFileHttp($fileUploadUrl,$filePath,"application/pdf");
  73. EsignLogHelper::printMsg($response->getStatus());
  74. EsignLogHelper::printMsg($response->getBody());
  75. return $fileId;
  76. }
  77. /**
  78. * 获取签署文件对应关键字坐标
  79. * @param $fileId
  80. * @param $keywords
  81. * @return array
  82. */
  83. function getKeywordPositions($fileId,$keywords)
  84. {
  85. $config=$this->config;
  86. $apiaddr="/v3/files/{$fileId}/keyword-positions";
  87. $requestType = HttpEmun::POST;
  88. $data=[
  89. "keywords" =>$keywords
  90. ];
  91. $paramStr = json_encode($data);
  92. //生成签名验签+json体的header
  93. $signAndBuildSignAndJsonHeader = EsignHttpHelper::signAndBuildSignAndJsonHeader($config['eSignAppId'], $config['eSignAppSecret'], $paramStr, $requestType, $apiaddr);
  94. //获取文件上传地址
  95. EsignLogHelper::printMsg($signAndBuildSignAndJsonHeader);
  96. $response = EsignHttpHelper::doCommHttp($config['eSignHost'], $apiaddr, $requestType, $signAndBuildSignAndJsonHeader, $paramStr);
  97. EsignLogHelper::printMsg($response->getStatus());
  98. EsignLogHelper::printMsg($response->getBody());
  99. $ret=json_decode($response->getBody(),true);
  100. if(isset($ret['code']) && $ret['code']==0){
  101. return ['code'=>'1','msg'=>'ok','data'=>$ret['data']['keywordPositions']];
  102. }else{
  103. return ['code'=>'0','msg'=>$ret['message']];
  104. }
  105. }
  106. }