|
|
@@ -20,7 +20,7 @@ import java.math.BigDecimal;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.List;
|
|
|
|
|
|
-/** Google Routes API 的服务端实现,密钥只从后端专用字典读取。 */
|
|
|
+/** Google Routes API 的服务端实现,复用既有地图字典中的 Key。 */
|
|
|
@Component
|
|
|
public class GoogleRoutesDistanceProvider implements RouteDistanceProvider {
|
|
|
private static final String ENDPOINT = "https://routes.googleapis.com/directions/v2:computeRoutes";
|
|
|
@@ -32,8 +32,8 @@ public class GoogleRoutesDistanceProvider implements RouteDistanceProvider {
|
|
|
public RouteDistance route(BigDecimal originLatitude, BigDecimal originLongitude,
|
|
|
BigDecimal destinationLatitude, BigDecimal destinationLongitude,
|
|
|
Integer vehicleType) {
|
|
|
- // 不复用可能被浏览器端读取的 sys_googlemap_key,防止服务端 API 权限泄露。
|
|
|
- List<SysDictData> keys = DictUtils.getDictCache("sys_google_routes_key");
|
|
|
+ // 闪送与外卖共用既有地图 Key,避免维护第二套字典配置。
|
|
|
+ List<SysDictData> keys = DictUtils.getDictCache(mapKeyDictType());
|
|
|
if (keys == null || keys.isEmpty() || keys.get(0).getDictValue() == null
|
|
|
|| keys.get(0).getDictValue().isBlank()) {
|
|
|
throw new IllegalStateException("google routes key unavailable");
|
|
|
@@ -53,8 +53,10 @@ public class GoogleRoutesDistanceProvider implements RouteDistanceProvider {
|
|
|
request.setEntity(new StringEntity(body.toJSONString(), ContentType.APPLICATION_JSON));
|
|
|
try (CloseableHttpClient client = HttpClients.custom().disableAutomaticRetries().build();
|
|
|
CloseableHttpResponse response = client.execute(request)) {
|
|
|
- if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 300) {
|
|
|
- throw new IllegalStateException("google routes non-success response");
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode < 200 || statusCode >= 300) {
|
|
|
+ // 仅保留 HTTP 状态,绝不记录 Key、地址或完整 Google 响应内容。
|
|
|
+ throw new IllegalStateException("google routes http status " + statusCode);
|
|
|
}
|
|
|
JSONObject json = JSON.parseObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8));
|
|
|
JSONArray routes = json.getJSONArray("routes");
|
|
|
@@ -67,6 +69,8 @@ public class GoogleRoutesDistanceProvider implements RouteDistanceProvider {
|
|
|
throw new IllegalStateException("google routes invalid distance");
|
|
|
}
|
|
|
return new RouteDistance(meters, parseDuration(first.getString("duration")), "ROUTE");
|
|
|
+ } catch (IllegalStateException exception) {
|
|
|
+ throw exception;
|
|
|
} catch (Exception exception) {
|
|
|
throw new IllegalStateException("google routes request failed", exception);
|
|
|
}
|
|
|
@@ -89,6 +93,11 @@ public class GoogleRoutesDistanceProvider implements RouteDistanceProvider {
|
|
|
? "DRIVE" : "TWO_WHEELER";
|
|
|
}
|
|
|
|
|
|
+ /** 闪送与外卖共用的地图 Key 字典类型。 */
|
|
|
+ static String mapKeyDictType() {
|
|
|
+ return "sys_googlemap_key";
|
|
|
+ }
|
|
|
+
|
|
|
private Integer parseDuration(String value) {
|
|
|
if (value == null || !value.endsWith("s")) return null;
|
|
|
try {
|