Commit f797b3aa sunke

国际化配置

1 个父辈 e0aa4f81
...@@ -36,9 +36,9 @@ import java.util.Enumeration; ...@@ -36,9 +36,9 @@ import java.util.Enumeration;
public class StringUtils extends org.apache.commons.lang3.StringUtils { public class StringUtils extends org.apache.commons.lang3.StringUtils {
private static final Logger log = LoggerFactory.getLogger(StringUtils.class); private static final Logger log = LoggerFactory.getLogger(StringUtils.class);
private static boolean ipLocal = false; // private static boolean ipLocal = false;
private static File file = null; // private static File file = null;
private static DbConfig config; // private static DbConfig config;
private static final char SEPARATOR = '_'; private static final char SEPARATOR = '_';
private static final String UNKNOWN = "unknown"; private static final String UNKNOWN = "unknown";
...@@ -50,24 +50,24 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils { ...@@ -50,24 +50,24 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
// .build(); // .build();
static { // static {
SpringContextHolder.addCallBacks(() -> { // SpringContextHolder.addCallBacks(() -> {
StringUtils.ipLocal = SpringContextHolder.getProperties("ip.local-parsing", false, Boolean.class); // StringUtils.ipLocal = SpringContextHolder.getProperties("ip.local-parsing", false, Boolean.class);
if (ipLocal) { // if (ipLocal) {
/* // /*
* 此文件为独享 ,不必关闭 // * 此文件为独享 ,不必关闭
*/ // */
String path = "ip2region/ip2region.db"; // String path = "ip2region/ip2region.db";
String name = "ip2region.db"; // String name = "ip2region.db";
try { // try {
config = new DbConfig(); // config = new DbConfig();
file = FileUtil.inputStreamToFile(new ClassPathResource(path).getInputStream(), name); // file = FileUtil.inputStreamToFile(new ClassPathResource(path).getInputStream(), name);
} catch (Exception e) { // } catch (Exception e) {
log.error(e.getMessage(), e); // log.error(e.getMessage(), e);
} // }
} // }
}); // });
} // }
/** /**
* 驼峰命名法工具 * 驼峰命名法工具
......
...@@ -9,11 +9,13 @@ import com.neotel.smfcore.core.barcode.utils.CodeResolve; ...@@ -9,11 +9,13 @@ import com.neotel.smfcore.core.barcode.utils.CodeResolve;
import com.neotel.smfcore.core.storage.bean.InventoryItem; import com.neotel.smfcore.core.storage.bean.InventoryItem;
import com.neotel.smfcore.core.storage.enums.CHECKOUT_TYPE; import com.neotel.smfcore.core.storage.enums.CHECKOUT_TYPE;
import com.neotel.smfcore.core.storage.service.po.StoragePos; import com.neotel.smfcore.core.storage.service.po.StoragePos;
import com.neotel.smfcore.core.system.service.dao.ICacheItemDao;
import com.neotel.smfcore.core.system.service.dao.ISettingsDao; import com.neotel.smfcore.core.system.service.dao.ISettingsDao;
import com.neotel.smfcore.core.barcode.bean.PlateSizeBean; import com.neotel.smfcore.core.barcode.bean.PlateSizeBean;
import com.neotel.smfcore.core.system.service.manager.IDataLogManager; import com.neotel.smfcore.core.system.service.manager.IDataLogManager;
import com.neotel.smfcore.core.storage.service.manager.IStorageManager; import com.neotel.smfcore.core.storage.service.manager.IStorageManager;
import com.neotel.smfcore.core.storage.service.manager.IStoragePosManager; import com.neotel.smfcore.core.storage.service.manager.IStoragePosManager;
import com.neotel.smfcore.core.system.service.po.CacheItem;
import com.neotel.smfcore.core.system.service.po.Settings; import com.neotel.smfcore.core.system.service.po.Settings;
import com.neotel.smfcore.core.storage.service.po.Storage; import com.neotel.smfcore.core.storage.service.po.Storage;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
...@@ -41,25 +43,23 @@ public class DataCache { ...@@ -41,25 +43,23 @@ public class DataCache {
private IStorageManager storageManager; private IStorageManager storageManager;
@Autowired @Autowired
private IDataLogManager dataLogManager;
@Autowired
private ISettingsDao settingsDao; private ISettingsDao settingsDao;
@Autowired @Autowired
private CodeResolve codeResolve; private CodeResolve codeResolve;
//
// @Autowired @Autowired
// private IBarcodeManager barcodeManager; private ICacheItemDao cacheItemDao;
//
// @Autowired
// private IComponentManager componentManager;
//
/** /**
* 是否需要推送温湿度报警值 * 是否需要推送温湿度报警值
*/ */
public static Map<String, Boolean> needUpdateHumidityMap = new ConcurrentHashMap<>(); public static Map<String, Boolean> needUpdateHumidityMap = new ConcurrentHashMap<>();
//
/**
* 一些设置的缓存
*/
public static Map<String, Object> cacheMap = Maps.newConcurrentMap();
/** /**
* 出库方式 * 出库方式
*/ */
...@@ -68,6 +68,37 @@ public class DataCache { ...@@ -68,6 +68,37 @@ public class DataCache {
@PostConstruct @PostConstruct
public void initialize() { public void initialize() {
settings = getSettings(); settings = getSettings();
initCacheItem();
}
private void initCacheItem(){
cacheMap = Maps.newConcurrentMap();
List<CacheItem> all = cacheItemDao.findAll();
for (CacheItem cacheItem : all) {
cacheMap.put(cacheItem.getCacheKey(), cacheItem.getCacheValue());
}
}
/**
* 获取缓存信息
* @param cacheKey
* @param <T>
* @return
*/
public <T> T getCache(String cacheKey){
Object value = cacheMap.get(cacheKey);
if(value != null){
return (T)value;
}
return null;
}
/**
* 更新缓存信息
*/
public void updateCache(String cacheKey, Object value){
cacheItemDao.updateCacheItem(cacheKey,value);
cacheMap.put(cacheKey,value);
} }
/** /**
...@@ -418,18 +449,4 @@ public class DataCache { ...@@ -418,18 +449,4 @@ public class DataCache {
inventoryMap.put(cid, storageInventory); inventoryMap.put(cid, storageInventory);
} }
// public Map<String,String> allBoxNameMap(){
// Map<String, String> allBoxMap = Maps.newHashMap();
// for (Storage storage : getAllStorage().values()){
// if(storage.getBoxCount() > 1){
// for (int i=1;i<=storage.getBoxCount();i++){
// allBoxMap.put(storage.getCid()+"-"+i, storage.getName() + " BOX-" + i);
// }
// }else{
// allBoxMap.put(storage.getCid()+"-i", storage.getName());
// }
// }
// return allBoxMap;
// }
} }
package com.neotel.smfcore.core.language.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
/**
* 配置国际化语言
* Created by sunke on 2021/7/30.
*/
@Configuration
public class LocaleConfig {
/**
* 默认解析器 其中locale表示默认语言
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.CHINESE);
return localeResolver;
}
/**
* 默认拦截器 其中lang表示切换语言的参数名
*/
@Bean
public WebMvcConfigurer localeInterceptor() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor);
}
};
}
}
package com.neotel.smfcore.core.language.util;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Locale;
/**
* 国际化工具类
* Created by sunke on 2021/7/30.
*/
@Component
public class MessageUtils {
private static MessageSource messageSource;
public MessageUtils(MessageSource messageSource) {
MessageUtils.messageSource = messageSource;
}
public String getText(String msgKey, Locale locale,String defaultMsg) {
return getText(msgKey,null, locale,defaultMsg);
}
public String getText(String msgKey, String[] params, Locale locale, String defaultMsg) {
try{
if (params != null) {
return messageSource.getMessage(msgKey, params, locale);
} else {
return messageSource.getMessage(msgKey, new String[]{}, locale);
}
}catch (Exception ex){
if(defaultMsg != null){
return defaultMsg;
}
return msgKey;
}
}
}
package com.neotel.smfcore.core.system.enums;
/**
* Created by sunke on 2021/7/29.
*/
public class CacheKey {
/**
* 支持语言列表
*/
public static final String LANGUAGE_KEY = "config.language";
}
package com.neotel.smfcore.core.system.service.dao;
import com.neotel.smfcore.common.base.IBaseDao;
public interface ICacheItemDao extends IBaseDao {
void updateCacheItem(String cacheKey, Object cacheValue);
}
package com.neotel.smfcore.core.system.service.dao.impl;
import com.neotel.smfcore.common.base.AbstractBaseDao;
import com.neotel.smfcore.core.system.service.dao.ICacheItemDao;
import com.neotel.smfcore.core.system.service.dao.ISettingsDao;
import com.neotel.smfcore.core.system.service.po.CacheItem;
import com.neotel.smfcore.core.system.service.po.Settings;
import org.springframework.stereotype.Service;
@Service
public class CacheItemDaoImpl extends AbstractBaseDao implements ICacheItemDao {
@Override
public void updateCacheItem(String cacheKey, Object cacheValue){
CacheItem cacheInfo = getCacheInfo(cacheKey);
if(cacheInfo == null){
cacheInfo = new CacheItem();
cacheInfo.setCacheKey(cacheKey);
}
cacheInfo.setCacheValue(cacheValue);
save(cacheInfo);
}
private CacheItem getCacheInfo(String cacheKey){
return findOneByCondition(new String[]{"cacheKey"}, new String[]{cacheKey});
}
@Override
public Class getEntityClass() {
return CacheItem.class;
}
}
\ No newline at end of file \ No newline at end of file
package com.neotel.smfcore.core.system.service.po;
import com.neotel.smfcore.common.base.BasePo;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
/**
* 缓存信息
*/
@Data
@Document
public class CacheItem extends BasePo implements Serializable {
private String cacheKey = "";
private Object cacheValue;
}
...@@ -53,24 +53,16 @@ public class ConfigurerAdapter implements WebMvcConfigurer { ...@@ -53,24 +53,16 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
@Bean @Bean
public CorsFilter corsFilter() { public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration(); CorsConfiguration config = new CorsConfiguration();
// 是否允许请求带有验证信息 //允许所有域名进行跨域调用
config.addAllowedOriginPattern("*");//替换这个
//允许跨越发送cookie
config.setAllowCredentials(true); config.setAllowCredentials(true);
//放行全部原始头信息
// 允许访问的客户端域名
// (springboot2.4以上的加入这一段可解决 allowedOrigins cannot contain the special value "*"问题)
List<String> allowedOriginPatterns = new ArrayList<>();
allowedOriginPatterns.add("*");
config.setAllowedOriginPatterns(allowedOriginPatterns);
// 设置访问源地址
// config.addAllowedOrigin("*");
// 设置访问源请求头
config.addAllowedHeader("*"); config.addAllowedHeader("*");
// 设置访问源请求方法 //允许所有请求方法跨域调用
config.addAllowedMethod("*"); config.addAllowedMethod("*");
// 对接口配置跨域设置 UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config); source.registerCorsConfiguration("/**", config);
return new CorsFilter(source); return new CorsFilter(source);
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!