| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Tools;
- use App\Constants\ErrorCode;
- use Hyperf\Snowflake\IdGeneratorInterface;
- use Hyperf\Context\ApplicationContext;
- class PublicData
- {
- /**
- * 递归查询
- * @param $menuItems
- * @param $parentId
- * @return array
- */
- public static function buildMenuTree($menuItems, $parentId = 0) {
- $tree = [];
- foreach ($menuItems as $item) {
- if ($item['pid'] == $parentId) {
- // 找到子菜单
- $children = self::buildMenuTree($menuItems, $item['id']);
- // 如果子菜单存在,则添加到当前菜单的children中
- if ($children) {
- $item['children'] = $children;
- }
- // 将当前菜单添加到树中
- $tree[] = $item;
- }
- }
- return $tree;
- }
- /**
- * 计算两个时间差的天数
- * @param $sTime
- * @param $eTime
- * @return float
- */
- public static function residueDay($sTime,$eTime)
- {
- $startTime = strtotime($sTime);
- $endTime = strtotime($eTime);
- $diffDays = floor(($endTime - $startTime) / (60 * 60 * 24));
- return $diffDays;
- }
- /**
- * 生成雪花算法ID
- * @return int|false
- */
- public static function uuid()
- {
- try {
- $container = ApplicationContext::getContainer();
- $generator = $container->get(IdGeneratorInterface::class);
- return $generator->generate();
- } catch (\Throwable $e) {
- // 记录错误日志
- error_log("Snowflake ID generation failed: " . $e->getMessage());
- return false;
- }
- }
- public static function im_post($url, $data, $options = [])
- {
- // 初始化CURL会话
- $ch = curl_init($url);
- // JSON 编码(保持中文不转义)
- $jsonBody = is_string($data) ? $data : json_encode($data, JSON_UNESCAPED_UNICODE);
- // 基础选项
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonBody);
- // HTTPS 兼容
- if (stripos($url, 'https://') === 0) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- // Header:Content-Type JSON,Authorization(优先使用传入的)
- $authorization = $options['authorization'] ?? 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI';
- $headers = [
- 'Content-Type: application/json',
- 'Accept: application/json',
- 'Authorization: ' . $authorization,
- ];
- // 允许外部追加自定义 header(数组,形如 ['X-xxx: yyy'])
- if (!empty($options['headers']) && is_array($options['headers'])) {
- $headers = array_merge($headers, $options['headers']);
- }
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- // 超时设置(可通过 options 覆盖)
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int)($options['connect_timeout'] ?? 5));
- curl_setopt($ch, CURLOPT_TIMEOUT, (int)($options['timeout'] ?? 15));
- // 追加额外的 CURL 选项(如需)
- if (!empty($options['curl']) && is_array($options['curl'])) {
- curl_setopt_array($ch, $options['curl']);
- }
- // 执行请求
- $response = curl_exec($ch);
- if ($response === false) {
- $errorMsg = curl_error($ch);
- curl_close($ch);
- throw new \Exception('CURL Error: ' . $errorMsg);
- }
- // 获取HTTP状态码
- $httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
- // 关闭会话
- curl_close($ch);
- // 返回结果
- return json_decode($response,true);
- }
- }
|