PublicData.php 6.9 KB

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