LocationController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package jnpf.base.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import io.swagger.v3.oas.annotations.Operation;
  4. import io.swagger.v3.oas.annotations.tags.Tag;
  5. import jnpf.base.ActionResult;
  6. import jnpf.base.model.province.MapParams;
  7. import jnpf.constant.MsgCode;
  8. import jnpf.util.NoDataSourceBind;
  9. import jnpf.util.ServletUtil;
  10. import jnpf.util.wxutil.HttpUtil;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import javax.imageio.ImageIO;
  15. import jakarta.servlet.http.HttpServletResponse;
  16. import java.awt.image.BufferedImage;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.net.HttpURLConnection;
  20. import java.net.URL;
  21. @Tag(name = "定位转发接口", description = "location")
  22. @RestController
  23. @RequestMapping("/api/system/Location")
  24. public class LocationController {
  25. @Operation(summary = "查询附近数据")
  26. @GetMapping("/around")
  27. @NoDataSourceBind
  28. public ActionResult<JSONObject> getAroundList(MapParams params) {
  29. JSONObject rstObj;
  30. String url = "https://restapi.amap.com/v3/place/around?key=" + params.getKey() + "&location=" + params.getLocation()
  31. + "&radius=" + params.getRadius() + "&offset=" + params.getOffset() + "&page=" + params.getPage();
  32. try {
  33. rstObj = HttpUtil.httpRequest(url, "GET", null);
  34. } catch (Exception e) {
  35. return ActionResult.fail(MsgCode.SYS023.get());
  36. }
  37. if (rstObj == null) {
  38. return ActionResult.fail(MsgCode.SYS024.get());
  39. }
  40. return ActionResult.success(rstObj);
  41. }
  42. @Operation(summary = "根据关键字查询附近数据")
  43. @GetMapping("/text")
  44. @NoDataSourceBind
  45. public ActionResult<JSONObject> getTextList(MapParams params) {
  46. JSONObject rstObj;
  47. String url = "https://restapi.amap.com/v3/place/text?key=" + params.getKey() + "&keywords=" + params.getKeywords()
  48. + "&radius=" + params.getRadius() + "&offset=" + params.getOffset() + "&page=" + params.getPage();
  49. try {
  50. rstObj = HttpUtil.httpRequest(url, "GET", null);
  51. } catch (Exception e) {
  52. return ActionResult.fail(MsgCode.SYS023.get());
  53. }
  54. if (rstObj == null) {
  55. return ActionResult.fail(MsgCode.SYS024.get());
  56. }
  57. return ActionResult.success(rstObj);
  58. }
  59. @Operation(summary = "输入提示")
  60. @GetMapping("/inputtips")
  61. @NoDataSourceBind
  62. public ActionResult<JSONObject> getInputTips(MapParams params) {
  63. JSONObject rstObj;
  64. String url = "https://restapi.amap.com/v3/assistant/inputtips?key=" + params.getKey() + "&keywords=" + params.getKeywords();
  65. try {
  66. rstObj = HttpUtil.httpRequest(url, "GET", null);
  67. } catch (Exception e) {
  68. return ActionResult.fail(MsgCode.SYS023.get());
  69. }
  70. if (rstObj == null) {
  71. return ActionResult.fail(MsgCode.SYS024.get());
  72. }
  73. return ActionResult.success(rstObj);
  74. }
  75. @Operation(summary = "定位图片")
  76. @GetMapping("/staticmap")
  77. @NoDataSourceBind
  78. public void staticmap(MapParams params) {
  79. String requestUrl = "https://restapi.amap.com/v3/staticmap?location=" + params.getLocation() + "&zoom=" + params.getZoom() + "&size="
  80. + params.getSize() + "&key=" + params.getKey();
  81. try {
  82. URL url = new URL(requestUrl);
  83. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  84. conn.setDoOutput(true);
  85. conn.setDoInput(true);
  86. conn.setUseCaches(false);
  87. conn.setRequestMethod("GET");
  88. conn.setConnectTimeout(50000);
  89. conn.setReadTimeout(60000);
  90. conn.setRequestProperty("Content-Type", "image/png");
  91. InputStream ins = null;
  92. OutputStream os = null;
  93. try {
  94. ins = conn.getInputStream();
  95. HttpServletResponse response = ServletUtil.getResponse();
  96. BufferedImage image = ImageIO.read(ins);
  97. os = response.getOutputStream();
  98. if (image != null) {
  99. ImageIO.write(image, "png", os);
  100. }
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. } finally {
  104. conn.disconnect();
  105. os.flush();
  106. os.close();
  107. ins.close();
  108. }
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. @Operation(summary = "经纬度转地址")
  114. @GetMapping("/regeo")
  115. @NoDataSourceBind
  116. public ActionResult<JSONObject> regeo(MapParams params) {
  117. JSONObject rstObj;
  118. String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + params.getKey() + "&&location=" + params.getLocation();
  119. try {
  120. rstObj = HttpUtil.httpRequest(url, "GET", null);
  121. } catch (Exception e) {
  122. return ActionResult.fail(MsgCode.SYS023.get());
  123. }
  124. if (rstObj == null) {
  125. return ActionResult.fail(MsgCode.SYS024.get());
  126. }
  127. return ActionResult.success(rstObj);
  128. }
  129. }