PublicData.php 6.9 KB

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