update 优化 转换模块代码 删除无用的 common-dict 依赖
This commit is contained in:
parent
a5835aa0c2
commit
bff9720b6f
@ -13,7 +13,6 @@
|
||||
<modules>
|
||||
<module>ruoyi-common-bom</module>
|
||||
<module>ruoyi-common-core</module>
|
||||
<module>ruoyi-common-dict</module>
|
||||
<module>ruoyi-common-doc</module>
|
||||
<module>ruoyi-common-excel</module>
|
||||
<module>ruoyi-common-idempotent</module>
|
||||
|
@ -26,13 +26,6 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 字典 -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-dict</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 接口模块 -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
|
@ -1,26 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-common-dict</artifactId>
|
||||
|
||||
<description>
|
||||
ruoyi-common-dict 字典
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,27 +0,0 @@
|
||||
package com.ruoyi.common.dict.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.ruoyi.common.dict.jackson.DictDataJsonSerializer;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 字典数据映射注解
|
||||
*
|
||||
* @author itino
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||
@JacksonAnnotationsInside
|
||||
@JsonSerialize(using = DictDataJsonSerializer.class)
|
||||
public @interface DictDataMapper {
|
||||
|
||||
/**
|
||||
* 设置字典的type值 (如: sys_user_sex)
|
||||
*/
|
||||
String dictType() default "";
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
package com.ruoyi.common.dict.jackson;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.BeanProperty;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
|
||||
import com.ruoyi.common.core.service.DictService;
|
||||
import com.ruoyi.common.core.utils.SpringUtils;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.common.dict.annotation.DictDataMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 字典数据json序列化工具
|
||||
*
|
||||
* @author itino
|
||||
*/
|
||||
@Slf4j
|
||||
public class DictDataJsonSerializer extends JsonSerializer<String> implements ContextualSerializer {
|
||||
|
||||
private String dictType;
|
||||
|
||||
@Override
|
||||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
try {
|
||||
DictService dictService = SpringUtils.getBean(DictService.class);
|
||||
if (ObjectUtil.isNotNull(dictService)) {
|
||||
String label = dictService.getDictLabel(dictType, value);
|
||||
gen.writeString(StringUtils.isNotBlank(label) ? label : value);
|
||||
} else {
|
||||
gen.writeString(value);
|
||||
}
|
||||
} catch (BeansException e) {
|
||||
log.error("字典数据未查到, 采用默认处理 => {}", e.getMessage());
|
||||
gen.writeString(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
|
||||
DictDataMapper anno = property.getAnnotation(DictDataMapper.class);
|
||||
if (Objects.nonNull(anno) && StrUtil.isNotBlank(anno.dictType())) {
|
||||
this.dictType = anno.dictType();
|
||||
return this;
|
||||
}
|
||||
return prov.findValueSerializer(property.getType(), property);
|
||||
}
|
||||
}
|
@ -25,8 +25,8 @@ public @interface Translation {
|
||||
String type();
|
||||
|
||||
/**
|
||||
* 通用Key 如果为空则取被标注的字段值 例如: 字典type(sys_user_sex)
|
||||
* 其他条件 例如: 字典type(sys_user_sex)
|
||||
*/
|
||||
String key() default "";
|
||||
String other() default "";
|
||||
|
||||
}
|
||||
|
@ -8,15 +8,18 @@ package com.ruoyi.common.translation.constant;
|
||||
public interface TransConstant {
|
||||
|
||||
/**
|
||||
* 用户名翻译
|
||||
* 用户id转账号
|
||||
*/
|
||||
String USER_ID_TO_NAME = "userIdToName";
|
||||
|
||||
/**
|
||||
* 字典值翻译
|
||||
* 字典type转label
|
||||
*/
|
||||
String DICT_TYPE_TO_LABEL = "dictTypeToLabel";
|
||||
|
||||
/**
|
||||
* ossId转url
|
||||
*/
|
||||
String OSS_ID_TO_URL = "ossIdToUrl";
|
||||
|
||||
}
|
||||
|
@ -13,5 +13,5 @@ public interface TranslationInterface {
|
||||
* @param key 需要被翻译的键
|
||||
* @return 返回键对应的值
|
||||
*/
|
||||
String translation(Object key);
|
||||
String translation(Object key, String other);
|
||||
}
|
||||
|
@ -38,13 +38,13 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
|
||||
try {
|
||||
TranslationInterface trans = TRANSLATION_MAPPER.get(translation.type());
|
||||
if (ObjectUtil.isNotNull(trans)) {
|
||||
String result = trans.translation(StringUtils.isBlank(translation.key()) ? value : translation.key());
|
||||
String result = trans.translation(value, translation.other());
|
||||
gen.writeString(StringUtils.isNotBlank(result) ? result : value.toString());
|
||||
} else {
|
||||
gen.writeString(value.toString());
|
||||
}
|
||||
} catch (BeansException e) {
|
||||
log.error("字典数据未查到, 采用默认处理 => {}", e.getMessage());
|
||||
log.error("数据未查到, 采用默认处理 => {}", e.getMessage());
|
||||
gen.writeString(value.toString());
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
package com.ruoyi.common.translation.core.impl;
|
||||
|
||||
import com.ruoyi.common.translation.annotation.TranslationType;
|
||||
import com.ruoyi.common.translation.constant.TransConstant;
|
||||
import com.ruoyi.common.translation.core.TranslationInterface;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 翻译接口 (实现类需标注 {@link com.ruoyi.common.translation.annotation.TranslationType} 注解标明翻译类型)
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Component
|
||||
@TranslationType(type = TransConstant.DICT_TYPE_TO_LABEL)
|
||||
public class DictTranslationImpl implements TranslationInterface {
|
||||
|
||||
/**
|
||||
* 翻译
|
||||
*
|
||||
* @param key 需要被翻译的键
|
||||
* @return 返回键对应的值
|
||||
*/
|
||||
public String translation(Object key) {
|
||||
if (key instanceof String dictType) {
|
||||
return "dict";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.ruoyi.common.translation.core.impl;
|
||||
|
||||
import com.ruoyi.common.core.service.DictService;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.common.translation.annotation.TranslationType;
|
||||
import com.ruoyi.common.translation.constant.TransConstant;
|
||||
import com.ruoyi.common.translation.core.TranslationInterface;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 字典翻译实现
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
@TranslationType(type = TransConstant.DICT_TYPE_TO_LABEL)
|
||||
public class DictTypeTranslationImpl implements TranslationInterface {
|
||||
|
||||
private final DictService dictService;
|
||||
|
||||
public String translation(Object key, String other) {
|
||||
if (key instanceof String dictValue && StringUtils.isNotBlank(other)) {
|
||||
return dictService.getDictLabel(other, dictValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ import com.ruoyi.common.translation.core.TranslationInterface;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 翻译接口 (实现类需标注 {@link com.ruoyi.common.translation.annotation.TranslationType} 注解标明翻译类型)
|
||||
* 用户名翻译实现
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@ -14,13 +14,8 @@ import org.springframework.stereotype.Component;
|
||||
@TranslationType(type = TransConstant.USER_ID_TO_NAME)
|
||||
public class UserNameTranslationImpl implements TranslationInterface {
|
||||
|
||||
/**
|
||||
* 翻译
|
||||
*
|
||||
* @param key 需要被翻译的键
|
||||
* @return 返回键对应的值
|
||||
*/
|
||||
public String translation(Object key) {
|
||||
public String translation(Object key, String other) {
|
||||
// todo 待实现
|
||||
if (key instanceof Long id) {
|
||||
return "admin";
|
||||
}
|
||||
|
@ -35,7 +35,7 @@
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-dict</artifactId>
|
||||
<artifactId>ruoyi-common-translation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- OSS功能模块 -->
|
||||
|
Loading…
x
Reference in New Issue
Block a user