//调用第三方接口
@Slf4j
@Component
public class HaierStoreClient {
@Value("${haier.store.url}")
private String haierStoreUrl;
@Value("${haier.mstore.url}")
private String haierMStoreUrl;
private String GET_SALES_AREA_BY_CODE = "/api/thirdpay/store/getSalesAreaByCode";
private String GET_STORE_FIVE_ADDR= "/mstore/region/location.json";
@Resource
private RequestUtil requestUtil;
public List<SalesAreaDto> getSalesAreaByCode(String storeCode){
log.info("【根据店铺编码远程调用商城接口查询销售区域】参数:storeCode={}",storeCode);
String resultStr = requestUtil.sendGetRequest(haierStoreUrl, GET_SALES_AREA_BY_CODE+"?storeCode="+storeCode,null);
Map resultMap = JSONObject.parseObject(resultStr, Map.class);
List<SalesAreaDto> salesAreaDtoList = JSONObject.parseArray(String.valueOf(resultMap.get("data")), SalesAreaDto.class);
log.info("【根据店铺编码远程调用商城接口查询销售区域】返回结果:salesAreaDtoList={}",salesAreaDtoList);
if(CollectionUtils.isEmpty(salesAreaDtoList)){
return new ArrayList<>();
}
for (SalesAreaDto salesAreaDto : salesAreaDtoList) {
salesAreaDto.setStoreCode(storeCode);
}
return salesAreaDtoList;
}
public StoreFiveAddrDto getStoreFiveAddr(BigDecimal latitude, BigDecimal longitude){
log.info("【根据店铺经纬度远程调用商城接口查询店铺四级地址】参数:latitude={},longitude={}",latitude,longitude);
String resultStr = requestUtil.sendGetRequest(haierMStoreUrl, GET_STORE_FIVE_ADDR+"?latitude="+latitude+"&longitude="+longitude,null);
Map resultMap = JSONObject.parseObject(resultStr, Map.class);
StoreFiveAddrDto storeFiveAddrDto = JSONObject.parseObject(String.valueOf(resultMap.get("data")), StoreFiveAddrDto.class);
log.info("【根据店铺经纬度远程调用商城接口查询店铺四级地址】返回结果:storeFiveAddrDto={}",storeFiveAddrDto);
return storeFiveAddrDto;
}
}
//调用第三方接口工具类
@Slf4j
@Component
public class RequestUtil {
@Resource
private RestTemplate restTemplateClient;
@Resource
private MallRequest mallRequest;
/**
* post请求(商城请求)
*/
public String sendPostRequest(String path,String URI, Object params) {
log.info("发送POST请求 URL={}",path+URI);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization",mallRequest.getMallToken());
HttpEntity<Object> request = new HttpEntity<>(JSON.toJSONString(params), headers);
ResponseEntity<String> response = restTemplateClient.postForEntity(path+URI, request, String.class);
return response.getStatusCode().is2xxSuccessful() ? response.getBody() : null;
}
/**
* get 有参数
*/
public String sendGetRequest(String path,String URI,Object params) {
String mallToken = mallRequest.getMallToken();
log.info("发送GET请求 URL={},token={}",path+URI,mallToken);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization",mallToken);
HttpEntity<Object> requestEntity = new HttpEntity<>(JSON.toJSONString(params), headers);
ResponseEntity<String> response = restTemplateClient.exchange(path+URI, HttpMethod.GET, requestEntity, String.class);
System.out.println(response);
return response.getStatusCode().is2xxSuccessful() ? response.getBody() : null;
}
}