package com.fdkankan.fusion.controller; import cn.hutool.core.util.URLUtil; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.fdkankan.fusion.common.ResultCode; import com.fdkankan.fusion.common.ResultData; import com.fdkankan.fusion.entity.MapConfig; import com.fdkankan.fusion.exception.BusinessException; import com.fdkankan.fusion.request.MapParam; import com.fdkankan.fusion.response.MapVo; import com.fdkankan.fusion.service.IMapConfigService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; /** *

* 前端控制器 *

* * @author * @since 2024-12-10 */ @RestController @RequestMapping("/mapConfig") @Slf4j public class MapConfigController { @Autowired IMapConfigService mapConfigService; @PostMapping("/geocode") public ResultData geocode(@RequestBody MapParam param){ try { if(param.getMapId() == null || StringUtils.isBlank(param.getAddress())){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } MapConfig mapConfig = mapConfigService.getById(param.getMapId()); if(mapConfig == null || mapConfig.getGeocodeUrl() == null){ throw new BusinessException(ResultCode.RECORD_NOT_EXIST); } String url = mapConfig.getGeocodeUrl().replace("{address}",param.getAddress()); String s = HttpUtil.get(url); JSONObject jsonObject = JSON.parseObject(s); JSONArray jsonArray = jsonObject.getJSONArray(mapConfig.getRespListKey()); List mapVos = new ArrayList<>(); for (Object object : jsonArray) { JSONObject jsonObject1 = (JSONObject) object; MapVo vo = new MapVo(); vo.setLocation(jsonObject1.getString(mapConfig.getRespLocationKey())); vo.setAddress(jsonObject1.getString(mapConfig.getRespAddressKey())); vo.setName(jsonObject1.getString(mapConfig.getRespNameKey())); mapVos.add(vo); } return ResultData.ok(mapVos); }catch ( Exception e){ log.info("map-error:{}",e); } return ResultData.ok(); } public static void main(String[] args) { String s = HttpUtil.get("https://restapi.amap.com/v3/place/text?keywords={address}&offset=20&page=1&key=3609daa52e8ae4493393292213e2fb98&extensions=base"); } }