PublicData.php 9.0 KB

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