| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace App\Servers\Wps;
- use App\Models\FactoringContractFile;
- use App\Models\FileManager;
- use App\Servers\System\CommonServer;
- class WpsServer
- {
- static private $server = null;
- private $url='https://solution.wps.cn';
- private $edit_app_id='';
- private $edit_app_secret='';
- private $to_app_id='';
- private $to_app_secret='';
- function __construct()
- {
- $this->edit_app_id=env('WPS_EDIT_APP_ID');
- $this->edit_app_secret=env('WPS_EDIT_APP_SECRET');
- $this->to_app_id=env('WPS_TO_APP_ID');
- $this->to_app_secret=env('WPS_TO_APP_SECRET');
- }
- /**
- * 创建对象
- * @return WpsServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) self::$server = new WpsServer();
- return self::$server;
- }
- /**
- * 回调验签
- * @return bool
- */
- function verificationSing(){
- $app_id=request()->header('X-App-Id');
- $app_secret=$app_id==$this->edit_app_id?$this->edit_app_secret:$this->to_app_secret;
- $authorization=request()->header('Authorization');
- $md5=request()->header('Content-Md5');
- $type=request()->header('Content-Type');
- $date=request()->header('Date');
- $sign='WPS-2:'.$app_id.':'.sha1($app_secret.$md5.$type.$date);
- return $authorization==$sign;
- }
- function getTemplate(){
- $uri='/api/developer/v1/files/w/template';
- $url=$this->url.$uri;
- $head=$this->wpsSing(1,'uri='.$uri);
- $ret=CommonServer::creatServer()->sendRequest($url,[],'get',$head);
- dd($ret);
- }
- /**
- * 请求签名
- * @param $type
- * @param $text
- * @return string[]
- */
- function wpsSing($type,$text){
- if($type==1){
- $app_id=$this->edit_app_id;
- $app_secret=$this->edit_app_secret;
- }else{
- $app_id=$this->to_app_id;
- $app_secret=$this->to_app_secret;
- }
- $md5=md5($text);
- $date= gmdate('D, d M Y H:i:s'). ' GMT';
- $type='application/json';
- $data=[
- 'Content-Md5:'.$md5,
- 'Content-Type:'.$type,
- 'DATE:'.$date,
- 'Authorization:'.'WPS-2:'.$app_id.':'.sha1($app_secret.$md5.$type.$date),
- ];
- return $data;
- }
- /**
- * 更新合同文件版本号
- * @param $file_id
- * @return FactoringContractFile|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object
- */
- function setContractFileVersion($file_id){
- //检查该文件编号是否存在
- $fileInfo = FactoringContractFile::where('file_no',$file_id)
- ->select(['id','file_no','file_name','file_path','file_url','version'])
- ->orderBy('id','desc')
- ->orderBy('version','desc')
- ->first();
- if (empty($fileInfo)) {
- return false;
- }
- //组装新版本文件
- // $fileNameArr = explode('-', $fileInfo->{'file_name'});
- $version = $fileInfo->{'version'} + 1;
- $res = FactoringContractFile::create([
- 'file_no' => $file_id,
- 'file_name' => $fileInfo->{'file_name'},
- 'file_url' => $fileInfo->{'file_url'},
- 'file_path' => $fileInfo->{'file_path'},
- 'version' => $version,
- 'type' => 1,
- 'is_end' => 1,
- ]);
- if (!$res){
- return false;
- }
- return $fileInfo;
- }
- function createFileConvert(){
- $list = FileManager::where('status',0)
- ->whereNotNull('task_id')
- ->orderBy('id','asc')
- ->limit(20)
- ->get();
- foreach ($list as $value){
- $extension = pathinfo($value->{'name'}, PATHINFO_EXTENSION);
- if (in_array($extension,['pptx','ppt','docx','doc','xlsx','xls'])){//走文档转pdf
- }elseif (in_array($extension,['png','jpg','jpeg','bmp','tif','tiff','gif'])){//走图片转文档
- }
- }
- }
- function imgConvertDocx($img_url,$office_type='docx'){
- $uri='/api/developer/v1/office/img/convert/to/'.$office_type;
- $url=$this->url.$uri;
- $data['img_urls'] = [$img_url];
- $head=$this->wpsSing(2,json_encode($data, JSON_UNESCAPED_UNICODE));
- $ret=CommonServer::creatServer()->sendRequest($url,$data,'post',$head);
- dd($ret);
- }
- /**
- * PDF转换文档
- * @param $pdf_url
- * @param $office_type
- * @return void
- */
- function pdfConvertDocx($pdf_url,$office_type='xlsx'){
- $uri='/api/developer/v1/office/pdf/convert/to/'.$office_type;
- $url=$this->url.$uri;
- $data['url'] = $pdf_url;
- if($office_type=='xlsx'){
- $data['sheet_option'] = 1;
- }
- $head=$this->wpsSing(2,json_encode($data, JSON_UNESCAPED_UNICODE));
- $ret=CommonServer::creatServer()->sendRequest($url,$data,'post',$head);
- dd($ret);
- }
- function queryConvertDocx($task_id,$office_type='xlsx'){
- $uri='/api/developer/v1/tasks/convert/to/'.$office_type.'/'.$task_id;
- $url=$this->url.$uri;
- $head=$this->wpsSing(2,'uri='.$uri);
- $ret=CommonServer::creatServer()->sendRequest($url,[],'get',$head);
- dd($ret);
- }
- function fileConvertPdf($file_url,$file_name){
- $uri='/api/developer/v1/office/convert/to/pdf';
- $url=$this->url.$uri;
- $data['url'] = $file_url;
- $data['filename'] = $file_name;
- $head=$this->wpsSing(2,json_encode($data));
- $ret=CommonServer::creatServer()->sendRequest($url,$data,'post',$head);
- if(isset($ret['code']) && $ret['code']==0){
- return $ret['data']['task_id'];
- }else{
- return false;
- }
- }
- /**
- * 查询Pdf转换结果
- * @param $task_id
- * @return bool|mixed
- */
- function queryConvertPdf($task_id){
- $uri='/api/developer/v1/tasks/'.$task_id;
- $url=$this->url.$uri;
- $head=$this->wpsSing(2,'uri='.$uri);
- $ret=CommonServer::creatServer()->sendRequest($url,[],'get',$head);
- if(isset($ret['code']) && $ret['code']==0){
- if($ret['data']['status']=='success' && !empty($ret['data']['result']) && count($ret['data']['result']['pdfs'])>0){
- $file_path = 'contractFile/wpspdf/' . date('Ymd') . '/'.$task_id.'/';
- CommonServer::creatServer()->creatDir($file_path);
- $file_name = md5($ret['data']['result']['pdfs'][0]['url'].time()).'.pdf';
- $file_path = $file_path . $file_name;
- file_put_contents(public_path($file_path), file_get_contents($ret['data']['result']['pdfs'][0]['url'])); // 写入文件
- $file_url = 'https://'.env('APP_ADMIN_HOST').'/'.$file_path;
- return $file_url;
- }else{
- return false;
- }
- }else{
- return false;
- }
- }
- }
|