WpsServer.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace App\Servers\Wps;
  3. use App\Models\FactoringContractFile;
  4. use App\Models\FileManager;
  5. use App\Servers\System\CommonServer;
  6. class WpsServer
  7. {
  8. static private $server = null;
  9. private $url='https://solution.wps.cn';
  10. private $edit_app_id='';
  11. private $edit_app_secret='';
  12. private $to_app_id='';
  13. private $to_app_secret='';
  14. function __construct()
  15. {
  16. $this->edit_app_id=env('WPS_EDIT_APP_ID');
  17. $this->edit_app_secret=env('WPS_EDIT_APP_SECRET');
  18. $this->to_app_id=env('WPS_TO_APP_ID');
  19. $this->to_app_secret=env('WPS_TO_APP_SECRET');
  20. }
  21. /**
  22. * 创建对象
  23. * @return WpsServer
  24. */
  25. static function creatServer()
  26. {
  27. if (empty(self::$server)) self::$server = new WpsServer();
  28. return self::$server;
  29. }
  30. /**
  31. * 回调验签
  32. * @return bool
  33. */
  34. function verificationSing(){
  35. $app_id=request()->header('X-App-Id');
  36. $app_secret=$app_id==$this->edit_app_id?$this->edit_app_secret:$this->to_app_secret;
  37. $authorization=request()->header('Authorization');
  38. $md5=request()->header('Content-Md5');
  39. $type=request()->header('Content-Type');
  40. $date=request()->header('Date');
  41. $sign='WPS-2:'.$app_id.':'.sha1($app_secret.$md5.$type.$date);
  42. return $authorization==$sign;
  43. }
  44. function getTemplate(){
  45. $uri='/api/developer/v1/files/w/template';
  46. $url=$this->url.$uri;
  47. $head=$this->wpsSing(1,'uri='.$uri);
  48. $ret=CommonServer::creatServer()->sendRequest($url,[],'get',$head);
  49. dd($ret);
  50. }
  51. /**
  52. * 请求签名
  53. * @param $type
  54. * @param $text
  55. * @return string[]
  56. */
  57. function wpsSing($type,$text){
  58. if($type==1){
  59. $app_id=$this->edit_app_id;
  60. $app_secret=$this->edit_app_secret;
  61. }else{
  62. $app_id=$this->to_app_id;
  63. $app_secret=$this->to_app_secret;
  64. }
  65. $md5=md5($text);
  66. $date= gmdate('D, d M Y H:i:s'). ' GMT';
  67. $type='application/json';
  68. $data=[
  69. 'Content-Md5:'.$md5,
  70. 'Content-Type:'.$type,
  71. 'DATE:'.$date,
  72. 'Authorization:'.'WPS-2:'.$app_id.':'.sha1($app_secret.$md5.$type.$date),
  73. ];
  74. return $data;
  75. }
  76. /**
  77. * 更新合同文件版本号
  78. * @param $file_id
  79. * @return FactoringContractFile|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object
  80. */
  81. function setContractFileVersion($file_id){
  82. //检查该文件编号是否存在
  83. $fileInfo = FactoringContractFile::where('file_no',$file_id)
  84. ->select(['id','file_no','file_name','file_path','file_url','version'])
  85. ->orderBy('id','desc')
  86. ->orderBy('version','desc')
  87. ->first();
  88. if (empty($fileInfo)) {
  89. return false;
  90. }
  91. //组装新版本文件
  92. // $fileNameArr = explode('-', $fileInfo->{'file_name'});
  93. $version = $fileInfo->{'version'} + 1;
  94. $res = FactoringContractFile::create([
  95. 'file_no' => $file_id,
  96. 'file_name' => $fileInfo->{'file_name'},
  97. 'file_url' => $fileInfo->{'file_url'},
  98. 'file_path' => $fileInfo->{'file_path'},
  99. 'version' => $version,
  100. 'type' => 1,
  101. 'is_end' => 1,
  102. ]);
  103. if (!$res){
  104. return false;
  105. }
  106. return $fileInfo;
  107. }
  108. function createFileConvert(){
  109. $list = FileManager::where('status',0)
  110. ->whereNotNull('task_id')
  111. ->orderBy('id','asc')
  112. ->limit(20)
  113. ->get();
  114. foreach ($list as $value){
  115. $extension = pathinfo($value->{'name'}, PATHINFO_EXTENSION);
  116. if (in_array($extension,['pptx','ppt','docx','doc','xlsx','xls'])){//走文档转pdf
  117. }elseif (in_array($extension,['png','jpg','jpeg','bmp','tif','tiff','gif'])){//走图片转文档
  118. }
  119. }
  120. }
  121. function imgConvertDocx($img_url,$office_type='docx'){
  122. $uri='/api/developer/v1/office/img/convert/to/'.$office_type;
  123. $url=$this->url.$uri;
  124. $data['img_urls'] = [$img_url];
  125. $head=$this->wpsSing(2,json_encode($data, JSON_UNESCAPED_UNICODE));
  126. $ret=CommonServer::creatServer()->sendRequest($url,$data,'post',$head);
  127. dd($ret);
  128. }
  129. /**
  130. * PDF转换文档
  131. * @param $pdf_url
  132. * @param $office_type
  133. * @return void
  134. */
  135. function pdfConvertDocx($pdf_url,$office_type='xlsx'){
  136. $uri='/api/developer/v1/office/pdf/convert/to/'.$office_type;
  137. $url=$this->url.$uri;
  138. $data['url'] = $pdf_url;
  139. if($office_type=='xlsx'){
  140. $data['sheet_option'] = 1;
  141. }
  142. $head=$this->wpsSing(2,json_encode($data, JSON_UNESCAPED_UNICODE));
  143. $ret=CommonServer::creatServer()->sendRequest($url,$data,'post',$head);
  144. dd($ret);
  145. }
  146. function queryConvertDocx($task_id,$office_type='xlsx'){
  147. $uri='/api/developer/v1/tasks/convert/to/'.$office_type.'/'.$task_id;
  148. $url=$this->url.$uri;
  149. $head=$this->wpsSing(2,'uri='.$uri);
  150. $ret=CommonServer::creatServer()->sendRequest($url,[],'get',$head);
  151. dd($ret);
  152. }
  153. function fileConvertPdf($file_url,$file_name){
  154. $uri='/api/developer/v1/office/convert/to/pdf';
  155. $url=$this->url.$uri;
  156. $data['url'] = $file_url;
  157. $data['filename'] = $file_name;
  158. $head=$this->wpsSing(2,json_encode($data));
  159. $ret=CommonServer::creatServer()->sendRequest($url,$data,'post',$head);
  160. if(isset($ret['code']) && $ret['code']==0){
  161. return $ret['data']['task_id'];
  162. }else{
  163. return false;
  164. }
  165. }
  166. /**
  167. * 查询Pdf转换结果
  168. * @param $task_id
  169. * @return bool|mixed
  170. */
  171. function queryConvertPdf($task_id){
  172. $uri='/api/developer/v1/tasks/'.$task_id;
  173. $url=$this->url.$uri;
  174. $head=$this->wpsSing(2,'uri='.$uri);
  175. $ret=CommonServer::creatServer()->sendRequest($url,[],'get',$head);
  176. if(isset($ret['code']) && $ret['code']==0){
  177. if($ret['data']['status']=='success' && !empty($ret['data']['result']) && count($ret['data']['result']['pdfs'])>0){
  178. $file_path = 'contractFile/wpspdf/' . date('Ymd') . '/'.$task_id.'/';
  179. CommonServer::creatServer()->creatDir($file_path);
  180. $file_name = md5($ret['data']['result']['pdfs'][0]['url'].time()).'.pdf';
  181. $file_path = $file_path . $file_name;
  182. file_put_contents(public_path($file_path), file_get_contents($ret['data']['result']['pdfs'][0]['url'])); // 写入文件
  183. $file_url = 'https://'.env('APP_ADMIN_HOST').'/'.$file_path;
  184. return $file_url;
  185. }else{
  186. return false;
  187. }
  188. }else{
  189. return false;
  190. }
  191. }
  192. }