diff --git a/src/main/java/com/chint/interfaces/rest/amap/AmapLocationRequest.java b/src/main/java/com/chint/interfaces/rest/amap/AmapLocationRequest.java index 7944b6ed..8a56787f 100644 --- a/src/main/java/com/chint/interfaces/rest/amap/AmapLocationRequest.java +++ b/src/main/java/com/chint/interfaces/rest/amap/AmapLocationRequest.java @@ -1,10 +1,16 @@ package com.chint.interfaces.rest.amap; +import com.chint.interfaces.rest.amap.dto.location.LocationRequestParam; +import com.chint.interfaces.rest.amap.dto.location.LocationResponse; import com.chint.interfaces.rest.amap.request.AmapRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.util.Optional; + +import static com.chint.infrastructure.constant.AmapConstant.AMAP_LOCATION_PATH; + @Service public class AmapLocationRequest { @@ -15,5 +21,15 @@ public class AmapLocationRequest { private String BaseUrl; -// public + public LocationResponse locationQuery(String keyWords) { + LocationRequestParam param = LocationRequestParam.of(keyWords); + return amapRequest.post(BaseUrl + AMAP_LOCATION_PATH, param, LocationResponse.class); + } + + public String getACodeByLocationName(String locationName) { + LocationResponse locationQuery = locationQuery(locationName); + Optional amapCityInfo = Optional.ofNullable(locationQuery.getData()) + .flatMap(it -> Optional.ofNullable(it.get(0))); + return amapCityInfo.map(LocationResponse.AmapCityInfo::getName).orElse(null); + } } diff --git a/src/main/java/com/chint/interfaces/rest/amap/dto/location/LocationRequestParam.java b/src/main/java/com/chint/interfaces/rest/amap/dto/location/LocationRequestParam.java index 5d94b914..13bb03a1 100644 --- a/src/main/java/com/chint/interfaces/rest/amap/dto/location/LocationRequestParam.java +++ b/src/main/java/com/chint/interfaces/rest/amap/dto/location/LocationRequestParam.java @@ -1,8 +1,16 @@ package com.chint.interfaces.rest.amap.dto.location; +import com.chint.interfaces.rest.amap.BaseRequestParam; import lombok.Data; +import org.springframework.web.bind.annotation.RequestParam; @Data -public class LocationRequestParam { +public class LocationRequestParam extends BaseRequestParam { private String keywords; + + public static LocationRequestParam of(String keywords) { + LocationRequestParam param = new LocationRequestParam(); + param.keywords = keywords; + return param; + } } diff --git a/src/main/java/com/chint/interfaces/rest/amap/dto/location/LocationResponse.java b/src/main/java/com/chint/interfaces/rest/amap/dto/location/LocationResponse.java index a5761fa0..2d3014b8 100644 --- a/src/main/java/com/chint/interfaces/rest/amap/dto/location/LocationResponse.java +++ b/src/main/java/com/chint/interfaces/rest/amap/dto/location/LocationResponse.java @@ -1,4 +1,18 @@ package com.chint.interfaces.rest.amap.dto.location; -public class LocationResponse { +import com.chint.interfaces.rest.amap.BaseResponse; +import lombok.Data; + +import java.util.List; + +@Data +public class LocationResponse extends BaseResponse { + private List data; + @Data + public static class AmapCityInfo { + private String adcode; //城市adcode + private String name; //城市名称 + private String namePinyin; //城市名称拼音 + private String initialLetter; //城市名称拼音首字母大写 + } } diff --git a/src/test/java/com/chint/AmapTest.java b/src/test/java/com/chint/AmapTest.java index a3d811c7..2ecf79be 100644 --- a/src/test/java/com/chint/AmapTest.java +++ b/src/test/java/com/chint/AmapTest.java @@ -1,11 +1,13 @@ package com.chint; import com.chint.domain.aggregates.user.User; +import com.chint.interfaces.rest.amap.AmapLocationRequest; import com.chint.interfaces.rest.amap.AmapLoginRequest; import com.chint.interfaces.rest.amap.AmapUserRequest; import com.chint.interfaces.rest.amap.BaseResponse; import com.chint.interfaces.rest.amap.dto.UserQueryResponse; import com.chint.interfaces.rest.amap.dto.detail.OrderDetailResponse; +import com.chint.interfaces.rest.amap.dto.location.LocationResponse; import com.chint.interfaces.rest.amap.dto.token.TokenDto; import com.chint.interfaces.rest.amap.dto.token.TokenResponse; import com.chint.interfaces.rest.amap.request.OrderDetailRequest; @@ -27,6 +29,9 @@ public class AmapTest { @Autowired private AmapLoginRequest amapLoginRequest; + @Autowired + private AmapLocationRequest amapLocationRequest; + private Gson gson = new Gson(); private User user = new User(1L, "230615020", 1, "卢麟哲", "lulz1@chint.com", "15857193365", "A30000001"); @@ -56,6 +61,11 @@ public class AmapTest { System.out.println(gson.toJson(h5LoginResponse.getRedirectUrl())); } + @Test + public void queryLocation(){ + LocationResponse locationQuery = amapLocationRequest.locationQuery("北京"); + System.out.println(gson.toJson(locationQuery.getData())); + } @Autowired private TokenRequest tokenRequest; @@ -104,4 +114,6 @@ public class AmapTest { public void cancelOrder() { takeCarRequest.cancelOrder("20240301", "行程变更"); } + + }