PublicData.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace App\Tools;
  3. use Hyperf\Snowflake\IdGeneratorInterface;
  4. use Hyperf\Context\ApplicationContext;
  5. use function Hyperf\Support\env;
  6. class PublicData
  7. {
  8. /**
  9. * ceshi-todo
  10. * 递归查询
  11. * @param $menuItems
  12. * @param $parentId
  13. * @return array
  14. */
  15. public static function buildMenuTree($menuItems, $parentId = 0)
  16. {
  17. $tree = [];
  18. foreach ($menuItems as $item) {
  19. if ($item['pid'] == $parentId) {
  20. // 找到子菜单
  21. $children = self::buildMenuTree($menuItems, $item['id']);
  22. // 如果子菜单存在,则添加到当前菜单的children中
  23. if ($children) {
  24. $item['children'] = $children;
  25. } else {
  26. $item['children'] = [];
  27. }
  28. // 将当前菜单添加到树中
  29. $tree[] = $item;
  30. }
  31. }
  32. return $tree;
  33. }
  34. public static function buildCategoryTree($menuItems, $parentId = 0)
  35. {
  36. $tree = [];
  37. foreach ($menuItems as $item) {
  38. if ($item['pid'] == $parentId) {
  39. // 找到子菜单
  40. $children = self::buildCategoryTree($menuItems, $item['category_id']);
  41. // 如果子菜单存在,则添加到当前菜单的children中
  42. if ($children) {
  43. $item['children'] = $children;
  44. }
  45. // 将当前菜单添加到树中
  46. $tree[] = $item;
  47. }
  48. }
  49. return $tree;
  50. }
  51. /**
  52. * 把数组里面的某一个字段作为key
  53. * @param $array
  54. * @param $column
  55. * @return array
  56. */
  57. public static function arrayColumnAsKey($array, $column)
  58. {
  59. $result = [];
  60. foreach ($array as $item) {
  61. if (isset($item[$column])) {
  62. $result[$item[$column]] = $item;
  63. } else {
  64. // 如果字段不存在,你可以选择跳过、使用默认值或抛出异常
  65. // 这里我们选择跳过
  66. continue;
  67. }
  68. }
  69. return $result;
  70. }
  71. /**
  72. * 计算两个时间差的天数
  73. * @param $sTime
  74. * @param $eTime
  75. * @return float
  76. */
  77. public static function residueDay($sTime, $eTime)
  78. {
  79. $startTime = strtotime($sTime);
  80. $endTime = strtotime($eTime);
  81. $diffDays = floor(($endTime - $startTime) / (60 * 60 * 24));
  82. return $diffDays;
  83. }
  84. public static function uuid()
  85. {
  86. $container = ApplicationContext::getContainer();
  87. $generator = $container->get(IdGeneratorInterface::class);
  88. return $generator->generate();
  89. }
  90. public static function http_get($url)
  91. {
  92. $oCurl = curl_init();
  93. if (stripos($url, "https://") !== false) {
  94. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  95. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  96. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  97. }
  98. curl_setopt($oCurl, CURLOPT_URL, $url);
  99. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  100. $sContent = curl_exec($oCurl);
  101. $aStatus = curl_getinfo($oCurl);
  102. curl_close($oCurl);
  103. if (intval($aStatus["http_code"]) == 200) {
  104. return $sContent;
  105. } else {
  106. return false;
  107. }
  108. }
  109. /**
  110. * POST 请求
  111. * @param string $url
  112. * @param array $param
  113. * @param boolean $post_file 是否文件上传
  114. * @return string content
  115. */
  116. public static function http_post($url, $param, $post_file = false)
  117. {
  118. $oCurl = curl_init();
  119. if (stripos($url, "https://") !== false) {
  120. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  121. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  122. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  123. }
  124. if (is_string($param) || $post_file) {
  125. $strPOST = $param;
  126. } else {
  127. $aPOST = [];
  128. foreach ($param as $key => $val) {
  129. $aPOST[] = $key . "=" . urlencode($val);
  130. }
  131. $strPOST = join("&", $aPOST);
  132. }
  133. curl_setopt($oCurl, CURLOPT_URL, $url);
  134. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  135. curl_setopt($oCurl, CURLOPT_POST, true);
  136. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
  137. $sContent = curl_exec($oCurl);
  138. $aStatus = curl_getinfo($oCurl);
  139. curl_close($oCurl);
  140. if (intval($aStatus["http_code"]) == 200) {
  141. return $sContent;
  142. } else {
  143. return false;
  144. }
  145. }
  146. /**
  147. * POST 请求
  148. * @param string $url
  149. * @param array $param
  150. * @param boolean $post_file 是否文件上传
  151. * @return string content
  152. */
  153. public static function http_post_zp($url, $data, $options = [])
  154. {
  155. // 初始化CURL会话
  156. $ch = curl_init($url);
  157. var_dump("参数:", $data);
  158. // 设置CURL选项
  159. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
  160. curl_setopt($ch, CURLOPT_HEADER, false); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
  161. curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的POST请求。
  162. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // 要传递的POST数据,这里使用http_build_query将数组转换为URL编码的查询字符串。
  163. $headers = [
  164. 'Authorization: Bearer be1856920c54ac537b530d69bc2eda73.gOO2BMq9NXavzEMq',
  165. 'Content-Type: application/json',
  166. // 'Custom-Header: customHeaderValue'
  167. ];
  168. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  169. // 如果有额外的CURL选项,则合并它们
  170. if (!empty($options)) {
  171. curl_setopt_array($ch, $options);
  172. }
  173. // 执行CURL会话并获取响应
  174. $response = curl_exec($ch);
  175. // 检查是否有CURL错误
  176. if (curl_errno($ch)) {
  177. $error_msg = curl_error($ch);
  178. curl_close($ch);
  179. throw new Exception("CURL Error: $error_msg");
  180. }
  181. // 获取HTTP状态码
  182. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  183. // 关闭CURL会话
  184. curl_close($ch);
  185. // 返回一个包含响应和HTTP状态码的数组
  186. $responseBody = $response;
  187. $headerEnd = strpos($response, "\r\n\r\n");
  188. if ($headerEnd !== false) {
  189. // 去除响应头,只保留响应体
  190. $responseBody = substr($response, $headerEnd + 4); // +4 是因为 "\r\n\r\n" 有4个字符
  191. // echo $responseBody; // 输出:This is the response body.
  192. } else {
  193. // 如果没有找到空行,可能响应格式不正确或没有响应头
  194. // echo "No headers found in response.";
  195. }
  196. return [
  197. 'response' => $responseBody,
  198. 'http_code' => $http_code
  199. ];
  200. }
  201. /**
  202. * 历史上的今天
  203. * @return mixed
  204. */
  205. public static function getHistoryToday()
  206. {
  207. $api_url = "https://api.52vmy.cn/api/wl/today?type=json";
  208. $ch = curl_init();
  209. curl_setopt($ch, CURLOPT_URL, $api_url);
  210. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  211. $response = curl_exec($ch);
  212. curl_close($ch);
  213. $data = json_decode($response, true);
  214. return $data;
  215. }
  216. /**
  217. * 获取谜语
  218. * @return mixed
  219. */
  220. public static function addRiddle()
  221. {
  222. $api_url = "https://v.api.aa1.cn/api/api-miyu/index.php";
  223. $ch = curl_init();
  224. curl_setopt($ch, CURLOPT_URL, $api_url);
  225. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  226. $response = curl_exec($ch);
  227. curl_close($ch);
  228. var_dump("返回数据:", $response);
  229. $cleanStr = preg_replace('/<script[^>]*?>.*?<\/script>/is', '', $response);
  230. $jsonStr = trim(preg_replace('/\s+/', ' ', $cleanStr)); // 压缩空白符
  231. $jsonStr = preg_replace('/^.*?({.*})$/', '$1', $jsonStr); // 提取最后一个完整 JSON
  232. $arr = json_decode($jsonStr, true);
  233. if (json_last_error() !== JSON_ERROR_NONE) {
  234. return json_last_error_msg();
  235. }
  236. var_dump("返回数据111:", $response);
  237. return $arr;
  238. }
  239. /**
  240. * @param $text
  241. * @return bool|string
  242. */
  243. public static function getCouplet($text = "")
  244. {
  245. if ($text) {
  246. $api_url = "https://apis.tianapi.com/msdl/index?key=44c4f9b3d66ce06900a098c78b90478f&num=10&fenlei=" . urlencode($text);
  247. } else {
  248. $api_url = "https://apis.tianapi.com/msdl/index?key=44c4f9b3d66ce06900a098c78b90478f&num=10";
  249. }
  250. var_dump("请求地址:", $api_url);
  251. $ch = curl_init();
  252. curl_setopt($ch, CURLOPT_URL, $api_url);
  253. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  254. $response = curl_exec($ch);
  255. curl_close($ch);
  256. $data = json_decode($response, true);
  257. return $data;
  258. }
  259. public static function im_post($url, $data, $options = [])
  260. {
  261. // 初始化CURL会话
  262. $ch = curl_init($url);
  263. // JSON 编码(保持中文不转义)
  264. $jsonBody = is_string($data) ? $data : json_encode($data, JSON_UNESCAPED_UNICODE);
  265. // 基础选项
  266. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  267. curl_setopt($ch, CURLOPT_HEADER, false);
  268. curl_setopt($ch, CURLOPT_POST, true);
  269. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonBody);
  270. // HTTPS 兼容
  271. if (stripos($url, 'https://') === 0) {
  272. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  273. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  274. }
  275. // Header:Content-Type JSON,Authorization(优先使用传入的)
  276. $authorization = $options['authorization'] ?? 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI';
  277. $headers = [
  278. 'Content-Type: application/json',
  279. 'Accept: application/json',
  280. 'Authorization: ' . $authorization,
  281. ];
  282. // 允许外部追加自定义 header(数组,形如 ['X-xxx: yyy'])
  283. if (!empty($options['headers']) && is_array($options['headers'])) {
  284. $headers = array_merge($headers, $options['headers']);
  285. }
  286. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  287. // 超时设置(可通过 options 覆盖)
  288. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int)($options['connect_timeout'] ?? 5));
  289. curl_setopt($ch, CURLOPT_TIMEOUT, (int)($options['timeout'] ?? 15));
  290. // 追加额外的 CURL 选项(如需)
  291. if (!empty($options['curl']) && is_array($options['curl'])) {
  292. curl_setopt_array($ch, $options['curl']);
  293. }
  294. // 执行请求
  295. $response = curl_exec($ch);
  296. if ($response === false) {
  297. $errorMsg = curl_error($ch);
  298. curl_close($ch);
  299. throw new \Exception('CURL Error: ' . $errorMsg);
  300. }
  301. // 获取HTTP状态码
  302. $httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
  303. // 关闭会话
  304. curl_close($ch);
  305. // 返回结果
  306. return json_decode($response, true);
  307. }
  308. }