Weatherwarning.php 1.7 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Weatherwarning extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $url = "https://api.caiyunapp.com/v2.6/HQi1gyemirzw5Nfd/";
    /**
     * 天气预警计划任务
     * @return void
     */
    public function weatherwarning(){
        //查询需要检测点
        $detectionaddress=Db::name("detectionaddress")->select();
        foreach ($detectionaddress as $k=>$v){
            $url=$v['lng'].','.$v['lat']."/realtime?alert=true";
            $res=send_post($this->url.$url,[],'GET');
            $res=json_decode($res,true);
            if($res['result']['alert']['content']){
                foreach ($res['result']['alert']['content'] as $ks=>$vs){
                    $warning_grade=substr($res['result']['alert']['content'][$ks]['code'],'2',2);//截取预警等级
                    $res['result']['alert']['content'][$ks]['code'];
                    $data=[
                        'detectionaddress_id'=>$detectionaddress[$k]['id'],
                        'warning_grade'=>$warning_grade,
                        'describe'=>$res['result']['alert']['content'][$ks]['description']
                    ];
                    Db::name('weatherwarning')->insert($data);
                }
            }else{
                $data=[
                    'detectionaddress_id'=>$detectionaddress[$k]['id'],
                    'warning_grade'=>5,
                    'describe'=>"暂无天气预警信息"
                ];
                Db::name('weatherwarning')->insert($data);
            }
        }
        $this->success("天气预警成功");

    }

}