提交 bd3ae3a9 authored 作者: 吕本才's avatar 吕本才

feat(tencent-map): 集成腾讯地图逆地理编码功能

上级 23734aca
package com.link.hub.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* 腾讯地图配置类
*/
@Configuration
public class TencentMapConfig {
@Value("${tencent.map.key:UTEBZ-UJ3KG-OORQO-QT3PT-JDWU7-YRBZA}")
private String tencentMapKey;
// 腾讯地图逆地理编码API地址
public static final String TENCENT_MAP_GEOCODER_URL = "https://apis.map.qq.com/ws/geocoder/v1/";
// 替换为你申请的腾讯地图Key
public static final String TENCENT_MAP_KEY = "UTEBZ-UJ3KG-OORQO-QT3PT-JDWU7-YRBZA";
// 注入RestTemplate(用于发送HTTP请求)
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.link.hub.controller.mp.query;
import com.alibaba.fastjson.JSONObject;
import com.link.hub.service.weChatMiniProgram.TencentMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 腾讯地图接口控制器
*/
@RestController
@RequestMapping("/miniProgram/query/tencent/map")
public class TencentMapController {
@Autowired
private TencentMapService tencentMapService;
/**
* 经纬度→地理位置查询接口
* @param lat 纬度(必填)
* @param lng 经度(必填)
* @param getPoi 是否返回POI(可选,默认0)
* @return 统一响应结果
*/
@GetMapping("/geo/reverse")
public ResponseEntity<?> getLocationByLngLat(
@RequestParam("lat") Double lat,
@RequestParam("lng") Double lng,
@RequestParam(value = "getPoi", defaultValue = "0") Integer getPoi) {
try {
JSONObject locationByLngLat = tencentMapService.getLocationByLngLat(lat, lng, getPoi);
// 返回成功结果(可封装统一响应体,这里简化)
return ResponseEntity.ok(locationByLngLat);
} catch (IllegalArgumentException e) {
// 参数错误
return ResponseEntity.badRequest().body("参数错误:" + e.getMessage());
} catch (Exception e) {
// 其他异常
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("查询失败:" + e.getMessage());
}
}
}
package com.link.hub.service.weChatMiniProgram;
import com.alibaba.fastjson.JSONObject;
public interface TencentMapService {
JSONObject getLocationByLngLat(Double lat, Double lng, Integer getPoi);
}
package com.link.hub.service.weChatMiniProgram.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.link.hub.config.TencentMapConfig;
import com.link.hub.service.weChatMiniProgram.TencentMapService;
import com.sfa.common.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
/**
* 腾讯地图业务层
*/
@Slf4j
@Service
public class TencentMapServiceImpl implements TencentMapService {
@Autowired
private RestTemplate restTemplate;
/**
* 根据经纬度查询地理位置
* @param latitude 纬度(如:39.984154)
* @param longitude 经度(如:116.307490)
* @param getPoi 是否返回周边POI(1=返回,0=不返回)
* @return 腾讯地图地理信息响应
* @throws Exception 调用异常/解析异常
*/
public JSONObject getLocationByLngLat(Double latitude, Double longitude, Integer getPoi) {
// 1. 参数校验
if (latitude == null || longitude == null) {
throw new IllegalArgumentException("纬度/经度不能为空");
}
if (getPoi == null) {
getPoi = 0; // 默认不返回POI
}
// 2. 拼接请求URL(带参数)
String location = latitude + "," + longitude;
String requestUrl = UriComponentsBuilder.fromHttpUrl(TencentMapConfig.TENCENT_MAP_GEOCODER_URL)
.queryParam("location", location)
.queryParam("key", TencentMapConfig.TENCENT_MAP_KEY)
.queryParam("get_poi", getPoi)
.toUriString();
// 3. 发送GET请求调用腾讯地图API
ResponseEntity<String> responseEntity;
try {
responseEntity = restTemplate.getForEntity(requestUrl, String.class);
} catch (RestClientException e) {
log.error(String.format("调用腾讯地图API失败:%s", e.getMessage()), e);
throw new ServiceException("调用腾讯地图API失败:" + e.getMessage());
}
// 4. 解析JSON响应
String responseBody = responseEntity.getBody();
if (responseBody == null) {
log.error("腾讯地图API返回空结果");
throw new ServiceException("腾讯地图API返回空结果");
}
JSONObject geoResponse = JSON.parseObject(responseBody);
// 5. 校验API返回状态
if (geoResponse.getInteger("status") != 0) {
log.error("腾讯地图API调用失败:{}(状态码:{})", geoResponse.getString("message"), geoResponse.getInteger("status"));
throw new ServiceException("腾讯地图API调用失败:" + geoResponse.getString("message") + "(状态码:" + geoResponse.getInteger("status") + ")");
}
return geoResponse;
}
}
......@@ -18,6 +18,7 @@ import com.sfa.common.core.utils.uuid.IdUtils;
import com.sfa.common.core.utils.wechat.WeChatPlatFormUtils;
import com.sfa.common.security.service.TokenService;
import com.sfa.system.api.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
......@@ -29,6 +30,7 @@ import java.util.Map;
/**
* 小程序服务实现
*/
@Slf4j
@Service
public class WechatMiniProgramServiceServiceImpl implements WechatMiniProgramService {
@Resource
......@@ -52,6 +54,7 @@ public class WechatMiniProgramServiceServiceImpl implements WechatMiniProgramSer
openid = "test1"+code;
}
if (ObjectUtil.isEmpty(openid)) {
log.error("获取openid失败,code:{},返回信息:{}", code,jsonObject.toString());
throw new ServiceException("获取openid失败");
}
MpUserDto mpUserDto = new MpUserDto();
......
......@@ -34,10 +34,7 @@ public class WechatMiniProgramUserServiceServiceImpl implements WechatMiniProgra
wechatMiniProgramUserWq.setOpenid(mpUserDto.getOpenid());
wechatMiniProgramUserWq.setUserId(mpUserDto.getId());
WechatMiniProgramUser wechatMiniProgramUser = wechatMiniProgramUserDao.queryMiniProgramUserOne(wechatMiniProgramUserWq);
// if (wechatMiniProgramUser == null) {
// log.error("用户不存在,openid:{},userId:{}", mpUserDto.getOpenid(), mpUserDto.getId());
// throw new ServiceException("用户不存在");
// }
return wechatMiniProgramUser;
}
......@@ -45,10 +42,10 @@ public class WechatMiniProgramUserServiceServiceImpl implements WechatMiniProgra
public Long saveMiniProgramUser(MpOpenIdDTO mpOpenIdDTO) {
WechatMiniProgramUser wechatMiniProgramUser = new WechatMiniProgramUser();
wechatMiniProgramUser.setOpenid(mpOpenIdDTO.getOpenid());
wechatMiniProgramUser.setUnionid(mpOpenIdDTO.getUnionid());
wechatMiniProgramUser.setCreateTime(DateUtil.date());
wechatMiniProgramUser.setUpdateTime(DateUtil.date());
wechatMiniProgramUserDao.insert(wechatMiniProgramUser);
return wechatMiniProgramUser.getId();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论