WeatherSvc.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\data\service;
  3. use app\data\model\BasePostageRegion;
  4. use Carbon\Carbon;
  5. use think\admin\Service;
  6. use think\helper\Arr;
  7. use think\helper\Str;
  8. class WeatherSvc extends Service
  9. {
  10. /** @var BasePostageRegion */
  11. protected $area;
  12. /**
  13. * @return BasePostageRegion
  14. */
  15. public function getArea(): BasePostageRegion
  16. {
  17. return $this->area;
  18. }
  19. /**
  20. * @param BasePostageRegion $area
  21. */
  22. public function setArea(BasePostageRegion $area): void
  23. {
  24. $this->area = $area;
  25. }
  26. public function getCity(){
  27. return $this->area->short;
  28. }
  29. public function location(){
  30. return sprintf('%s,%s',$this->area->lng,$this->area->lat);
  31. }
  32. public function getWeather(){
  33. $hourly=$this->app->cache->remember("weather_hour_{$this->getCity()}",function (){
  34. $data=client()->get($this->heApi('24h'),[
  35. 'query'=>[
  36. 'location'=>$this->location(),
  37. 'key'=>sysconf('app_juhe_config.key'),
  38. ]
  39. ]);
  40. $json=json_decode($data->getBody()->getContents(),true);
  41. $result=$json['hourly']??null;
  42. if(empty($result)){
  43. return $result;
  44. }
  45. foreach ($result as &$item){
  46. $item['fxTime']=Carbon::parse($item['fxTime'])->format('H:00');
  47. }
  48. return $result;
  49. },1*60);
  50. $daily=$this->app->cache->remember("weather_7d_{$this->getCity()}",function (){
  51. $data=client()->get($this->heApi('7d'),[
  52. 'query'=>[
  53. 'location'=>$this->location(),
  54. 'key'=>sysconf('app_juhe_config.key'),
  55. ]
  56. ]);
  57. $json=json_decode($data->getBody()->getContents(),true);
  58. $result=$json['daily']??null;
  59. if(empty($result)){
  60. return $result;
  61. }
  62. foreach ($result as $idx=>&$item){
  63. $item['fxDate']=Carbon::parse($item['fxDate'])->format('m/d');
  64. if($idx==0){
  65. $item['fxWeek']='今天';
  66. }else {
  67. $item['fxWeek'] = sprintf('周%s', ['日', '一', '二', '三', '四', '五', '六'][Carbon::parse($item['fxDate'])->dayOfWeek]);
  68. }
  69. }
  70. return $result;
  71. },1*60);
  72. $index=$this->app->cache->remember("weather_index_{$this->getCity()}",function (){
  73. $data=client()->get($this->heApi('index'),[
  74. 'query'=>[
  75. 'location'=>$this->location(),
  76. 'key'=>sysconf('app_juhe_config.key'),
  77. 'type'=>'2,3,5,9'
  78. ]
  79. ]);
  80. $json=json_decode($data->getBody()->getContents(),true);
  81. $result=$json['daily']??null;
  82. if(empty($result)){
  83. return $result;
  84. }
  85. return $result;
  86. },1*60);
  87. $airQuality=$this->app->cache->remember("weather_index_{$this->getCity()}",function (){
  88. $data=client()->get($this->heApi('airQuality'),[
  89. 'query'=>[
  90. 'location'=>$this->location(),
  91. 'key'=>sysconf('app_juhe_config.key'),
  92. ]
  93. ]);
  94. $json=json_decode($data->getBody()->getContents(),true);
  95. $result=$json['now']??null;
  96. if(empty($result)){
  97. return $result;
  98. }
  99. return $result;
  100. },1*60);
  101. $now=$this->app->cache->remember("weather_now_{$this->getCity()}",function (){
  102. $data=client()->get($this->heApi('now'),[
  103. 'query'=>[
  104. 'location'=>$this->location(),
  105. 'key'=>sysconf('app_juhe_config.key'),
  106. ]
  107. ]);
  108. $json=json_decode($data->getBody()->getContents(),true);
  109. $result=$json['now']??null;
  110. if(empty($result)){
  111. return $result;
  112. }
  113. return $result;
  114. },1*60);
  115. if($hourly){
  116. foreach ($hourly as &$item){
  117. $item['category']=$airQuality['category']??'优';
  118. }
  119. }
  120. if($now){
  121. $now['category']=$airQuality['category']??'优';
  122. $now['aqi']=$airQuality['aqi']??'22';
  123. $now['obsTime']=Carbon::parse($now['obsTime'])->format('m-d H:i');
  124. if(isset($daily[0])){
  125. $now['tempMin'] = $daily[0]['tempMin'];
  126. $now['tempMax'] = $daily[0]['tempMax'];
  127. }else {
  128. $now['tempMin'] = $now['temp'];
  129. $now['tempMax'] = $now['temp'] + mt_rand(1, 5);
  130. }
  131. }
  132. $tomorrow=null;
  133. if(isset($daily[1])){
  134. $tomorrow=$daily[1];
  135. }
  136. return compact('hourly','daily','index','now','tomorrow');
  137. }
  138. protected function heApi($api){
  139. $mode=sysconf('app_juhe_config.mode')?:'free';
  140. $apiList=[
  141. '24h'=>[
  142. 'fee'=>'https://api.qweather.com/v7/weather/24h',
  143. 'free'=>'https://devapi.qweather.com/v7/weather/24h',
  144. ],
  145. '7d'=>[
  146. 'fee'=>'https://api.qweather.com/v7/weather/7d',
  147. 'free'=>'https://devapi.qweather.com/v7/weather/7d',
  148. ],
  149. 'index'=>[
  150. 'fee'=>'https://api.qweather.com/v7/indices/1d',
  151. 'free'=>'https://devapi.qweather.com/v7/indices/1d',
  152. ],
  153. 'airQuality'=>[
  154. 'fee'=>'https://api.qweather.com/v7/air/now',
  155. 'free'=>'https://devapi.qweather.com/v7/air/now',
  156. ],
  157. 'now'=>[
  158. 'fee'=>'https://api.qweather.com/v7/weather/now',
  159. 'free'=>'https://devapi.qweather.com/v7/weather/now',
  160. ],
  161. ];
  162. return Arr::get($apiList[$api],$mode);
  163. }
  164. }