202506041836
This commit is contained in:
parent
fb692a6936
commit
cc4234b257
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> params) throws Exception {
|
||||
StringBuilder sb = new StringBuilder(API_URL);
|
||||
sb.append(path);
|
||||
sb.append("?");
|
||||
|
||||
// 添加参数
|
||||
for (Map.Entry<String, String> 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<String, String> 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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue