Service.php 7.5 KB

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