123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace App\Tools;
- use Hyperf\Snowflake\IdGeneratorInterface;
- use Hyperf\Context\ApplicationContext;
- use function Hyperf\Support\env;
- class PublicData
- {
-
- public static function buildMenuTree($menuItems, $parentId = 0) {
- $tree = [];
- foreach ($menuItems as $item) {
- if ($item['pid'] == $parentId) {
-
- $children = self::buildMenuTree($menuItems, $item['id']);
-
- if ($children) {
- $item['children'] = $children;
- }
-
- $tree[] = $item;
- }
- }
- return $tree;
- }
- public static function buildCategoryTree($menuItems, $parentId = 0) {
- $tree = [];
- foreach ($menuItems as $item) {
- if ($item['pid'] == $parentId) {
-
- $children = self::buildMenuTree($menuItems, $item['category_id']);
-
- if ($children) {
- $item['children'] = $children;
- }
-
- $tree[] = $item;
- }
- }
- return $tree;
- }
-
- public static function arrayColumnAsKey($array, $column) {
- $result = [];
- foreach ($array as $item) {
- if (isset($item[$column])) {
- $result[$item[$column]] = $item;
- } else {
-
-
- continue;
- }
- }
- return $result;
- }
-
- public static function residueDay($sTime,$eTime)
- {
- $startTime = strtotime($sTime);
- $endTime = strtotime($eTime);
- $diffDays = floor(($endTime - $startTime) / (60 * 60 * 24));
- return $diffDays;
- }
- public static function uuid()
- {
- $container = ApplicationContext::getContainer();
- $generator = $container->get(IdGeneratorInterface::class);
- return $generator->generate();
- }
- public static function http_get($url)
- {
- $oCurl = curl_init();
- if (stripos($url, "https://") !== false) {
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
- }
- curl_setopt($oCurl, CURLOPT_URL, $url);
- curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
- $sContent = curl_exec($oCurl);
- $aStatus = curl_getinfo($oCurl);
- curl_close($oCurl);
- if (intval($aStatus["http_code"]) == 200) {
- return $sContent;
- } else {
- return false;
- }
- }
-
- public static function http_post($url, $param, $post_file = false)
- {
- $oCurl = curl_init();
- if (stripos($url, "https://") !== false) {
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
- }
- if (is_string($param) || $post_file) {
- $strPOST = $param;
- } else {
- $aPOST = [];
- foreach ($param as $key => $val) {
- $aPOST[] = $key . "=" . urlencode($val);
- }
- $strPOST = join("&", $aPOST);
- }
- curl_setopt($oCurl, CURLOPT_URL, $url);
- curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($oCurl, CURLOPT_POST, true);
- curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
- $sContent = curl_exec($oCurl);
- $aStatus = curl_getinfo($oCurl);
- curl_close($oCurl);
- if (intval($aStatus["http_code"]) == 200) {
- return $sContent;
- } else {
- return false;
- }
- }
-
- public static function http_post_zp($url, $data, $options = [])
- {
-
- $ch = curl_init($url);
- var_dump("参数:",$data);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- $headers = [
- 'Authorization: Bearer be1856920c54ac537b530d69bc2eda73.gOO2BMq9NXavzEMq',
- 'Content-Type: application/json',
- ];
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-
- if (!empty($options)) {
- curl_setopt_array($ch, $options);
- }
-
- $response = curl_exec($ch);
-
- if (curl_errno($ch)) {
- $error_msg = curl_error($ch);
- curl_close($ch);
- throw new Exception("CURL Error: $error_msg");
- }
-
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
- curl_close($ch);
-
- $responseBody = $response;
- $headerEnd = strpos($response, "\r\n\r\n");
- if ($headerEnd !== false) {
-
- $responseBody = substr($response, $headerEnd + 4);
- } else {
-
- }
- return [
- 'response' => $responseBody,
- 'http_code' => $http_code
- ];
- }
-
- public static function getHistoryToday()
- {
- $api_url = "https://api.52vmy.cn/api/wl/today?type=json";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $api_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- curl_close($ch);
- $data = json_decode($response, true);
- return $data;
- }
-
- public static function addRiddle()
- {
- $api_url = "https://v.api.aa1.cn/api/api-miyu/index.php";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $api_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- curl_close($ch);
- var_dump("返回数据:",$response);
- $cleanStr = preg_replace('/<script[^>]*?>.*?<\/script>/is', '', $response);
- $jsonStr = trim(preg_replace('/\s+/', ' ', $cleanStr));
- $jsonStr = preg_replace('/^.*?({.*})$/', '$1', $jsonStr);
- $arr = json_decode($jsonStr, true);
- if (json_last_error() !== JSON_ERROR_NONE) {
- return json_last_error_msg();
- }
- var_dump("返回数据111:",$response);
- return $arr;
- }
-
- public static function getCouplet($text="") {
- if($text){
- $api_url = "https://apis.tianapi.com/msdl/index?key=44c4f9b3d66ce06900a098c78b90478f&num=10&fenlei=".urlencode($text);
- }else{
- $api_url = "https://apis.tianapi.com/msdl/index?key=44c4f9b3d66ce06900a098c78b90478f&num=10";
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $api_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- curl_close($ch);
- $data = json_decode($response, true);
- return $data;
- }
- }
|