where('name', 'amap')->value('value'); if ($raw) { $cfg = json_decode($raw, true); if (!empty($cfg['appkey'])) { return $cfg['appkey']; } } $platform = Db::name('shopro_config')->where('name', 'platform')->value('value'); if ($platform) { $cfg = json_decode($platform, true); if (!empty($cfg['amap']['appkey'])) { return $cfg['amap']['appkey']; } } return '24eb092b2163da8fa5087bf92844a5bd'; } protected function amapGet($path, $query) { $query['key'] = $this->amapKey(); $url = 'https://restapi.amap.com/v3/' . $path . '?' . http_build_query($query); $ctx = stream_context_create(['http' => ['timeout' => 8]]); $body = @file_get_contents($url, false, $ctx); if ($body === false) { $this->error('地图服务请求失败'); } $data = json_decode($body, true); if (!$data || (isset($data['status']) && $data['status'] !== '1')) { $info = isset($data['info']) ? $data['info'] : '地理编码失败'; $this->error($info); } return $data; } /** * 地址 → 坐标 * @ApiParams (name="address", type="string", required=true) */ public function geocode() { $address = trim($this->request->get('address', '')); if ($address === '') { $this->error('地址不能为空'); } $data = $this->amapGet('geocode/geo', ['address' => $address]); if (empty($data['geocodes'][0]['location'])) { $this->error('无法解析该地址,请尽量填写门牌号'); } list($lng, $lat) = explode(',', $data['geocodes'][0]['location']); $this->success('ok', [ 'latitude' => $lat, 'longitude' => $lng, 'formatted' => $data['geocodes'][0]['formatted_address'] ?? '', ]); } /** * 坐标 → 地址 * @ApiParams (name="location", type="string", required=true, description="lng,lat") */ public function regeo() { $location = trim($this->request->get('location', '')); if ($location === '') { $this->error('坐标不能为空'); } $data = $this->amapGet('geocode/regeo', ['location' => $location]); $this->success('ok', $data['regeocode'] ?? []); } }