<?php
namespace app\api\controller\screen;

use app\hander\HelpHander;
use think\Controller;
use think\Db;

/**
 * 天气接口
 */
class Weather extends Controller
{
//根据ip 获取城市
    public function address() {
        $getIp = request()->ip();//获取真实IP
        // $getIp = '120.46.129.222';//获取真实IP
        $content = file_get_contents("http://apis.juhe.cn/ip/ipNewV3?key=dd24ed2a3ba02ca20d6eec1d6175cb7c&ip={$getIp}");
        $address = json_decode($content);
        $address = $address->result;

        $return['province'] = $address->Province;//省份
        $return['city'] = $address->City;//城市
        $return['district'] = $address->District;//城市

        return $return;
    }
    //根据城市获取当天天气
    public function getTodayWeather(){

        // 请求的接口URL
        $apiUrl = 'http://apis.juhe.cn/simpleWeather/query';
        // 请求参数
        $addr = $this->address();

        $params = [
            'city' => $addr['city'], // 要查询的城市
            'key' => config('app.weather_key')
        ];

        $paramsString = http_build_query($params);

        // 发起接口网络请求
        $response = $this->juheHttpRequest($apiUrl, $paramsString , 1);
        $result = json_decode($response, true);
        $data = [];
        if($result){
            $temperature = '';
            if($result['result']['realtime']['temperature']){
                $temperature =$result['result']['realtime']['temperature'].'℃';
            }

            $wid =  $result['result']['realtime']['wid'];
            $data = [
                'city'=>$addr['city'],
                'direct'=>$result['result']['realtime']['info'],
                'temperature'=>$temperature,
                'img'=>getSite().'/admin/weather/'.$wid.'.png',
            ];

        }
        HelpHander::success($data);

    }

    /**
     * 发起网络请求函数
     * @param $url 请求的URL
     * @param bool $params 请求的参数内容
     * @param int $ispost 是否POST请求
     * @return bool|string 返回内容
     */
    function juheHttpRequest($url, $params = false, $ispost = 0)
    {
        $httpInfo = array();
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 12);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($ch, CURLOPT_PROXY, 'http://10.10.134.94:3128');
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            if($params){
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            }
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($params));
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $response = curl_exec($ch);
        if ($response === FALSE) {
            // echo "cURL Error: ".curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }


}