PublicData.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. }