xieruidong 2 years ago
parent
commit
9515e77794
2 changed files with 74 additions and 3 deletions
  1. 22 1
      app/data/controller/api/Weather.php
  2. 52 2
      app/data/service/WeatherSvc.php

+ 22 - 1
app/data/controller/api/Weather.php

@@ -17,7 +17,28 @@ class Weather extends Controller
     /**
      * @Title("天气预报")
      * @Param("name",desc="地区名称")
-     * @Returned("index.life",desc="指数对象")
+     * @Returned("index.date",desc="预报日期")
+     * @Returned("index.name",desc="生活指数类型的名称")
+     * @Returned("index.category",desc="生活指数预报级别名称")
+     * @Returned("daily.fxDate",desc="预报日期")
+     * @Returned("daily.textDay",desc="天气状况")
+     * @Returned("daily.tempMax",desc="最高温度")
+     * @Returned("daily.tempMin",desc="最低温度")
+     * @Returned("daily.windDirDay",desc="预报白天风向")
+     * @Returned("daily.windDirDay",desc="预报白天风向")
+     * @Returned("daily.windScaleNight",desc="预报夜间风力等级")
+     * @Returned("hourly.fxTime",desc="预报时间")
+     * @Returned("hourly.temp",desc="温度,默认单位:摄氏度")
+     * @Returned("hourly.windDir",desc="风向")
+     * @Returned("hourly.windScale",desc="风力等级")
+     * @Returned("hourly.category",desc="空气质量等级")
+     * @Returned("now.humidity",desc="相对湿度,百分比数值")
+     * @Returned("now.text",desc="天气状况的文字描述")
+     * @Returned("now.temp",desc="温度")
+     * @Returned("now.windDir",desc="风向")
+     * @Returned("now.category",desc="空气质量等级")
+     * @Returned("now.aqi",desc="空气质量指数")
+     * @Returned("now.obsTime",desc="更新时间")
      */
     public function area(WeatherSvc $svc){
         $data=$this->_vali([

+ 52 - 2
app/data/service/WeatherSvc.php

@@ -3,6 +3,7 @@
 namespace app\data\service;
 
 use app\data\model\BasePostageRegion;
+use Carbon\Carbon;
 use think\admin\Service;
 use think\helper\Arr;
 use think\helper\Str;
@@ -48,6 +49,9 @@ class WeatherSvc extends Service
             if(empty($result)){
                 return $result;
             }
+            foreach ($result as &$item){
+                $item['fxTime']=Carbon::parse($item['fxTime'])->format('H');
+            }
             return $result;
         },30*60);
         $daily=$this->app->cache->remember("weather_7d_{$this->getCity()}",function (){
@@ -79,7 +83,45 @@ class WeatherSvc extends Service
             }
             return $result;
         },30*60);
-        return compact('hourly','daily','index');
+        $airQuality=$this->app->cache->remember("weather_index_{$this->getCity()}",function (){
+            $data=client()->get($this->heApi('airQuality'),[
+                'query'=>[
+                    'location'=>$this->location(),
+                    'key'=>sysconf('app_juhe_config.key'),
+                ]
+            ]);
+            $json=json_decode($data->getBody()->getContents(),true);
+            $result=$json['now']??null;
+            if(empty($result)){
+                return $result;
+            }
+            return $result;
+        },30*60);
+        $now=$this->app->cache->remember("weather_now_{$this->getCity()}",function (){
+            $data=client()->get($this->heApi('now'),[
+                'query'=>[
+                    'location'=>$this->location(),
+                    'key'=>sysconf('app_juhe_config.key'),
+                ]
+            ]);
+            $json=json_decode($data->getBody()->getContents(),true);
+            $result=$json['now']??null;
+            if(empty($result)){
+                return $result;
+            }
+            return $result;
+        },1*60);
+        if($hourly){
+            foreach ($hourly as &$item){
+                $item['category']=$airQuality['category']??'优';
+            }
+        }
+        if($now){
+            $now['category']=$airQuality['category']??'优';
+            $now['aqi']=$airQuality['aqi']??'22';
+            $now['obsTime']=Carbon::parse($now['obsTime'])->format('m-d H:i');
+        }
+        return compact('hourly','daily','index','now');
     }
 
     protected function heApi($api){
@@ -96,7 +138,15 @@ class WeatherSvc extends Service
             'index'=>[
                 'fee'=>'https://api.qweather.com/v7/indices/1d',
                 'free'=>'https://devapi.qweather.com/v7/indices/1d',
-            ]
+            ],
+            'airQuality'=>[
+                'fee'=>'https://api.qweather.com/v7/air/now',
+                'free'=>'https://devapi.qweather.com/v7/air/now',
+            ],
+            'now'=>[
+                'fee'=>'https://api.qweather.com/v7/weather/now',
+                'free'=>'https://devapi.qweather.com/v7/weather/now',
+            ],
         ];
         return Arr::get($apiList[$api],$mode);
     }