优化地理编码中的各种键,重新定义微信登录的常量URL、配置读取(去掉URL配置)

This commit is contained in:
jkcdev 2025-05-27 14:08:11 +08:00
parent 03dd870202
commit c6b0b2fdd6
4 changed files with 58 additions and 45 deletions

View File

@ -24,12 +24,26 @@ public class GeoCodingUtil {
private final static String LOCATION_TO_ADDRESS = "regeo";
private final static String ADDRESS_TO_LOCATION = "geo";
private final static String STATUS_KEY = "status";
private final static String LONGITUDE_KEY = "lng";
private final static String LATITUDE_KEY = "lat";
// 结果键 高德地图webservice 为geocodesregeocode腾讯地图webservice为result
private final static String ADDRESS_RESULT_KEY = "regeocode";
private final static String LOCATION_RESULT_KEY = "geocodes";
//private final static String RESULT_KEY = "geocodes";
// 成功状态码 高德地图webservice 为1腾讯地图webservice为0
private final static int CODE_STATUS_SUCCESS = 1;
// 地址键 高德地图webservice 为formatted_address腾讯地图webservice为formatted_addresses-->standard_address
//private final static String ADDRESS_KEY = "formatted_addresses";
private final static String ADDRESS_KEY = "formatted_address";
private final static String STANDARD_ADDRESS_KEY = "standard_address";
// 位置键 高德地图webservice 为location腾讯地图webservice为location
private final static String LOCATION_KEY = "location";
// 地址详情键 高德地图webservice 为addressComponent腾讯地图webservice为ad_info
private final static String ADDRESS_COMPONENT_KEY = "addressComponent";
private final static String DISTRICT_KEY = "district";
private final static String ADCODE_KEY = "adcode";
private final static String MSG_KEY = "message";
private final String url;
private final String accessKey;
@ -55,8 +69,10 @@ public class GeoCodingUtil {
* @return 地址
*/
public String getLocationToAddress(JSONObject location) {
//String locationStr = String.format("%f,%f", location.getDouble(LATITUDE_KEY), location.getDouble(LONGITUDE_KEY));
String locationStr = String.format("%f,%f", location.getDouble(LONGITUDE_KEY), location.getDouble(LATITUDE_KEY));
Request request = new Request.Builder()
.url(url + "/"+LOCATION_TO_ADDRESS + "?"+"location=" + String.format("%f,%f", location.getDouble("lng"), location.getDouble("lat")) + "&key=" + accessKey)
.url(url + "/"+LOCATION_TO_ADDRESS + "?"+"location=" + locationStr + "&key=" + accessKey)
.build();
try(Response response = client.newCall(request).execute()) {
if(response.isSuccessful()) {
@ -64,10 +80,10 @@ public class GeoCodingUtil {
String result = response.body().string();
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject.getInteger(STATUS_KEY) == CODE_STATUS_SUCCESS) {
//return jsonObject.getJSONObject(RESULT_KEY).getJSONObject("formatted_addresses").getString("standard_address");
return jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getString("formatted_address");
//return jsonObject.getJSONObject(RESULT_KEY).getJSONObject(ADDRESS_KEY).getString(STANDARD_ADDRESS_KEY);
return jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getString(ADDRESS_KEY);
}
logError("地址解析失败==>{}", jsonObject.getString("message"));
logError("地址解析失败==>{}", jsonObject.getString(MSG_KEY));
return null;
}
logError("地址解析失败==>{}", response.message());
@ -88,8 +104,10 @@ public class GeoCodingUtil {
* @return 地址
*/
public JSONObject getLocationToAddressDetails(JSONObject location) {
//String locationStr = String.format("%f,%f", location.getDouble(LATITUDE_KEY), location.getDouble(LONGITUDE_KEY));
String locationStr = String.format("%f,%f", location.getDouble(LONGITUDE_KEY), location.getDouble(LATITUDE_KEY));
Request request = new Request.Builder()
.url(url + "/"+LOCATION_TO_ADDRESS + "?location=" + String.format("%f,%f", location.getDouble("lng"), location.getDouble("lat")) + "&key=" + accessKey)
.url(url + "/"+LOCATION_TO_ADDRESS + "?location=" + locationStr + "&key=" + accessKey)
.build();
try(Response response = client.newCall(request).execute()) {
if(response.isSuccessful()) {
@ -98,18 +116,18 @@ public class GeoCodingUtil {
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject.getInteger(STATUS_KEY) == CODE_STATUS_SUCCESS) {
JSONObject address = new JSONObject();
//String detail = jsonObject.getJSONObject("result").getJSONObject("formatted_addresses").getString("standard_address");
String detail = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getString("formatted_address");
//String detail = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getJSONObject(ADDRESS_KEY).getString(STANDARD_ADDRESS_KEY);
String detail = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getString(ADDRESS_KEY);
address.put("detail", detail);
//String district = jsonObject.getJSONObject("result").getJSONObject("ad_info").getString("district");
String district = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getJSONObject("addressComponent").getString("district");
//String district = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getJSONObject(ADDRESS_COMPONENT_KEY).getString(DISTRICT_KEY);
String district = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getJSONObject(ADDRESS_COMPONENT_KEY).getString(DISTRICT_KEY);
address.put("district", district);
//String adcode = jsonObject.getJSONObject("result").getJSONObject("ad_info").getString("adcode");
String adcode = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getJSONObject("addressComponent").getString("adcode");
//String adcode = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getJSONObject(ADDRESS_COMPONENT_KEY).getString(ADCODE_KEY);
String adcode = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getJSONObject(ADDRESS_COMPONENT_KEY).getString(ADCODE_KEY);
address.put("adcode", adcode);
return address;
}
logError("地址解析失败==>{}", jsonObject.getString("message"));
logError("地址解析失败==>{}", jsonObject.getString(MSG_KEY));
return null;
}
logError("地址解析失败==>{}", response.message());
@ -139,15 +157,16 @@ public class GeoCodingUtil {
String result = response.body().string();
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject.getInteger(STATUS_KEY) == CODE_STATUS_SUCCESS) {
//return jsonObject.getJSONObject(LOCATION_RESULT_KEY).getJSONObject("location");
String loc = jsonObject.getJSONArray(LOCATION_RESULT_KEY).getJSONObject(0).getString("location");
// 腾讯地图webservice的位置是 JSONObject {"lat": 39.9042, "lng": 116.4074}
//return jsonObject.getJSONObject(LOCATION_RESULT_KEY).getJSONObject(LOCATION_KEY);
String loc = jsonObject.getJSONArray(LOCATION_RESULT_KEY).getJSONObject(0).getString(LOCATION_KEY);
String[] locArr = loc.split(",");
JSONObject location = new JSONObject();
location.put("lng", Double.valueOf(locArr[0]));
location.put("lat", Double.valueOf(locArr[1]));
return location;
}else{
logError("位置解析失败==>{}", jsonObject.getString("message"));
logError("位置解析失败==>{}", jsonObject.getString(MSG_KEY));
return null;
}
}else{

View File

@ -19,12 +19,13 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "wechat")
public class WechatConfig {
private String url;
/**
* 微信小程序 appId
*/
private String appId;
/**
* 微信小程序 appSecret
*/
private String appSecret;
@Bean
public WechatUtil wechatUtil() {
return new WechatUtil(url, appId, appSecret);
}
}

View File

@ -1,10 +1,10 @@
package com.cdzy.orders.uitls;
import com.alibaba.fastjson2.JSONObject;
import com.cdzy.orders.config.WechatConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.stereotype.Service;
@ -12,9 +12,9 @@ import org.springframework.stereotype.Service;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.Security;
import java.security.*;
import java.util.Base64;
/**
@ -27,24 +27,25 @@ import java.util.Base64;
@Slf4j
@Service
public class WechatUtil {
/**
* 微信登录API地址
*/
private static final String WECHAT_LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";
private static String url;
private static String appId;
private static String appSecret;
/**
* OkHttpClient实例
*/
private static OkHttpClient client = null;
@Resource
private WechatConfig wechatConfig;
/**
* 微信工具类构造函数
*
* @param apiUrl 微信登录API地址
* @param appId 微信小程序ID
* @param appSecret 微信小程序密钥
*/
public WechatUtil(String apiUrl, String appId, String appSecret) {
this.url = apiUrl;
this.appId = appId;
this.appSecret = appSecret;
this.client = new OkHttpClient();
public WechatUtil() throws IOException {
client = new OkHttpClient();
}
/**
@ -55,7 +56,7 @@ public class WechatUtil {
*/
public JSONObject wechatAuthority(String code){
Request request = new Request.Builder()
.url(url + "?appid=" + appId + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=authorization_code")
.url(WECHAT_LOGIN_URL + "?appid=" + wechatConfig.getAppId() + "&secret=" + wechatConfig.getAppSecret() + "&js_code=" + code + "&grant_type=authorization_code")
.build();
try(Response response = client.newCall(request).execute()) {
if(response.isSuccessful()) {
@ -108,11 +109,4 @@ public class WechatUtil {
}
}
//public static void main(String[] args) {
// String encryptedData = "eXzeeTDZZVYGXr1YfFxI3Z7ntaEzuFQ5DHNSSB1OEGaN4s1gxBv8/o2YIRKnyghvaCXhs+BuRZlM1mqbw/4Q+hAA9Zb0E+hzipuTHuVNEFrq7SwUZbb7WUgmrtD+9vAf/8XdHkXzs+Krybh/a7Pa7QnKx82Z1HQvP+I4cWVIhBJrhqFQX0TP2XuJ8OTliBEE+MnxGylvpqDzXxVPRA4CAQ==";
// String sessionkey = "6bWACSG9u15Lv2YI9B5IKw==";
// String iv = "PeEtJMMfPStNH0Pd61DB0A==";
// String result = decryptData(encryptedData, sessionkey, iv);
// System.out.println(result);
//}
}

View File

@ -85,7 +85,6 @@ sa-token:
# 是否输出操作日志
is-log: true
wechat:
url: https://api.weixin.qq.com/sns/jscode2session
appid: wx327d788d7bd6eddf
app-secret: adf2539a6c26499c67b5a3829f2e05e3
minio: