WordServer.php 6.4 KB

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