1. 枚举字典支持国际化

This commit is contained in:
黄昕 2024-03-24 11:25:36 +08:00
parent 76df3543e1
commit e654fc3fe5
8 changed files with 116 additions and 55 deletions

View File

@ -1,5 +1,7 @@
package com.cf.cs.base.common;
import com.cf.cs.base.misc.ApiContextUtils;
import com.cf.cs.base.misc.MessageUtil;
import com.houkunlin.system.dict.starter.DictEnum;
import com.houkunlin.system.dict.starter.json.DictType;
@ -35,6 +37,15 @@ public enum CommonStatus implements BaseEnum, DictEnum<Integer> {
this.readme = readme;
}
/**
* Gets string value.
*
* @return the string value
*/
public String getStringValue() {
return this.name();
}
@Override
public Integer getValue() {
return this.code;
@ -47,6 +58,6 @@ public enum CommonStatus implements BaseEnum, DictEnum<Integer> {
@Override
public String getDescription() {
return this.readme;
return MessageUtil.get(getStringValue(), ApiContextUtils.getLanguare());
}
}

View File

@ -1,5 +1,7 @@
package com.cf.cs.base.common;
import com.cf.cs.base.misc.ApiContextUtils;
import com.cf.cs.base.misc.MessageUtil;
import com.houkunlin.system.dict.starter.DictEnum;
import com.houkunlin.system.dict.starter.json.DictType;
@ -61,6 +63,6 @@ public enum ProtoCryptoType implements BaseEnum, DictEnum<Integer> {
@Override
public String getDescription() {
return this.readme;
return MessageUtil.get(getStringValue(), ApiContextUtils.getLanguare());
}
}

View File

@ -23,7 +23,7 @@ public class LocaleConfig {
Locale.setDefault(Locale.CHINA);
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
//设置国际化文件存储路径和名称 i18n目录messages文件名
source.setBasenames("i18n/message", "i18n/errorMessage");
source.setBasenames("i18n/message", "i18n/errorMessage", "i18n/enumMessage");
//设置根据key如果没有获取到对应的文本信息,则返回key作为信息
source.setUseCodeAsDefaultMessage(true);
//设置字符编码

View File

@ -0,0 +1,9 @@
NORMAL=Normal
LOCKED=Locked
DISABLED=Disabled
DELETED=Deleted
CRYPTO_NONE=Unencrypted
CRYPTO_BASE64=Base64 encoding
CRYPTO_AES128=AES128 encryption
CRYPTO_DES=DES symmetric encryption
CRYPTO_AES256=AES256 encryption

View File

@ -0,0 +1,9 @@
NORMAL=\u6B63\u5E38
LOCKED=\u5DF2\u9501\u5B9A
DISABLED=\u5DF2\u7981\u7528
DELETED=\u5DF2\u5220\u9664
CRYPTO_NONE=\u4E0D\u52A0\u5BC6
CRYPTO_BASE64=Base64\u7F16\u7801
CRYPTO_AES128=AES128\u52A0\u5BC6
CRYPTO_DES=DES\u5BF9\u79F0\u52A0\u5BC6
CRYPTO_AES256=AES256\u52A0\u5BC6

View File

@ -12,7 +12,7 @@ ERR_PERMISSION=Operator permission is insufficient
ERR_REQTIMEOUT=Request timeout
ERR_PARAMS=Parameter error
ERR_SYSTEMEXCEPTION=System exception
ERR_UNKNOWNCMD=unknown command
ERR_UNKNOWNCMD=Unknown command
ERR_LOGOUT=User not logged in
ERR_TOKENTIMEOUT=Token timeout
ERR_TOKENNOTFOUND=Illegal Token
@ -49,6 +49,6 @@ ERR_JSON_ENCODE=Json Sequence number error
ERR_JSON_DECODE=Json deserialization error
ERR_ENCRYPT_AES256=AES256 Encryption failure
ERR_DECRYPT_AES256=AES256 decryption failure
ERR_CRYPTO_KEY=wrong secret key
ERR_CRYPTO_KEY=Wrong secret key
ERR_USER_ROLE_NOTEXISTS=The user role does not exist
ERR_RESOURCE_USED=Resource used

View File

@ -1,5 +1,6 @@
package com.cf.cs.protocol.pojo.po;
import com.cf.cs.base.common.ErrorCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor;
@ -28,4 +29,10 @@ public class DictContent extends BaseRespStatus {
private String dictAlias;
private String dictRemark;
private List<DictDetails> children;
public DictContent(String dictName, ErrorCode err) {
this.dictName = dictName;
this.setStatus(err.getCode());
this.setMessage(new String[] {err.getDescription()});
}
}

View File

@ -1,14 +1,17 @@
package com.cf.cs.restful.service.impl;
import com.cf.cs.base.common.CommonEnumHandler;
import com.cf.cs.base.common.CommonStatus;
import com.cf.cs.base.common.ErrorCode;
import com.cf.cs.base.common.ProtoCryptoType;
import com.cf.cs.base.exception.CommonErrorCodeException;
import com.cf.cs.base.misc.ApiContextUtils;
import com.cf.cs.base.misc.HelperUtils;
import com.cf.cs.base.pojo.po.PageResults;
import com.cf.cs.database.mapper.DictDataMapper;
import com.cf.cs.database.mapper.DictTypeMapper;
import com.cf.cs.database.pojo.entity.DictType;
import com.cf.cs.database.pojo.entity.DictData;
import com.cf.cs.database.pojo.entity.DictType;
import com.cf.cs.database.pojo.po.UserDictionary;
import com.cf.cs.protocol.pojo.po.DictContent;
import com.cf.cs.protocol.pojo.po.DictDetails;
@ -16,6 +19,7 @@ import com.cf.cs.restful.service.DictionaryService;
import com.houkunlin.system.dict.starter.DictUtil;
import com.houkunlin.system.dict.starter.SystemDictStarter;
import com.houkunlin.system.dict.starter.bean.DictTypeVo;
import com.houkunlin.system.dict.starter.bean.DictValueVo;
import com.houkunlin.system.dict.starter.notice.RefreshDictTypeEvent;
import com.houkunlin.system.dict.starter.notice.RefreshDictValueEvent;
import com.houkunlin.system.dict.starter.provider.SystemDictProvider;
@ -65,24 +69,21 @@ public class DictionaryServiceImpl implements DictionaryService {
SystemDictStarter.getBean(SystemDictProvider.class).dictTypeIterator().forEachRemaining(k -> {
if (dictType.contains(k.getType())) {
List<DictDetails> children = new ArrayList<>();
k.getChildren().forEach(m -> children.add(DictDetails.builder()
.title(m.getTitle())
.value((Integer) m.getValue())
.disabled(m.isDisabled())
.build()));
sysDictList.add(DictContent.builder()
.dictName(k.getType())
.children(children)
.build());
k.getChildren()
.forEach(m -> children.add(DictDetails.builder()
.title(getLocalEnumMessage(m))
.value((Integer) m.getValue())
.disabled(m.isDisabled())
.build()));
sysDictList.add(DictContent.builder().dictName(k.getType()).children(children).build());
}
allSysDict.add(k.getType());
});
dictType.removeAll(allSysDict);
dictType.forEach(k -> sysDictList.add(DictContent.builder()
.dictName(k)
.build()));
dictType.stream().filter(k -> !allSysDict.contains(k)).forEach(m -> {
DictContent dict = new DictContent(m, ErrorCode.ERR_NOSUCHITEM) ;
sysDictList.add(dict);
});
return sysDictList;
}
@ -117,8 +118,7 @@ public class DictionaryServiceImpl implements DictionaryService {
// 找不到对应的字典供修改
if (dt == null) {
throw new CommonErrorCodeException(ErrorCode.ERR_NOSUCHITEM,
type + ": " + ErrorCode.ERR_NOSUCHITEM.getDescription());
throw new CommonErrorCodeException(ErrorCode.ERR_NOSUCHITEM, type + ": " + ErrorCode.ERR_NOSUCHITEM.getDescription());
}
if (name != null) {
@ -154,9 +154,7 @@ public class DictionaryServiceImpl implements DictionaryService {
totalSize = -1L;
}
QueryWrapper wrapper = QueryWrapper.create()
.from(DICT_TYPE)
.select();
QueryWrapper wrapper = QueryWrapper.create().from(DICT_TYPE).select();
Page<UserDictionary> dict = dictTypeMapper.paginateAs(pageNumber, pageSize, totalSize, wrapper, UserDictionary.class);
@ -173,30 +171,28 @@ public class DictionaryServiceImpl implements DictionaryService {
public List<DictContent> getUserDictionaryContent(List<String> dictName) {
List<DictContent> resp = new ArrayList<>();
QueryWrapper wrapper = QueryWrapper.create()
.from(DICT_TYPE)
.select()
.where(DICT_TYPE.DICT_TYPE_NAME.in(dictName));
QueryWrapper wrapper = QueryWrapper.create().from(DICT_TYPE).select().where(DICT_TYPE.DICT_TYPE_NAME.in(dictName));
List<DictType> ret = dictTypeMapper.selectListWithRelationsByQuery(wrapper);
ret.forEach(k -> {
List<DictDetails> children = new ArrayList<>();
k.getDictDataList().forEach(m -> children.add(DictDetails.builder()
.id(m.getId())
.title(m.getDictLabel())
.value(m.getDictValue())
.sorted(m.getDictSort())
.cssClass(HelperUtils.meagreDbStringValue(m.getCssClass()))
.listClass(HelperUtils.meagreDbStringValue(m.getListClass()))
.isDefault("Y".equals(m.getIsDefault()))
.createBy(m.getCreateBy())
.createTime(m.getCreateTime())
.upgradeBy(HelperUtils.meagreDbStringValue(m.getUpdateBy()))
.upgradeTime(m.getUpdateTime())
.disabled(false)
.build()));
k.getDictDataList()
.forEach(m -> children.add(DictDetails.builder()
.id(m.getId())
.title(m.getDictLabel())
.value(m.getDictValue())
.sorted(m.getDictSort())
.cssClass(HelperUtils.meagreDbStringValue(m.getCssClass()))
.listClass(HelperUtils.meagreDbStringValue(m.getListClass()))
.isDefault("Y".equals(m.getIsDefault()))
.createBy(m.getCreateBy())
.createTime(m.getCreateTime())
.upgradeBy(HelperUtils.meagreDbStringValue(m.getUpdateBy()))
.upgradeTime(m.getUpdateTime())
.disabled(false)
.build()));
resp.add(DictContent.builder()
.dictId(k.getId())
@ -207,16 +203,12 @@ public class DictionaryServiceImpl implements DictionaryService {
.build());
});
dictName.stream()
.filter(k -> ret.stream().noneMatch(v -> v.getDictTypeName().equals(k)))
.forEach(m -> {
DictContent dt = DictContent.builder()
.dictName(m)
.build();
dt.setStatus(ErrorCode.ERR_NOSUCHITEM.getCode());
dt.setMessage(new String[] {ErrorCode.ERR_NOSUCHITEM.getDescription()});
resp.add(dt);
});
dictName.stream().filter(k -> ret.stream().noneMatch(v -> v.getDictTypeName().equals(k))).forEach(m -> {
DictContent dt = DictContent.builder().dictName(m).build();
dt.setStatus(ErrorCode.ERR_NOSUCHITEM.getCode());
dt.setMessage(new String[] {ErrorCode.ERR_NOSUCHITEM.getDescription()});
resp.add(dt);
});
return resp;
}
@ -227,8 +219,7 @@ public class DictionaryServiceImpl implements DictionaryService {
DictType dt = dictTypeMapper.selectOneWithRelationsByCondition(DICT_TYPE.DICT_TYPE_NAME.eq(dictName));
if (dt == null) {
throw new CommonErrorCodeException(ErrorCode.ERR_NOSUCHITEM,
dictName + ": " + ErrorCode.ERR_NOSUCHITEM.getDescription());
throw new CommonErrorCodeException(ErrorCode.ERR_NOSUCHITEM, dictName + ": " + ErrorCode.ERR_NOSUCHITEM.getDescription());
}
List<Long> contentId = dt.getDictDataList().stream().map(DictData::getId).toList();
@ -248,4 +239,36 @@ public class DictionaryServiceImpl implements DictionaryService {
dv.getChildren().forEach(v -> v.setDisabled(true));
publisher.publishEvent(new RefreshDictValueEvent(dv.getChildren(), true));
}
private String getLocalEnumMessage(DictValueVo m) {
int value = (Integer) m.getValue();
switch (m.getDictType()) {
case "ErrorCode":
ErrorCode err = CommonEnumHandler.codeOf(ErrorCode.class, value);
if (err != null) {
return err.getDescription();
}
break;
case "CommonStatus":
CommonStatus cs = CommonEnumHandler.codeOf(CommonStatus.class, value);
if (cs != null) {
return cs.getDescription();
}
break;
case "ProtoCryptoType":
ProtoCryptoType pt = CommonEnumHandler.codeOf(ProtoCryptoType.class, value);
if (pt != null) {
return pt.getDescription();
}
break;
default:
return m.getTitle();
}
return m.getTitle();
}
}