Service.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Service;
  3. //use App\Service\LogService;
  4. use Illuminate\Support\Facades\File;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Redis;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Storage;
  9. /**
  10. * 公用的公共服务
  11. * @package App\Models
  12. */
  13. class Service
  14. {
  15. public $log;
  16. public $total = 0;
  17. public function __construct()
  18. {
  19. }
  20. //分页共用
  21. public function limit($db, $columns, $request)
  22. {
  23. $per_page = $request['page_size'] ?? 9999;
  24. $page = $request['page_index'] ?? 1;
  25. $return = $db->paginate($per_page, $columns, 'page', $page)->toArray();
  26. $data['total'] = $return['total'];
  27. $data['data'] = $return['data'];
  28. return $data;
  29. }
  30. //抽象一个查询条件,省的每天写很多行
  31. protected function is_exist_db($data, $word, $words, $db, $formula = '=', $type = '')
  32. {
  33. if (isset($data[$word]) && ($data[$word] !== null && $data[$word] !== '' && $data[$word] !== [])) {
  34. switch ($type) {
  35. case 'time':
  36. $data[$word] = ctype_digit($data[$word]) ? $data[$word] : strtotime($data[$word]);
  37. if ($formula == '<'||$formula == '<=') $data[$word] += 86400;
  38. $db = $db->where($words, $formula, $data[$word]);
  39. break;
  40. case 'like':
  41. $db = $db->where($words, $type, '%' . $data[$word] . '%');
  42. break;
  43. case 'in':
  44. if (is_array($data[$word])) {
  45. $db = $db->wherein($words, $data[$word]);
  46. } else {
  47. $db = $db->wherein($words, explode(',', $data[$word]));
  48. }
  49. break;
  50. default:
  51. $db = $db->where($words, $formula, $data[$word]);
  52. break;
  53. }
  54. return $db;
  55. }
  56. return $db;
  57. }
  58. //判断是否为空
  59. protected function isEmpty($data, $word)
  60. {
  61. if (isset($data[$word]) && (!empty($data[$word]) || $data[$word] === '0'|| $data[$word] === 0)) return false;
  62. return true;
  63. }
  64. //递归处理数据成子父级关系
  65. public function makeTree($parentId, &$node)
  66. {
  67. $tree = [];
  68. foreach ($node as $key => $value) {
  69. if ($value['parent_id'] == $parentId) {
  70. $tree[$value['id']] = $value;
  71. unset($node[$key]);
  72. if (isset($tree[$value['id']]['children'])) {
  73. $tree[$value['id']]['children'] = array_merge($tree[$value['id']]['children'], $this->makeTree($value['id'], $node));
  74. } else {
  75. $tree[$value['id']]['children'] = $this->makeTree($value['id'], $node);
  76. }
  77. }
  78. }
  79. return $tree;
  80. }
  81. //进行递归处理数组的排序问题
  82. public function set_sort_circle($data){
  83. foreach ($data as $k=>$v){
  84. // var_dump($v);die;
  85. if(isset($v['children'])&&!empty($v['children'])){
  86. $data[$k]['children'] = $this->set_sort_circle($v['children']);
  87. }
  88. }
  89. $data = array_merge($data);
  90. return $data;
  91. }
  92. //根据编码规则获取每一级的code
  93. public function get_rule_code($code, $rule)
  94. {
  95. $rules = [];
  96. $num = 0;
  97. foreach ($rule as $v) {
  98. $num += $v['num'];
  99. $code = substr($code, 0, $num);
  100. if (strlen($code) != $num) continue;
  101. $rules[] = $code;
  102. }
  103. return $rules;
  104. }
  105. function curlOpen($url, $config = array())
  106. {
  107. $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');
  108. $arr = array_merge($arr, $config);
  109. $ch = curl_init();
  110. curl_setopt($ch, CURLOPT_URL, $url);
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
  112. curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);
  113. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  114. curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
  115. curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
  116. curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
  117. curl_setopt($ch, CURLOPT_HEADER, $arr['returnheader']);//��ȡheader
  118. if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
  119. if($arr['ssl'])
  120. {
  121. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  122. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  123. }
  124. if(!empty($arr['cookie']))
  125. {
  126. curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
  127. curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
  128. }
  129. if(!empty($arr['proxy']))
  130. {
  131. //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  132. curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
  133. if(!empty($arr['userpwd']))
  134. {
  135. curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
  136. }
  137. }
  138. //ip�Ƚ����⣬�ü�ֵ��ʾ
  139. if(!empty($arr['header']['ip']))
  140. {
  141. array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
  142. unset($arr['header']['ip']);
  143. }
  144. $arr['header'] = array_filter($arr['header']);
  145. if(!empty($arr['header']))
  146. {
  147. curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
  148. }
  149. if($arr['request'] === 'put'){
  150. curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  151. curl_setopt($ch, CURLOPT_POSTFIELDS,$arr['post']);
  152. }elseif($arr['post'] != false)
  153. {
  154. curl_setopt($ch, CURLOPT_POST, true);
  155. if(is_array($arr['post']) && $arr['isupfile'] === false)
  156. {
  157. $post = http_build_query($arr['post']);
  158. }
  159. else
  160. {
  161. $post = $arr['post'];
  162. }
  163. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  164. }
  165. $result = curl_exec($ch);
  166. curl_close($ch);
  167. return $result;
  168. }
  169. }