PublicData.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Tools;
  3. use App\Constants\ErrorCode;
  4. use Hyperf\Snowflake\IdGeneratorInterface;
  5. use Hyperf\Context\ApplicationContext;
  6. class PublicData
  7. {
  8. /**
  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. * 计算两个时间差的天数
  32. * @param $sTime
  33. * @param $eTime
  34. * @return float
  35. */
  36. public static function residueDay($sTime,$eTime)
  37. {
  38. $startTime = strtotime($sTime);
  39. $endTime = strtotime($eTime);
  40. $diffDays = floor(($endTime - $startTime) / (60 * 60 * 24));
  41. return $diffDays;
  42. }
  43. /**
  44. * 生成雪花算法ID
  45. * @return int|false
  46. */
  47. public static function uuid()
  48. {
  49. try {
  50. $container = ApplicationContext::getContainer();
  51. $generator = $container->get(IdGeneratorInterface::class);
  52. return $generator->generate();
  53. } catch (\Throwable $e) {
  54. // 记录错误日志
  55. error_log("Snowflake ID generation failed: " . $e->getMessage());
  56. return false;
  57. }
  58. }
  59. public static function im_post($url, $data, $options = [])
  60. {
  61. // 初始化CURL会话
  62. $ch = curl_init($url);
  63. // JSON 编码(保持中文不转义)
  64. $jsonBody = is_string($data) ? $data : json_encode($data, JSON_UNESCAPED_UNICODE);
  65. // 基础选项
  66. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  67. curl_setopt($ch, CURLOPT_HEADER, false);
  68. curl_setopt($ch, CURLOPT_POST, true);
  69. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonBody);
  70. // HTTPS 兼容
  71. if (stripos($url, 'https://') === 0) {
  72. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  73. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  74. }
  75. // Header:Content-Type JSON,Authorization(优先使用传入的)
  76. $authorization = $options['authorization'] ?? 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI';
  77. $headers = [
  78. 'Content-Type: application/json',
  79. 'Accept: application/json',
  80. 'Authorization: ' . $authorization,
  81. ];
  82. // 允许外部追加自定义 header(数组,形如 ['X-xxx: yyy'])
  83. if (!empty($options['headers']) && is_array($options['headers'])) {
  84. $headers = array_merge($headers, $options['headers']);
  85. }
  86. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  87. // 超时设置(可通过 options 覆盖)
  88. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int)($options['connect_timeout'] ?? 5));
  89. curl_setopt($ch, CURLOPT_TIMEOUT, (int)($options['timeout'] ?? 15));
  90. // 追加额外的 CURL 选项(如需)
  91. if (!empty($options['curl']) && is_array($options['curl'])) {
  92. curl_setopt_array($ch, $options['curl']);
  93. }
  94. // 执行请求
  95. $response = curl_exec($ch);
  96. if ($response === false) {
  97. $errorMsg = curl_error($ch);
  98. curl_close($ch);
  99. throw new \Exception('CURL Error: ' . $errorMsg);
  100. }
  101. // 获取HTTP状态码
  102. $httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
  103. // 关闭会话
  104. curl_close($ch);
  105. // 返回结果
  106. return json_decode($response,true);
  107. }
  108. }