提交git
This commit is contained in:
@ -0,0 +1,80 @@
|
||||
package cc.uling.plugins.exchange;
|
||||
|
||||
import cc.uling.common.constant.CacheKeys;
|
||||
import cc.uling.common.utils.StringUtil;
|
||||
import cc.uling.plugins.exchange.config.Config;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.jfinal.aop.Aop;
|
||||
import com.web.consts.SysConfigKey;
|
||||
import com.web.gen.service.SysConfigService;
|
||||
import io.jboot.aop.annotation.Bean;
|
||||
import io.jboot.utils.CacheUtil;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Bean
|
||||
public abstract class ExchangeRageService implements CurrencyExchangePluginsService {
|
||||
|
||||
protected final SysConfigService sysConfigService;
|
||||
|
||||
public ExchangeRageService() {
|
||||
this.sysConfigService = Aop.get(SysConfigService.class);
|
||||
}
|
||||
|
||||
private static Map<String, ExchangeRageService> serviceMap = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public BigDecimal getByCache(String currency, String toCurrency) {
|
||||
if (currency == null) {
|
||||
return null;
|
||||
}
|
||||
String search = "USDT".equalsIgnoreCase(toCurrency) ? "USD" : toCurrency;
|
||||
if (currency.equalsIgnoreCase(search)) {
|
||||
return BigDecimal.ONE;
|
||||
}
|
||||
BigDecimal rate = CacheUtil.get(CacheKeys.ExchangeRate, currency + "_" + search);
|
||||
if (rate != null) {
|
||||
return rate;
|
||||
}
|
||||
//获取配置
|
||||
String configValue = sysConfigService.getValueByKey(SysConfigKey.EXCHANGE_RATE_CONFIG);
|
||||
if (StringUtil.isBlank(configValue)) {
|
||||
return null;
|
||||
}
|
||||
Config config = JSONUtil.toBean(configValue, Config.class);
|
||||
if (config == null) {
|
||||
return null;
|
||||
}
|
||||
ExchangeRageService service = serviceMap.get(config.getType());
|
||||
if (service == null) {
|
||||
rate = doService(config, configValue, currency, search);
|
||||
} else {
|
||||
rate = service.getRate(configValue, currency, search);
|
||||
}
|
||||
if (rate != null) {
|
||||
CacheUtil.put(CacheKeys.ExchangeRate, currency + "_" + search, rate, config.getExpire().intValue());
|
||||
}
|
||||
return rate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, BigDecimal> getRates(String currency) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
|
||||
private BigDecimal doService(Config config, String configValue, String currency, String toCurrency) {
|
||||
ExchangeRateEnum type = ExchangeRateEnum.valueOf(config.getType());
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
ExchangeRageService service = Aop.get(type.getService());
|
||||
if (service == null) {
|
||||
return null;
|
||||
}
|
||||
return service.getRate(configValue, currency, toCurrency);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
package cc.uling.plugins.exchange;
|
||||
|
||||
import cc.uling.common.utils.HttpUtil;
|
||||
import cc.uling.common.utils.StringUtil;
|
||||
import cc.uling.plugins.exchange.config.ApiConfig;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import io.jboot.aop.annotation.Bean;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Bean
|
||||
public class ExchangeRateApiServiceImpl extends ExchangeRageService implements CurrencyExchangePluginsService {
|
||||
|
||||
|
||||
private static final String URL = "https://v6.exchangerate-api.com/v6/{0}/latest/{1}";
|
||||
|
||||
public BigDecimal getByCache(String currency, String toCurrency) {
|
||||
return super.getByCache(currency, toCurrency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigDecimal getRate(String config, String currency, String toCurrency) {
|
||||
ApiConfig apiConfig = getConfig(config);
|
||||
String url = MessageFormat.format(URL, apiConfig.getApiKey(), currency);
|
||||
String result = HttpUtil.doGet(url, new HashMap<>());
|
||||
if (StringUtil.isBlank(result)) {
|
||||
return null;
|
||||
}
|
||||
if (!JSONUtil.isTypeJSON(result)) {
|
||||
return null;
|
||||
}
|
||||
JSONObject json = JSONUtil.parseObj(result);
|
||||
if (!json.containsKey("result") || !"success".equalsIgnoreCase(json.getStr("result")) || !json.containsKey("conversion_rates")) {
|
||||
return null;
|
||||
}
|
||||
JSONObject rates = json.getJSONObject("conversion_rates");
|
||||
if (!rates.containsKey(toCurrency)) {
|
||||
return null;
|
||||
}
|
||||
return rates.getBigDecimal(toCurrency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, BigDecimal> getRates(String currency) {
|
||||
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
public ApiConfig getConfig(String config) {
|
||||
return JSONUtil.toBean(config, ApiConfig.class);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package cc.uling.plugins.exchange;
|
||||
|
||||
public enum ExchangeRateEnum {
|
||||
|
||||
EXCHANGE_RATE_API(ExchangeRateApiServiceImpl.class),
|
||||
;
|
||||
|
||||
private Class<ExchangeRageService> service;
|
||||
|
||||
ExchangeRateEnum(Class service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public Class<ExchangeRageService> getService() {
|
||||
return service;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package cc.uling.plugins.maps;
|
||||
|
||||
import cc.uling.plugins.maps.config.BaseConfig;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class BaseMapApiServiceImpl<T extends BaseConfig> implements MapsApiService<T> {
|
||||
|
||||
|
||||
public String contact(String... places) {
|
||||
return Arrays.stream(places).filter(Objects::nonNull).collect(Collectors.joining(", "));
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
package cc.uling.plugins.maps;
|
||||
|
||||
import cc.uling.common.utils.HttpUtil;
|
||||
import cc.uling.plugins.maps.config.GoogleConfig;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.google.maps.GeoApiContext;
|
||||
import com.google.maps.GeocodingApi;
|
||||
import com.google.maps.model.AddressComponent;
|
||||
import com.google.maps.model.AddressComponentType;
|
||||
import com.google.maps.model.GeocodingResult;
|
||||
import io.jboot.aop.annotation.Bean;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Bean
|
||||
public class GoogleMapsApiServiceImpl extends BaseMapApiServiceImpl<GoogleConfig> {
|
||||
|
||||
@Override
|
||||
public String getPostalCode(GoogleConfig config, String address) {
|
||||
Map<String, String> data = searchPlaceDetails(config, address);
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
log.info(" data {} ", JSONUtil.toJsonStr(data));
|
||||
return data.get(AddressComponentType.POSTAL_CODE.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public GoogleConfig getConfig(String config) {
|
||||
return JSONUtil.toBean(config, GoogleConfig.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> searchPlaceDetails(GoogleConfig config, String address) {
|
||||
log.info(" apikey {}", config.getConfig());
|
||||
GeoApiContext context = new GeoApiContext.Builder().apiKey(config.getConfig()).build();
|
||||
Map<String, String> data = new HashMap<>();
|
||||
GeocodingResult[] results = null;
|
||||
log.info(" 谷歌API获取邮政编码 {} ", address);
|
||||
try {
|
||||
results = GeocodingApi.geocode(context, address).await();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 发送地理编码请求
|
||||
log.info(" results {} ", JSONUtil.toJsonStr(results));
|
||||
if (results == null || results.length == 0) {
|
||||
return null;
|
||||
}
|
||||
// 解析地址组件查找邮政编码
|
||||
for (AddressComponent component : results[0].addressComponents) {
|
||||
for (AddressComponentType type : component.types) {
|
||||
data.put(type.toString(), component.longName);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private static final String API_KEY = "AIzaSyDuhvBCcejeZSI5v-OxKbJ2fYZmt1dqmOw";
|
||||
private static final String BASE_URL = "https://maps.googleapis.com/maps/api/geocode/json";
|
||||
|
||||
public static String getPostalCode(String address) throws Exception {
|
||||
String encodedAddress = java.net.URLEncoder.encode(address, "UTF-8");
|
||||
String url = BASE_URL + "?address=" + encodedAddress + "&key=" + API_KEY;
|
||||
String result = HttpUtil.doGet(url, new HashMap<>());
|
||||
log.info("result {}", result);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
String postalCode = getPostalCode("Agoura Hills, California, US");
|
||||
System.out.println("邮政编码: " + postalCode);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
package cc.uling.plugins.maps;
|
||||
|
||||
import cc.uling.common.utils.StringUtil;
|
||||
import cc.uling.plugins.maps.config.BaseConfig;
|
||||
import cc.uling.plugins.maps.config.MapApiConfig;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.jfinal.aop.Aop;
|
||||
import com.jfinal.aop.Inject;
|
||||
import com.web.consts.SysConfigKey;
|
||||
import com.web.gen.service.SysConfigService;
|
||||
import io.jboot.aop.annotation.Bean;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Bean
|
||||
public class MapApiServiceImpl<T extends BaseConfig> implements MapsApiPlugins {
|
||||
|
||||
@Inject
|
||||
private SysConfigService sysConfigService;
|
||||
|
||||
private final Map<String, MapsApiService<T>> serviceMap = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public String getPostalCode(String... places) {
|
||||
String config = sysConfigService.getValueByKey(SysConfigKey.MAPS_API_CONFIG);
|
||||
if (StringUtil.isBlank(config) || !JSONUtil.isTypeJSON(config)) {
|
||||
return null;
|
||||
}
|
||||
MapApiConfig mapApiConfig = JSONUtil.toBean(config, MapApiConfig.class);
|
||||
MapsApiService<T> apiService = serviceMap.get(mapApiConfig.getType());
|
||||
if (apiService == null) {
|
||||
apiService = getService(mapApiConfig.getType());
|
||||
serviceMap.put(mapApiConfig.getType(), apiService);
|
||||
}
|
||||
if (apiService == null) {
|
||||
return null;
|
||||
}
|
||||
T baseConfig = apiService.getConfig(config);
|
||||
String address = apiService.contact(places);
|
||||
return apiService.getPostalCode(baseConfig, address);
|
||||
}
|
||||
|
||||
public MapsApiService getService(String type) {
|
||||
MapsApiService apiService = serviceMap.get(type);
|
||||
if (apiService == null) {
|
||||
MapsApiEnum apiEnum = MapsApiEnum.valueOf(type);
|
||||
if (apiEnum != null) {
|
||||
apiService = Aop.get(apiEnum.getService());
|
||||
serviceMap.put(type, apiService);
|
||||
}
|
||||
}
|
||||
return apiService;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package cc.uling.plugins.maps;
|
||||
|
||||
public enum MapsApiEnum {
|
||||
|
||||
GOOGLE_MAPS_API(GoogleMapsApiServiceImpl.class),
|
||||
;
|
||||
|
||||
private Class<GoogleMapsApiServiceImpl> service;
|
||||
|
||||
MapsApiEnum(Class service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public Class<GoogleMapsApiServiceImpl> getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
package cc.uling.plugins.maps;
|
||||
|
||||
import cc.uling.plugins.maps.config.BaseConfig;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 地图API
|
||||
*/
|
||||
public interface MapsApiService<T extends BaseConfig> {
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 拼接地址
|
||||
*
|
||||
* @param places
|
||||
* @return
|
||||
*/
|
||||
String contact(String... places);
|
||||
|
||||
/**
|
||||
* 获取邮政编码
|
||||
*
|
||||
* @param address
|
||||
* @return
|
||||
*/
|
||||
String getPostalCode(T config, String address);
|
||||
|
||||
/**
|
||||
* @param config
|
||||
* @return
|
||||
*/
|
||||
T getConfig(String config);
|
||||
|
||||
/**
|
||||
* 查询地点详情
|
||||
*
|
||||
* @param address 地址(国家、州/省份、城市、地区、详细地址)
|
||||
* @return
|
||||
*/
|
||||
Map<String, String> searchPlaceDetails(T config, String address);
|
||||
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package cc.uling.plugins.maps.config;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
|
||||
public class BaseConfig<T> {
|
||||
|
||||
private String url;
|
||||
|
||||
private T config;
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSONUtil.toJsonStr(this);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package cc.uling.plugins.maps.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Slf4j
|
||||
public class GoogleConfig extends BaseConfig<String> {
|
||||
|
||||
private String config;
|
||||
|
||||
public static void main(String[] args) {
|
||||
GoogleConfig googleConfig = new GoogleConfig();
|
||||
googleConfig.setUrl("https://maps.googleapis.com");
|
||||
googleConfig.setConfig("AIzaSyDuhvBCcejeZSI5v-OxKbJ2fYZmt1dqmOw");
|
||||
log.info(" GoogleConfig {} ", googleConfig.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package cc.uling.plugins.maps.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class MapApiConfig implements Serializable {
|
||||
|
||||
private String type;
|
||||
|
||||
private String config;
|
||||
}
|
||||
Reference in New Issue
Block a user