From cc4234b257043c710030004cbae77d12bed6aba4 Mon Sep 17 00:00:00 2001 From: "925116093-qq.com" <925116093@qq.com> Date: Wed, 4 Jun 2025 18:36:25 +0800 Subject: [PATCH] 202506041836 --- 683e68d5ab764623781efbf0/orderUtil.java | 21 ++ .../com/ruoyi/common/config/AmapConfig.java | 41 +++ .../com/ruoyi/common/utils/AmapUtils.java | 302 ++++++++++++++++++ 3 files changed, 364 insertions(+) create mode 100644 683e68d5ab764623781efbf0/orderUtil.java create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/config/AmapConfig.java create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/utils/AmapUtils.java diff --git a/683e68d5ab764623781efbf0/orderUtil.java b/683e68d5ab764623781efbf0/orderUtil.java new file mode 100644 index 0000000..6c91073 --- /dev/null +++ b/683e68d5ab764623781efbf0/orderUtil.java @@ -0,0 +1,21 @@ +package com.ruoyi.system.controllerUtil; + +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.system.service.IUsersService; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * 服务订单Controller工具类 + * + */ +public class orderUtil { + @Autowired + public IUsersService usersService; + //1,根据用户手机号判断用户不存在 + public static int isUser(String userid){ + + + return 1; + } + +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/config/AmapConfig.java b/ruoyi-common/src/main/java/com/ruoyi/common/config/AmapConfig.java new file mode 100644 index 0000000..47e6d7a --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/config/AmapConfig.java @@ -0,0 +1,41 @@ +package com.ruoyi.common.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "amap") +public class AmapConfig { + /** 高德地图Key */ + private String key; + + /** 高德地图密钥 */ + private String secret; + + /** 高德地图API地址 */ + private String apiUrl = "https://restapi.amap.com/v3"; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getSecret() { + return secret; + } + + public void setSecret(String secret) { + this.secret = secret; + } + + public String getApiUrl() { + return apiUrl; + } + + public void setApiUrl(String apiUrl) { + this.apiUrl = apiUrl; + } +} \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/AmapUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/AmapUtils.java new file mode 100644 index 0000000..c020145 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/AmapUtils.java @@ -0,0 +1,302 @@ +package com.ruoyi.common.utils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.ruoyi.common.config.AmapConfig; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.HashMap; +import java.util.Map; + +@Component +public class AmapUtils { + private static final Logger log = LoggerFactory.getLogger(AmapUtils.class); + + @Resource + private AmapConfig amapConfig; + + + private static final String API_URL = "https://restapi.amap.com/v3"; + private static final String API_KEY = "308ad08f306d74daddffba44f5537767"; + private static final String API_SECRET = "8c58e51cb91b527f0fb863b3c97ef3c7"; + + + + /** + * 地理编码 + * @param address 地址 + * @return 返回经纬度等信息 + */ + public JSONObject geocode(String address) { + try { + Map params = new HashMap<>(); + params.put("key", API_KEY); + params.put("address", address); + + String url = buildUrl("/geocode/geo", params); + String response = sendGetRequest(url); + return JSON.parseObject(response); + } catch (Exception e) { + log.error("地理编码请求失败", e); + return null; + } + } + + /** + * 逆地理编码 + * @param location 经纬度,格式:longitude,latitude + * @return 返回地址等信息 + */ + public JSONObject regeocode(String location) { + try { + Map params = new HashMap<>(); + params.put("key", API_KEY); + params.put("location", location); + params.put("extensions", "all"); // 返回扩展信息 + + String url = buildUrl("/geocode/regeo", params); + String response = sendGetRequest(url); + return JSON.parseObject(response); + } catch (Exception e) { + log.error("逆地理编码请求失败", e); + return null; + } + } + + /** + * 路径规划 + * @param origin 起点经纬度 + * @param destination 终点经纬度 + * @param type 路径规划类型:driving(驾车)、walking(步行)、bicycling(骑行)、transit(公交) + * @return 返回路径规划信息 + */ + public JSONObject direction(String origin, String destination, String type) { + try { + Map params = new HashMap<>(); + params.put("key", API_KEY); + params.put("origin", origin); + params.put("destination", destination); + params.put("extensions", "all"); // 返回扩展信息 + + String url = buildUrl("/direction/" + type, params); + String response = sendGetRequest(url); + return JSON.parseObject(response); + } catch (Exception e) { + log.error("路径规划请求失败", e); + return null; + } + } + + /** + * 关键字搜索 + * @param keywords 关键字 + * @param city 城市 + * @param type POI类型 + * @return 返回POI信息 + */ + public JSONObject placeSearch(String keywords, String city, String type) { + try { + Map params = new HashMap<>(); + params.put("key", API_KEY); + params.put("keywords", keywords); + params.put("city", city); + if (type != null) { + params.put("types", type); + } + params.put("extensions", "all"); // 返回扩展信息 + + String url = buildUrl("/place/text", params); + String response = sendGetRequest(url); + return JSON.parseObject(response); + } catch (Exception e) { + log.error("关键字搜索请求失败", e); + return null; + } + } + + /** + * 周边搜索 + * @param location 中心点经纬度 + * @param radius 搜索半径,单位:米 + * @param type POI类型 + * @return 返回POI信息 + */ + public JSONObject placeAround(String location, int radius, String type) { + try { + Map params = new HashMap<>(); + params.put("key", API_KEY); + params.put("location", location); + params.put("radius", String.valueOf(radius)); + if (type != null) { + params.put("types", type); + } + params.put("extensions", "all"); // 返回扩展信息 + + String url = buildUrl("/place/around", params); + String response = sendGetRequest(url); + return JSON.parseObject(response); + } catch (Exception e) { + log.error("周边搜索请求失败", e); + return null; + } + } + + /** + * 计算两点之间的距离 + * @param origins 起点经纬度,支持多个,格式:longitude1,latitude1;longitude2,latitude2 + * @param destination 终点经纬度 + * @param type 计算方式:straight(直线距离)、driving(驾车导航距离) + * @return 返回距离信息 + */ + public JSONObject distance(String origins, String destination, String type) { + try { + Map params = new HashMap<>(); + params.put("key", API_KEY); + params.put("origins", origins); + params.put("destination", destination); + params.put("type", type); + + String url = buildUrl("/distance", params); + String response = sendGetRequest(url); + return JSON.parseObject(response); + } catch (Exception e) { + log.error("距离计算请求失败", e); + return null; + } + } + + /** + * 获取行政区域 + * @param keywords 关键字 + * @param subdistrict 子级行政区:0、1、2、3 + * @return 返回行政区域信息 + */ + public JSONObject district(String keywords, int subdistrict) { + try { + Map params = new HashMap<>(); + params.put("key", API_KEY); + params.put("keywords", keywords); + params.put("subdistrict", String.valueOf(subdistrict)); + params.put("extensions", "all"); // 返回扩展信息 + + String url = buildUrl("/config/district", params); + String response = sendGetRequest(url); + return JSON.parseObject(response); + } catch (Exception e) { + log.error("获取行政区域失败", e); + return null; + } + } + + /** + * IP定位 + * @param ip IP地址 + * @return 返回定位信息 + */ + public JSONObject ip(String ip) { + try { + Map params = new HashMap<>(); + params.put("key", API_KEY); + params.put("ip", ip); + + String url = buildUrl("/ip", params); + String response = sendGetRequest(url); + return JSON.parseObject(response); + } catch (Exception e) { + log.error("IP定位请求失败", e); + return null; + } + } + + /** + * 天气查询 + * @param city 城市编码 + * @return 返回天气信息 + */ + public JSONObject weather(String city) { + try { + Map params = new HashMap<>(); + params.put("key", API_KEY); + params.put("city", city); + params.put("extensions", "all"); // 返回预报天气 + + String url = buildUrl("/weather/weatherInfo", params); + String response = sendGetRequest(url); + return JSON.parseObject(response); + } catch (Exception e) { + log.error("天气查询请求失败", e); + return null; + } + } + + /** + * 构建请求URL + */ + private String buildUrl(String path, Map params) throws Exception { + StringBuilder sb = new StringBuilder(API_URL); + sb.append(path); + sb.append("?"); + + // 添加参数 + for (Map.Entry entry : params.entrySet()) { + sb.append(entry.getKey()) + .append("=") + .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name())) + .append("&"); + } + + // 添加数字签名 + + String sig = generateSignature(params, API_SECRET); + sb.append("sig=").append(sig); + + + return sb.toString(); + } + + /** + * 发送GET请求 + */ + private String sendGetRequest(String url) throws IOException { + HttpClient client = HttpClients.createDefault(); + HttpGet request = new HttpGet(url); + HttpResponse response = client.execute(request); + return EntityUtils.toString(response.getEntity()); + } + + /** + * 生成数字签名 + */ + private String generateSignature(Map params, String secret) throws NoSuchAlgorithmException { + StringBuilder sb = new StringBuilder(); + // 按参数名称升序排序 + params.entrySet().stream() + .sorted(Map.Entry.comparingByKey()) + .forEach(entry -> sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&")); + sb.append(secret); + + // 计算MD5 + MessageDigest md5 = MessageDigest.getInstance("MD5"); + byte[] bytes = md5.digest(sb.toString().getBytes(StandardCharsets.UTF_8)); + + // 转换为16进制字符串 + StringBuilder result = new StringBuilder(); + for (byte b : bytes) { + result.append(String.format("%02x", b)); + } + return result.toString(); + } +} \ No newline at end of file