123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace app\data\service;
- use app\data\model\BasePostageRegion;
- use Carbon\Carbon;
- use think\admin\Service;
- use think\helper\Arr;
- use think\helper\Str;
- class WeatherSvc extends Service
- {
- /** @var BasePostageRegion */
- protected $area;
- /**
- * @return BasePostageRegion
- */
- public function getArea(): BasePostageRegion
- {
- return $this->area;
- }
- /**
- * @param BasePostageRegion $area
- */
- public function setArea(BasePostageRegion $area): void
- {
- $this->area = $area;
- }
- public function getCity(){
- return $this->area->short;
- }
- public function location(){
- return sprintf('%s,%s',$this->area->lng,$this->area->lat);
- }
- public function getWeather(){
- $hourly=$this->app->cache->remember("weather_hour_{$this->getCity()}",function (){
- $data=client()->get($this->heApi('24h'),[
- 'query'=>[
- 'location'=>$this->location(),
- 'key'=>sysconf('app_juhe_config.key'),
- ]
- ]);
- $json=json_decode($data->getBody()->getContents(),true);
- $result=$json['hourly']??null;
- if(empty($result)){
- return $result;
- }
- foreach ($result as &$item){
- $item['fxTime']=Carbon::parse($item['fxTime'])->format('H:00');
- }
- return $result;
- },1*60);
- $daily=$this->app->cache->remember("weather_7d_{$this->getCity()}",function (){
- $data=client()->get($this->heApi('7d'),[
- 'query'=>[
- 'location'=>$this->location(),
- 'key'=>sysconf('app_juhe_config.key'),
- ]
- ]);
- $json=json_decode($data->getBody()->getContents(),true);
- $result=$json['daily']??null;
- if(empty($result)){
- return $result;
- }
- foreach ($result as $idx=>&$item){
- $item['fxDate']=Carbon::parse($item['fxDate'])->format('m/d');
- if($idx==0){
- $item['fxWeek']='今天';
- }else {
- $item['fxWeek'] = sprintf('周%s', ['日', '一', '二', '三', '四', '五', '六'][Carbon::parse($item['fxDate'])->dayOfWeek]);
- }
- }
- return $result;
- },1*60);
- $index=$this->app->cache->remember("weather_index_{$this->getCity()}",function (){
- $data=client()->get($this->heApi('index'),[
- 'query'=>[
- 'location'=>$this->location(),
- 'key'=>sysconf('app_juhe_config.key'),
- 'type'=>'2,3,5,9'
- ]
- ]);
- $json=json_decode($data->getBody()->getContents(),true);
- $result=$json['daily']??null;
- if(empty($result)){
- return $result;
- }
- return $result;
- },1*60);
- $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;
- },1*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');
- if(isset($daily[0])){
- $now['tempMin'] = $daily[0]['tempMin'];
- $now['tempMax'] = $daily[0]['tempMax'];
- }else {
- $now['tempMin'] = $now['temp'];
- $now['tempMax'] = $now['temp'] + mt_rand(1, 5);
- }
- }
- $tomorrow=null;
- if(isset($daily[1])){
- $tomorrow=$daily[1];
- }
- return compact('hourly','daily','index','now','tomorrow');
- }
- protected function heApi($api){
- $mode=sysconf('app_juhe_config.mode')?:'free';
- $apiList=[
- '24h'=>[
- 'fee'=>'https://api.qweather.com/v7/weather/24h',
- 'free'=>'https://devapi.qweather.com/v7/weather/24h',
- ],
- '7d'=>[
- 'fee'=>'https://api.qweather.com/v7/weather/7d',
- 'free'=>'https://devapi.qweather.com/v7/weather/7d',
- ],
- '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);
- }
- }
|