PublicData.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /**
  31. * 把数组里面的某一个字段作为key
  32. * @param $array
  33. * @param $column
  34. * @return array
  35. */
  36. public static function arrayColumnAsKey($array, $column) {
  37. $result = [];
  38. foreach ($array as $item) {
  39. if (isset($item[$column])) {
  40. $result[$item[$column]] = $item;
  41. } else {
  42. // 如果字段不存在,你可以选择跳过、使用默认值或抛出异常
  43. // 这里我们选择跳过
  44. continue;
  45. }
  46. }
  47. return $result;
  48. }
  49. /**
  50. * 计算两个时间差的天数
  51. * @param $sTime
  52. * @param $eTime
  53. * @return float
  54. */
  55. public static function residueDay($sTime,$eTime)
  56. {
  57. $startTime = strtotime($sTime);
  58. $endTime = strtotime($eTime);
  59. $diffDays = floor(($endTime - $startTime) / (60 * 60 * 24));
  60. return $diffDays;
  61. }
  62. public static function uuid()
  63. {
  64. $container = ApplicationContext::getContainer();
  65. $generator = $container->get(IdGeneratorInterface::class);
  66. return $generator->generate();
  67. }
  68. public static function http_get($url)
  69. {
  70. $oCurl = curl_init();
  71. if (stripos($url, "https://") !== false) {
  72. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  73. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  74. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  75. }
  76. curl_setopt($oCurl, CURLOPT_URL, $url);
  77. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  78. $sContent = curl_exec($oCurl);
  79. $aStatus = curl_getinfo($oCurl);
  80. curl_close($oCurl);
  81. if (intval($aStatus["http_code"]) == 200) {
  82. return $sContent;
  83. } else {
  84. return false;
  85. }
  86. }
  87. /**
  88. * POST 请求
  89. * @param string $url
  90. * @param array $param
  91. * @param boolean $post_file 是否文件上传
  92. * @return string content
  93. */
  94. public static function http_post($url, $param, $post_file = false)
  95. {
  96. $oCurl = curl_init();
  97. if (stripos($url, "https://") !== false) {
  98. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  99. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  100. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  101. }
  102. if (is_string($param) || $post_file) {
  103. $strPOST = $param;
  104. } else {
  105. $aPOST = [];
  106. foreach ($param as $key => $val) {
  107. $aPOST[] = $key . "=" . urlencode($val);
  108. }
  109. $strPOST = join("&", $aPOST);
  110. }
  111. curl_setopt($oCurl, CURLOPT_URL, $url);
  112. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  113. curl_setopt($oCurl, CURLOPT_POST, true);
  114. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
  115. $sContent = curl_exec($oCurl);
  116. $aStatus = curl_getinfo($oCurl);
  117. curl_close($oCurl);
  118. if (intval($aStatus["http_code"]) == 200) {
  119. return $sContent;
  120. } else {
  121. return false;
  122. }
  123. }
  124. }