| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- namespace App\Servers\Wps;
- use App\Servers\System\CommonServer;
- use App\Servers\System\RedisDataServer;
- use PhpOffice\PhpWord\IOFactory;
- use PhpOffice\PhpWord\TemplateProcessor;
- use PhpOffice\PhpWord\Writer\HTML;
- class WordServer
- {
- static private $server = null;
- private $propertyKey = [
- 'property_id',
- 'property_contract_sn',
- 'property_name',
- 'property_original_credit',
- 'property_original_debtor',
- 'end_date',
- ];
- function __construct()
- {
- }
- /**
- * 创建对象
- * @return WordServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) self::$server = new WordServer();
- return self::$server;
- }
- /**
- * 验证word文件是否包含某个字符串
- * @param $filePath
- * @param $searchChar
- * @return bool
- */
- function containText($filePath,$searchChar)
- {
- $path=public_path('storage/word_html/'.date('Ymd').'/');
- if(!is_dir($path)){
- CommonServer::creatServer()->creatDir('storage/word_html/'.date('Ymd').'/');
- }
- $htmlPath=$path.md5($filePath).'.html';
- if(!is_file($htmlPath)){
- $phpWord = IOFactory::load($filePath);
- $htmlWriter =new HTML($phpWord);
- $htmlWriter->save($htmlPath);
- }
- $htmlContent = file_get_contents($htmlPath);
- $htmlContent=strip_tags($htmlContent);
- return !strpos($htmlContent,$searchChar)===false;
- }
- /**
- * 合同保存的时候生成文本替换内容
- * @throws \PhpOffice\PhpWord\Exception\Exception
- */
- function replaceWordText($file_path,$file_name,$json)
- {
- $filePath=public_path($file_path);
- $phpWord = new TemplateProcessor($filePath);
- $data = json_decode($json,true);
- foreach ($data as $key=>$text){
- $custom='{$' . $key . '}';
- $phpWord->setValue($custom, $text);
- }
- CommonServer::creatServer()->creatDir('storage/contractComplete');
- $new_path = 'storage/contractComplete/'.$file_name;
- $phpWord->saveAs(public_path($new_path));
- return $new_path;
- }
- /**
- * 获取相关收益信息
- * @param $factoring
- * @param $type
- * @return string
- */
- function getEarningTextKey($factoring,$type)
- {
- $str='0%';
- $type=str_replace($type,'earning_type',$type);
- if(empty($type) || !is_numeric($type)){
- return $str;
- }
- $model=FactoringEarning::where('factoring_id',$factoring['id'])->where('earning_type',$type)->first();
- if(empty($model)){
- return $str;
- }
- return $model->{'earning_num'}.'%('.$model->{'earning_msg'}.')';
- }
- /**
- * 企业基本信息匹配
- * @param $propertys
- * @param $key
- * @param $type
- * @return false|string
- */
- function getPropertyFirmTextKey($propertys,$key,$type)
- {
- $data=[];
- foreach ($propertys as $property){
- if($type=='zrf'){
- $firmName=Firm::where('id',$property['firm_id'])->value('firm_name');
- }elseif ($type=='yszw'){
- $firmName=$property['original_debtor'];
- }elseif ($type=='hxqy'){
- $firmName=$property['core_enterprise'];
- }elseif ($type=='bls'){
- $firmName=Firm::where('id',$property['factor_id'])->value('firm_name');
- }
- if(empty($firmName)){
- return false;
- }
- if($key=='firm_name'){
- $data[]=$firmName;
- }else{
- $firm=$this->getFirmTextKey($firmName);
- if(empty($firm)){
- $data[]='无';
- }else{
- $data[]=empty($firm[$key])?'无':$firm[$key];
- }
- }
- }
- $data=array_unique($data);
- return implode(';',$data);
- }
- /**
- * 获取企业基本信息
- * @param $firmName
- * @return Firm|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed|object|null
- */
- function getFirmTextKey($firmName)
- {
- $firmData=RedisDataServer::creatServer()->getData(md5($firmName),'json');
- if(empty($firmData)){
- $firmData=FirmServer::creatServer()->queryFirm($firmName);
- RedisDataServer::creatServer()->setData(md5($firmName),$firmData,'json');
- }
- return $firmData;
- }
- /**
- * 合同信息获取
- * @param $factoring_contract
- * @param $key
- * @return mixed|string
- */
- function getContractTextKey($factoring_contract,$key)
- {
- $str='';
- if($key=='contract_sn'){
- $str=$factoring_contract->{'contract_sn'};
- }
- return $str;
- }
- /**
- * 获取基础替换信息
- * @param $key
- * @param $factoring_contract
- * @param $factoring
- * @return mixed|void
- */
- function getTextKey($key,$factoring_contract,$factoring)
- {
- if (isset($factoring_contract[$key])){
- return $factoring_contract[$key];
- }
- if (isset($factoring[$key])){
- return $factoring[$key];
- }
- return '';
- }
- /**
- * 获取资产替换信息
- * @param $property
- * @param $key
- * @return string
- */
- function getPropertyTextKey($property,$key)
- {
- $data=[];
- foreach ($property as $value){
- if($key=='id'){
- $data[]=$value->{'id'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='name'){
- $data[]=$value->{'property_name'};
- }elseif ($key=='contract_sn'){
- $data[]=$value->{'contract_sn'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='original_credit'){
- $data[]=$value->{'original_credit'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='original_debtor'){
- $data[]=$value->{'original_debtor'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='end_date'){
- $data[]=$value->{'end_date'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='due_date'){
- $data[]=$value->{'due_date'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='property_sn'){
- $data[]=$value->{'property_sn'} . '('.$value->{'property_name'}.')';
- }
- }
- return implode(';',$data);
- }
- }
|