PublicData.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * 计算两个时间差的天数
  31. * @param $sTime
  32. * @param $eTime
  33. * @return float
  34. */
  35. public static function residueDay($sTime,$eTime)
  36. {
  37. $startTime = strtotime($sTime);
  38. $endTime = strtotime($eTime);
  39. $diffDays = floor(($endTime - $startTime) / (60 * 60 * 24));
  40. return $diffDays;
  41. }
  42. public static function uuid()
  43. {
  44. $container = ApplicationContext::getContainer();
  45. $generator = $container->get(IdGeneratorInterface::class);
  46. return $generator->generate();
  47. }
  48. public static function http_get($url)
  49. {
  50. $oCurl = curl_init();
  51. if (stripos($url, "https://") !== false) {
  52. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  53. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  54. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  55. }
  56. curl_setopt($oCurl, CURLOPT_URL, $url);
  57. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  58. $sContent = curl_exec($oCurl);
  59. $aStatus = curl_getinfo($oCurl);
  60. curl_close($oCurl);
  61. if (intval($aStatus["http_code"]) == 200) {
  62. return $sContent;
  63. } else {
  64. return false;
  65. }
  66. }
  67. /**
  68. * POST 请求
  69. * @param string $url
  70. * @param array $param
  71. * @param boolean $post_file 是否文件上传
  72. * @return string content
  73. */
  74. public static function http_post($url, $param, $post_file = false)
  75. {
  76. $oCurl = curl_init();
  77. if (stripos($url, "https://") !== false) {
  78. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  79. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  80. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  81. }
  82. if (is_string($param) || $post_file) {
  83. $strPOST = $param;
  84. } else {
  85. $aPOST = [];
  86. foreach ($param as $key => $val) {
  87. $aPOST[] = $key . "=" . urlencode($val);
  88. }
  89. $strPOST = join("&", $aPOST);
  90. }
  91. curl_setopt($oCurl, CURLOPT_URL, $url);
  92. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  93. curl_setopt($oCurl, CURLOPT_POST, true);
  94. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
  95. $sContent = curl_exec($oCurl);
  96. $aStatus = curl_getinfo($oCurl);
  97. curl_close($oCurl);
  98. if (intval($aStatus["http_code"]) == 200) {
  99. return $sContent;
  100. } else {
  101. return false;
  102. }
  103. }
  104. }