Passed
Push — v6 ( 533a60...c76d97 )
by 光春
03:18
created

LbsService::route()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
namespace DtApp\ThinkLibrary\service\tencent;
21
22
use DtApp\ThinkLibrary\exception\DtaException;
23
use DtApp\ThinkLibrary\Service;
24
use think\exception\HttpException;
25
26
/**
27
 * 腾讯地图
28
 * https://lbs.qq.com/service/webService/webServiceGuide/webServiceOverview
29
 * Class Lbs
30
 * @package DtApp\ThinkLibrary\service\tencent
31
 */
32
class LbsService extends Service
33
{
34
    /**
35
     * 开发者密钥(Key)
36
     * @var string
37
     */
38
    private $key = "";
39
40
    /**
41
     * 需要发送的的参数
42
     * @var
43
     */
44
    private $param;
45
46
    /**
47
     * 响应内容
48
     * @var
49
     */
50
    private $output;
51
52
    /**
53
     * 待请求的链接
54
     * @var string
55
     */
56
    private $api_url = '';
57
58
    /**
59
     * @param string $key
60
     * @return $this
61
     */
62
    public function key(string $key): self
63
    {
64
        $this->key = $key;
65
        return $this;
66
    }
67
68
    /**
69
     * 请求参数
70
     * @param array $param
71
     * @return $this
72
     */
73
    public function param(array $param): self
74
    {
75
        $this->param = $param;
76
        return $this;
77
    }
78
79
    /**
80
     * IP定位
81
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceIp
82
     * @return $this
83
     */
84
    public function ip(): self
85
    {
86
        $this->api_url = 'https://apis.map.qq.com/ws/location/v1/ip';
87
        return $this;
88
    }
89
90
    /**
91
     * 行政区划 - 获取全部行政区划数据
92
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceDistrict
93
     * @return $this
94
     */
95
    public function districtList(): self
96
    {
97
        $this->api_url = "https://apis.map.qq.com/ws/district/v1/list";
98
        return $this;
99
    }
100
101
    /**
102
     * 行政区划 - 获取全部行政区划数据
103
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceDistrict
104
     * @return $this
105
     */
106
    public function districtGetChildren(): self
107
    {
108
        $this->api_url = "https://apis.map.qq.com/ws/district/v1/getchildren";
109
        return $this;
110
    }
111
112
    /**
113
     * 行政区划 - 获取全部行政区划数据
114
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceDistrict
115
     * @return $this
116
     */
117
    public function districtSearch(): self
118
    {
119
        $this->api_url = "https://apis.map.qq.com/ws/district/v1/search";
120
        return $this;
121
    }
122
123
    /**
124
     * 坐标转换 - 实现从其它地图供应商坐标系或标准GPS坐标系,批量转换到腾讯地图坐标系。
125
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceTranslate
126
     * @return $this
127
     */
128
    public function translate(): self
129
    {
130
        $this->api_url = "https://apis.map.qq.com/ws/coord/v1/translate";
131
        return $this;
132
    }
133
134
    /**
135
     * 批量距离计算(矩阵) - 距离矩阵(DistanceMatrix),用于批量计算一组起终点的路面距离(或称导航距离),可应用于网约车派单、多目的地最优路径智能计算等场景中,支持驾车、步行、骑行多种交通方式,满足不同应用需要。
136
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceMatrix
137
     * @return $this
138
     */
139
    public function matrix(): self
140
    {
141
        $this->api_url = "https://apis.map.qq.com/ws/distance/v1/matrix";
142
        return $this;
143
    }
144
145
    /**
146
     * 路线规划(驾车/公交/步骑) - 腾讯地图Direction API,提供多种交通方式的路线计算能力,包括:
147
     * 1. 驾车(driving):支持结合实时路况、少收费、不走高速等多种偏好,精准预估到达时间(ETA);
148
     * 2. 步行(walking):基于步行路线规划。
149
     * 3. 骑行(bicycling):基于自行车的骑行路线;
150
     * 4. 公交(transit):支持公共汽车、地铁等多种公共交通工具的换乘方案计算;
151
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceRoute
152
     * @return $this
153
     */
154
    public function route(): self
155
    {
156
        $this->api_url = "https://apis.map.qq.com/ws/direction/v1/driving/";
157
        return $this;
158
    }
159
160
    /**
161
     * 地址解析(地址转坐标) - 本接口提供由地址描述到所述位置坐标的转换,与逆地址解析的过程正好相反。
162
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceGeocoder
163
     * @return $this
164
     */
165
    public function geoCoder(): self
166
    {
167
        $this->api_url = "https://apis.map.qq.com/ws/geocoder/v1/";
168
        return $this;
169
    }
170
171
    /**
172
     * 逆地址解析(坐标位置描述) - 本接口提供由坐标到坐标所在位置的文字描述的转换。输入坐标返回地理位置信息和附近poi列表。目前应用于物流、出行、O2O、社交等场景。服务响应速度快、稳定,支撑亿级调用。
173
     * 1)满足传统对省市区、乡镇村、门牌号、道路及交叉口、河流、湖泊、桥、poi列表的需求。
174
     * 2)业界首创,提供易于人理解的地址描述:海淀区中钢国际广场(欧美汇购物中心北)。
175
     * 3)提供精准的商圈、知名的大型区域、附近知名的一级地标、代表当前位置的二级地标等。
176
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder
177
     * @return $this
178
     */
179
    public function gCoder(): self
180
    {
181
        $this->api_url = "https://apis.map.qq.com/ws/geocoder/v1/";
182
        return $this;
183
    }
184
185
    /**
186
     * 关键词输入提示 - 用于获取输入关键字的补完与提示,帮助用户快速输入。本接口为纯HTTP数据接口,需配合前端程序实现Autocomplete(自动完成)的效果。
187
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceSuggestion
188
     * @return $this
189
     */
190
    public function suggestion(): self
191
    {
192
        $this->api_url = "https://apis.map.qq.com/ws/place/v1/suggestion";
193
        return $this;
194
    }
195
196
    /**
197
     * 地点搜索 - 地点搜索(search接口),提供三类范围条件的搜索功能:
198
     * 指定城市的地点搜索:如在北京搜索餐馆;
199
     * 圆形区域的地点搜索:一般用于指定位置的周边(附近)地点搜索,如,搜索颐和园附近的酒店;
200
     * 矩形区域的地点搜索:在地图应用中,往往用于视野内搜索,因为显示地图的区域是个矩形。
201
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceSuggestion
202
     * @return $this
203
     */
204
    public function search(): self
205
    {
206
        $this->api_url = "https://apis.map.qq.com/ws/place/v1/search";
207
        return $this;
208
    }
209
210
    /**
211
     * @return array|mixed
212
     * @throws DtaException
213
     */
214
    public function toArray()
215
    {
216
        //首先检测是否支持curl
217
        if (!extension_loaded("curl")) {
218
            throw new HttpException(404, '请开启curl模块!');
219
        }
220
        if (empty($this->key)) {
221
            throw new DtaException('开发密钥不能为空');
222
        }
223
        if (empty($this->api_url)) {
224
            throw new DtaException('请检查需要调用的接口');
225
        }
226
        $this->param['key'] = $this->key;
227
        $this->http();
228
        // 正常
229
        if (is_array($this->output)) {
230
            return $this->output;
231
        }
232
        if (is_object($this->output)) {
233
            $this->output = json_encode($this->output, JSON_UNESCAPED_UNICODE);
234
        }
235
        $this->output = json_decode($this->output, true);
236
        return $this->output;
237
    }
238
239
    /**
240
     * 网络请求
241
     */
242
    private function http(): void
243
    {
244
        //组织参数
245
        $strParam = $this->createStrParam();
246
        $result = file_get_contents($this->api_url . "?{$strParam}");
247
        $result = json_decode($result, true);
248
        $this->output = $result;
249
    }
250
251
    /**
252
     * 组参
253
     * @return string
254
     */
255
    private function createStrParam(): string
256
    {
257
        $strParam = '';
258
        foreach ($this->param as $key => $val) {
259
            if ($key !== '' && $val !== '') {
260
                $strParam .= $key . '=' . urlencode($val) . '&';
261
            }
262
        }
263
        return $strParam;
264
    }
265
}