PublicData.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Tools;
  3. use Hyperf\Snowflake\IdGeneratorInterface;
  4. use Hyperf\Context\ApplicationContext;
  5. class PublicData
  6. {
  7. /**
  8. * 递归查询
  9. * @param $menuItems
  10. * @param $parentId
  11. * @return array
  12. */
  13. public static function buildMenuTree($menuItems, $parentId = 0) {
  14. $tree = [];
  15. foreach ($menuItems as $item) {
  16. if ($item['pid'] == $parentId) {
  17. // 找到子菜单
  18. $children = self::buildMenuTree($menuItems, $item['id']);
  19. // 如果子菜单存在,则添加到当前菜单的children中
  20. if ($children) {
  21. $item['children'] = $children;
  22. }
  23. // 将当前菜单添加到树中
  24. $tree[] = $item;
  25. }
  26. }
  27. return $tree;
  28. }
  29. /**
  30. * 把数组里面的某一个字段作为key
  31. * @param $array
  32. * @param $column
  33. * @return array
  34. */
  35. public static function arrayColumnAsKey($array, $column) {
  36. $result = [];
  37. foreach ($array as $item) {
  38. if (isset($item[$column])) {
  39. $result[$item[$column]] = $item;
  40. } else {
  41. // 如果字段不存在,你可以选择跳过、使用默认值或抛出异常
  42. // 这里我们选择跳过
  43. continue;
  44. }
  45. }
  46. return $result;
  47. }
  48. /**
  49. * 计算两个时间差的天数
  50. * @param $sTime
  51. * @param $eTime
  52. * @return float
  53. */
  54. public static function residueDay($sTime,$eTime)
  55. {
  56. $startTime = strtotime($sTime);
  57. $endTime = strtotime($eTime);
  58. $diffDays = floor(($endTime - $startTime) / (60 * 60 * 24));
  59. return $diffDays;
  60. }
  61. public static function uuid()
  62. {
  63. $container = ApplicationContext::getContainer();
  64. $generator = $container->get(IdGeneratorInterface::class);
  65. return $generator->generate();
  66. }
  67. public static function http_get($url)
  68. {
  69. $oCurl = curl_init();
  70. if (stripos($url, "https://") !== false) {
  71. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  72. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  73. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  74. }
  75. curl_setopt($oCurl, CURLOPT_URL, $url);
  76. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  77. $sContent = curl_exec($oCurl);
  78. $aStatus = curl_getinfo($oCurl);
  79. curl_close($oCurl);
  80. if (intval($aStatus["http_code"]) == 200) {
  81. return $sContent;
  82. } else {
  83. return false;
  84. }
  85. }
  86. /**
  87. * POST 请求
  88. * @param string $url
  89. * @param array $param
  90. * @param boolean $post_file 是否文件上传
  91. * @return string content
  92. */
  93. public static function http_post($url, $param, $post_file = false)
  94. {
  95. $oCurl = curl_init();
  96. if (stripos($url, "https://") !== false) {
  97. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  98. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  99. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  100. }
  101. if (is_string($param) || $post_file) {
  102. $strPOST = $param;
  103. } else {
  104. $aPOST = [];
  105. foreach ($param as $key => $val) {
  106. $aPOST[] = $key . "=" . urlencode($val);
  107. }
  108. $strPOST = join("&", $aPOST);
  109. }
  110. curl_setopt($oCurl, CURLOPT_URL, $url);
  111. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  112. curl_setopt($oCurl, CURLOPT_POST, true);
  113. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
  114. $sContent = curl_exec($oCurl);
  115. $aStatus = curl_getinfo($oCurl);
  116. curl_close($oCurl);
  117. if (intval($aStatus["http_code"]) == 200) {
  118. return $sContent;
  119. } else {
  120. return false;
  121. }
  122. }
  123. /**
  124. * POST 请求
  125. * @param string $url
  126. * @param array $param
  127. * @param boolean $post_file 是否文件上传
  128. * @return string content
  129. */
  130. public static function http_post_zp($url, $data, $options = [])
  131. {
  132. // 初始化CURL会话
  133. $ch = curl_init($url);
  134. var_dump("参数:",$data);
  135. // 设置CURL选项
  136. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
  137. curl_setopt($ch, CURLOPT_HEADER, false); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
  138. curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的POST请求。
  139. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // 要传递的POST数据,这里使用http_build_query将数组转换为URL编码的查询字符串。
  140. $headers = [
  141. 'Authorization: Bearer be1856920c54ac537b530d69bc2eda73.gOO2BMq9NXavzEMq',
  142. 'Content-Type: application/json',
  143. // 'Custom-Header: customHeaderValue'
  144. ];
  145. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  146. // 如果有额外的CURL选项,则合并它们
  147. if (!empty($options)) {
  148. curl_setopt_array($ch, $options);
  149. }
  150. // 执行CURL会话并获取响应
  151. $response = curl_exec($ch);
  152. // 检查是否有CURL错误
  153. if (curl_errno($ch)) {
  154. $error_msg = curl_error($ch);
  155. curl_close($ch);
  156. throw new Exception("CURL Error: $error_msg");
  157. }
  158. // 获取HTTP状态码
  159. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  160. // 关闭CURL会话
  161. curl_close($ch);
  162. // 返回一个包含响应和HTTP状态码的数组
  163. $responseBody = $response;
  164. $headerEnd = strpos($response, "\r\n\r\n");
  165. if ($headerEnd !== false) {
  166. // 去除响应头,只保留响应体
  167. $responseBody = substr($response, $headerEnd + 4); // +4 是因为 "\r\n\r\n" 有4个字符
  168. // echo $responseBody; // 输出:This is the response body.
  169. } else {
  170. // 如果没有找到空行,可能响应格式不正确或没有响应头
  171. // echo "No headers found in response.";
  172. }
  173. return [
  174. 'response' => $responseBody,
  175. 'http_code' => $http_code
  176. ];
  177. }
  178. }