WordServer.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Servers\Wps;
  3. use App\Servers\System\CommonServer;
  4. use App\Servers\System\RedisDataServer;
  5. use PhpOffice\PhpWord\IOFactory;
  6. use PhpOffice\PhpWord\TemplateProcessor;
  7. use PhpOffice\PhpWord\Writer\HTML;
  8. class WordServer
  9. {
  10. static private $server = null;
  11. private $propertyKey = [
  12. 'property_id',
  13. 'property_contract_sn',
  14. 'property_name',
  15. 'property_original_credit',
  16. 'property_original_debtor',
  17. 'end_date',
  18. ];
  19. function __construct()
  20. {
  21. }
  22. /**
  23. * 创建对象
  24. * @return WordServer
  25. */
  26. static function creatServer()
  27. {
  28. if (empty(self::$server)) self::$server = new WordServer();
  29. return self::$server;
  30. }
  31. /**
  32. * 验证word文件是否包含某个字符串
  33. * @param $filePath
  34. * @param $searchChar
  35. * @return bool
  36. */
  37. function containText($filePath,$searchChar)
  38. {
  39. $path=public_path('storage/word_html/'.date('Ymd').'/');
  40. if(!is_dir($path)){
  41. CommonServer::creatServer()->creatDir('storage/word_html/'.date('Ymd').'/');
  42. }
  43. $htmlPath=$path.md5($filePath).'.html';
  44. if(!is_file($htmlPath)){
  45. $phpWord = IOFactory::load($filePath);
  46. $htmlWriter =new HTML($phpWord);
  47. $htmlWriter->save($htmlPath);
  48. }
  49. $htmlContent = file_get_contents($htmlPath);
  50. $htmlContent=strip_tags($htmlContent);
  51. return !strpos($htmlContent,$searchChar)===false;
  52. }
  53. /**
  54. * 合同保存的时候生成文本替换内容
  55. * @throws \PhpOffice\PhpWord\Exception\Exception
  56. */
  57. function replaceWordText($file_path,$file_name,$json)
  58. {
  59. $filePath=public_path($file_path);
  60. $phpWord = new TemplateProcessor($filePath);
  61. $data = json_decode($json,true);
  62. foreach ($data as $key=>$text){
  63. $custom='{$' . $key . '}';
  64. $phpWord->setValue($custom, $text);
  65. }
  66. CommonServer::creatServer()->creatDir('storage/contractComplete');
  67. $new_path = 'storage/contractComplete/'.$file_name;
  68. $phpWord->saveAs(public_path($new_path));
  69. return $new_path;
  70. }
  71. /**
  72. * 获取相关收益信息
  73. * @param $factoring
  74. * @param $type
  75. * @return string
  76. */
  77. function getEarningTextKey($factoring,$type)
  78. {
  79. $str='0%';
  80. $type=str_replace($type,'earning_type',$type);
  81. if(empty($type) || !is_numeric($type)){
  82. return $str;
  83. }
  84. $model=FactoringEarning::where('factoring_id',$factoring['id'])->where('earning_type',$type)->first();
  85. if(empty($model)){
  86. return $str;
  87. }
  88. return $model->{'earning_num'}.'%('.$model->{'earning_msg'}.')';
  89. }
  90. /**
  91. * 企业基本信息匹配
  92. * @param $propertys
  93. * @param $key
  94. * @param $type
  95. * @return false|string
  96. */
  97. function getPropertyFirmTextKey($propertys,$key,$type)
  98. {
  99. $data=[];
  100. foreach ($propertys as $property){
  101. if($type=='zrf'){
  102. $firmName=Firm::where('id',$property['firm_id'])->value('firm_name');
  103. }elseif ($type=='yszw'){
  104. $firmName=$property['original_debtor'];
  105. }elseif ($type=='hxqy'){
  106. $firmName=$property['core_enterprise'];
  107. }elseif ($type=='bls'){
  108. $firmName=Firm::where('id',$property['factor_id'])->value('firm_name');
  109. }
  110. if(empty($firmName)){
  111. return false;
  112. }
  113. if($key=='firm_name'){
  114. $data[]=$firmName;
  115. }else{
  116. $firm=$this->getFirmTextKey($firmName);
  117. if(empty($firm)){
  118. $data[]='无';
  119. }else{
  120. $data[]=empty($firm[$key])?'无':$firm[$key];
  121. }
  122. }
  123. }
  124. $data=array_unique($data);
  125. return implode(';',$data);
  126. }
  127. /**
  128. * 获取企业基本信息
  129. * @param $firmName
  130. * @return Firm|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed|object|null
  131. */
  132. function getFirmTextKey($firmName)
  133. {
  134. $firmData=RedisDataServer::creatServer()->getData(md5($firmName),'json');
  135. if(empty($firmData)){
  136. $firmData=FirmServer::creatServer()->queryFirm($firmName);
  137. RedisDataServer::creatServer()->setData(md5($firmName),$firmData,'json');
  138. }
  139. return $firmData;
  140. }
  141. /**
  142. * 合同信息获取
  143. * @param $factoring_contract
  144. * @param $key
  145. * @return mixed|string
  146. */
  147. function getContractTextKey($factoring_contract,$key)
  148. {
  149. $str='';
  150. if($key=='contract_sn'){
  151. $str=$factoring_contract->{'contract_sn'};
  152. }
  153. return $str;
  154. }
  155. /**
  156. * 获取基础替换信息
  157. * @param $key
  158. * @param $factoring_contract
  159. * @param $factoring
  160. * @return mixed|void
  161. */
  162. function getTextKey($key,$factoring_contract,$factoring)
  163. {
  164. if (isset($factoring_contract[$key])){
  165. return $factoring_contract[$key];
  166. }
  167. if (isset($factoring[$key])){
  168. return $factoring[$key];
  169. }
  170. return '';
  171. }
  172. /**
  173. * 获取资产替换信息
  174. * @param $property
  175. * @param $key
  176. * @return string
  177. */
  178. function getPropertyTextKey($property,$key)
  179. {
  180. $data=[];
  181. foreach ($property as $value){
  182. if($key=='id'){
  183. $data[]=$value->{'id'} . '('.$value->{'property_name'}.')';
  184. }elseif ($key=='name'){
  185. $data[]=$value->{'property_name'};
  186. }elseif ($key=='contract_sn'){
  187. $data[]=$value->{'contract_sn'} . '('.$value->{'property_name'}.')';
  188. }elseif ($key=='original_credit'){
  189. $data[]=$value->{'original_credit'} . '('.$value->{'property_name'}.')';
  190. }elseif ($key=='original_debtor'){
  191. $data[]=$value->{'original_debtor'} . '('.$value->{'property_name'}.')';
  192. }elseif ($key=='end_date'){
  193. $data[]=$value->{'end_date'} . '('.$value->{'property_name'}.')';
  194. }elseif ($key=='due_date'){
  195. $data[]=$value->{'due_date'} . '('.$value->{'property_name'}.')';
  196. }elseif ($key=='property_sn'){
  197. $data[]=$value->{'property_sn'} . '('.$value->{'property_name'}.')';
  198. }
  199. }
  200. return implode(';',$data);
  201. }
  202. }