AmApService.php 2.4 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

namespace DtApp\ThinkLibrary\service\amap;

李光春's avatar
李光春 已提交
19
use DtApp\ThinkLibrary\exception\AliException;
李光春's avatar
李光春 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
use DtApp\ThinkLibrary\exception\CurlException;
use DtApp\ThinkLibrary\Service;
use DtApp\ThinkLibrary\service\Curl\HttpService;

/**
 * 高德地图
 * https://lbs.amap.com/api/webservice/summary
 * Class AmApService
 * @package DtApp\ThinkLibrary\service\amap
 */
class AmApService extends Service
{
    private $url = "https://restapi.amap.com/v3/";

    private $key = "";

    private $output = "JSON";

    public function key(string $key)
    {
        $this->key = $key;
        return $this;
    }

李光春's avatar
李光春 已提交
44 45 46 47 48 49 50 51 52 53
    /**
     * 获取配置信息
     * @return $this
     */
    private function getConfig()
    {
        $this->key = config('dtapp.amap.key');
        return $this;
    }

李光春's avatar
李光春 已提交
54 55 56 57 58 59 60
    /**
     * 天气查询
     * https://lbs.amap.com/api/webservice/guide/api/weatherinfo
     * @param string $city
     * @param string $extensions
     * @return array|bool|mixed|string
     * @throws CurlException
李光春's avatar
李光春 已提交
61
     * @throws AliException
李光春's avatar
李光春 已提交
62 63 64
     */
    public function weather($city = "110101", $extensions = "base")
    {
李光春's avatar
李光春 已提交
65 66
        if (empty($this->key)) $this->getConfig();
        if (empty($this->key)) throw new AliException('请检查key参数');
李光春's avatar
李光春 已提交
67 68 69 70 71 72 73 74 75 76 77
        $data = http_build_query([
            "city" => $city,
            "extensions" => $extensions,
            "key" => $this->key,
            "output" => $this->output,
        ]);
        return HttpService::instance()
            ->url("{$this->url}weather/weatherInfo?{$data}")
            ->toArray();
    }
}