Service.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Service\Wx;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Redis;
  5. use Illuminate\Support\Facades\Storage;
  6. /**
  7. * 公用的公共服务
  8. * @package App\Models
  9. */
  10. class Service
  11. {
  12. public $log;
  13. public $total = 0;
  14. public function __construct()
  15. {
  16. }
  17. function curlOpen($url, $config = array())
  18. {
  19. $arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 100, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>true,'isupfile'=>false,'returnheader'=>false,'request'=>'post');
  20. $arr = array_merge($arr, $config);
  21. $ch = curl_init();
  22. curl_setopt($ch, CURLOPT_URL, $url);
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
  24. curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);
  25. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  26. curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
  27. curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
  28. curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
  29. curl_setopt($ch, CURLOPT_HEADER, $arr['returnheader']);//��ȡheader
  30. if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
  31. if($arr['ssl'])
  32. {
  33. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  35. }
  36. if(!empty($arr['cookie']))
  37. {
  38. curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
  39. curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
  40. }
  41. if(!empty($arr['proxy']))
  42. {
  43. //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  44. curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
  45. if(!empty($arr['userpwd']))
  46. {
  47. curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
  48. }
  49. }
  50. //ip�Ƚ����⣬�ü�ֵ��ʾ
  51. if(!empty($arr['header']['ip']))
  52. {
  53. array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
  54. unset($arr['header']['ip']);
  55. }
  56. $arr['header'] = array_filter($arr['header']);
  57. if(!empty($arr['header']))
  58. {
  59. curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
  60. }
  61. if($arr['request'] === 'put'){
  62. curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  63. curl_setopt($ch, CURLOPT_POSTFIELDS,$arr['post']);
  64. }elseif($arr['post'] != false)
  65. {
  66. curl_setopt($ch, CURLOPT_POST, true);
  67. if(is_array($arr['post']) && $arr['isupfile'] === false)
  68. {
  69. $post = http_build_query($arr['post']);
  70. }
  71. else
  72. {
  73. $post = $arr['post'];
  74. }
  75. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  76. }
  77. $result = curl_exec($ch);
  78. curl_close($ch);
  79. return $result;
  80. }
  81. //分页共用
  82. public function limit($db, $columns, $request)
  83. {
  84. $per_page = $request['page_size'] ?? 9999;
  85. $page = $request['page_index'] ?? 1;
  86. $return = $db->paginate($per_page, $columns, 'page', $page)->toArray();
  87. $data['total'] = $return['total'];
  88. $data['data'] = $return['data'];
  89. return $data;
  90. }
  91. //判断是否为空
  92. protected function isEmpty($data, $word)
  93. {
  94. if (isset($data[$word]) && (!empty($data[$word]) || $data[$word] === '0'|| $data[$word] === 0)) return false;
  95. return true;
  96. }
  97. //后台端 某些需要限制请求频率的接口
  98. public function limitingSendRequestBackg($key,$ttl = 5){
  99. if($ttl < 5) $ttl = 5;
  100. // 使用Redis Facade设置,当键名不存在时才设置成功
  101. if (Redis::setnx($key, 1)) {
  102. Redis::expire($key, $ttl); //多少秒后过期
  103. return [true, ''];
  104. }
  105. return [false,'操作频繁, 请在 ' . $ttl . '秒后重试'];
  106. }
  107. public function delStorageFile($old, $new = [], $dir = "upload_files/"){
  108. foreach ($old as $value){
  109. if(! in_array($value, $new)){
  110. $filename_rep = "/api/uploadFiles/";
  111. $filename = str_replace($filename_rep, "", $value);
  112. $filePath = $dir . $filename;
  113. if (Storage::disk('public')->exists($filePath)) {
  114. // 文件存在 进行删除操作
  115. Storage::disk('public')->delete($filePath);
  116. }
  117. }
  118. }
  119. }
  120. }