PublicData.php 9.0 KB

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