WordServer.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // dd($filePath);
  61. $phpWord = new TemplateProcessor($filePath);
  62. $data = json_decode($json,true);
  63. foreach ($data as $key=>$text){
  64. $custom='{$' . $key . '}';
  65. $phpWord->setValue($custom, $text);
  66. }
  67. CommonServer::creatServer()->creatDir('storage/contractComplete');
  68. $new_path = 'storage/contractComplete/'.$file_name;
  69. $phpWord->saveAs(public_path($new_path));
  70. return $new_path;
  71. }
  72. /**
  73. * 获取相关收益信息
  74. * @param $factoring
  75. * @param $type
  76. * @return string
  77. */
  78. function getEarningTextKey($factoring,$type)
  79. {
  80. $str='0%';
  81. $type=str_replace($type,'earning_type',$type);
  82. if(empty($type) || !is_numeric($type)){
  83. return $str;
  84. }
  85. $model=FactoringEarning::where('factoring_id',$factoring['id'])->where('earning_type',$type)->first();
  86. if(empty($model)){
  87. return $str;
  88. }
  89. return $model->{'earning_num'}.'%('.$model->{'earning_msg'}.')';
  90. }
  91. /**
  92. * 企业基本信息匹配
  93. * @param $propertys
  94. * @param $key
  95. * @param $type
  96. * @return false|string
  97. */
  98. function getPropertyFirmTextKey($propertys,$key,$type)
  99. {
  100. $data=[];
  101. foreach ($propertys as $property){
  102. if($type=='zrf'){
  103. $firmName=Firm::where('id',$property['firm_id'])->value('firm_name');
  104. }elseif ($type=='yszw'){
  105. $firmName=$property['original_debtor'];
  106. }elseif ($type=='hxqy'){
  107. $firmName=$property['core_enterprise'];
  108. }elseif ($type=='bls'){
  109. $firmName=Firm::where('id',$property['factor_id'])->value('firm_name');
  110. }
  111. if(empty($firmName)){
  112. return false;
  113. }
  114. if($key=='firm_name'){
  115. $data[]=$firmName;
  116. }else{
  117. $firm=$this->getFirmTextKey($firmName);
  118. if(empty($firm)){
  119. $data[]='无';
  120. }else{
  121. $data[]=empty($firm[$key])?'无':$firm[$key];
  122. }
  123. }
  124. }
  125. $data=array_unique($data);
  126. return implode(';',$data);
  127. }
  128. /**
  129. * 获取企业基本信息
  130. * @param $firmName
  131. * @return Firm|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed|object|null
  132. */
  133. function getFirmTextKey($firmName)
  134. {
  135. $firmData=RedisDataServer::creatServer()->getData(md5($firmName),'json');
  136. if(empty($firmData)){
  137. $firmData=FirmServer::creatServer()->queryFirm($firmName);
  138. RedisDataServer::creatServer()->setData(md5($firmName),$firmData,'json');
  139. }
  140. return $firmData;
  141. }
  142. /**
  143. * 合同信息获取
  144. * @param $factoring_contract
  145. * @param $key
  146. * @return mixed|string
  147. */
  148. function getContractTextKey($factoring_contract,$key)
  149. {
  150. $str='';
  151. if($key=='contract_sn'){
  152. $str=$factoring_contract->{'contract_sn'};
  153. }
  154. return $str;
  155. }
  156. /**
  157. * 获取基础替换信息
  158. * @param $key
  159. * @param $factoring_contract
  160. * @param $factoring
  161. * @return mixed|void
  162. */
  163. function getTextKey($key,$factoring_contract,$factoring)
  164. {
  165. if (isset($factoring_contract[$key])){
  166. return $factoring_contract[$key];
  167. }
  168. if (isset($factoring[$key])){
  169. return $factoring[$key];
  170. }
  171. return '';
  172. }
  173. /**
  174. * 获取资产替换信息
  175. * @param $property
  176. * @param $key
  177. * @return string
  178. */
  179. function getPropertyTextKey($property,$key)
  180. {
  181. $data=[];
  182. foreach ($property as $value){
  183. if($key=='id'){
  184. $data[]=$value->{'id'} . '('.$value->{'property_name'}.')';
  185. }elseif ($key=='name'){
  186. $data[]=$value->{'property_name'};
  187. }elseif ($key=='contract_sn'){
  188. $data[]=$value->{'contract_sn'} . '('.$value->{'property_name'}.')';
  189. }elseif ($key=='original_credit'){
  190. $data[]=$value->{'original_credit'} . '('.$value->{'property_name'}.')';
  191. }elseif ($key=='original_debtor'){
  192. $data[]=$value->{'original_debtor'} . '('.$value->{'property_name'}.')';
  193. }elseif ($key=='end_date'){
  194. $data[]=$value->{'end_date'} . '('.$value->{'property_name'}.')';
  195. }elseif ($key=='due_date'){
  196. $data[]=$value->{'due_date'} . '('.$value->{'property_name'}.')';
  197. }elseif ($key=='property_sn'){
  198. $data[]=$value->{'property_sn'} . '('.$value->{'property_name'}.')';
  199. }
  200. }
  201. return implode(';',$data);
  202. }
  203. }