From afd95d2499b8a35df7d873e4f8f3bc8f371d0a41 Mon Sep 17 00:00:00 2001 From: dataprince Date: Sun, 3 Dec 2023 14:37:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4ruoyi-common-translation?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-common/pom.xml | 1 - ruoyi-common/ruoyi-common-bom/pom.xml | 7 -- ruoyi-common/ruoyi-common-translation/pom.xml | 27 -------- .../translation/annotation/Translation.java | 39 ----------- .../annotation/TranslationType.java | 23 ------- .../translation/config/TranslationConfig.java | 50 -------------- .../translation/constant/TransConstant.java | 30 --------- .../core/TranslationInterface.java | 20 ------ .../TranslationBeanSerializerModifier.java | 29 --------- .../core/handler/TranslationHandler.java | 65 ------------------- .../core/impl/DeptNameTranslationImpl.java | 29 --------- .../core/impl/DictTypeTranslationImpl.java | 28 -------- .../core/impl/UserNameTranslationImpl.java | 27 -------- ...ot.autoconfigure.AutoConfiguration.imports | 4 -- ruoyi-modules/ruoyi-system/pom.xml | 5 -- 15 files changed, 384 deletions(-) delete mode 100644 ruoyi-common/ruoyi-common-translation/pom.xml delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/annotation/Translation.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/annotation/TranslationType.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/config/TranslationConfig.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/constant/TransConstant.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/TranslationInterface.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/handler/TranslationBeanSerializerModifier.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/handler/TranslationHandler.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DeptNameTranslationImpl.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DictTypeTranslationImpl.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/UserNameTranslationImpl.java delete mode 100644 ruoyi-common/ruoyi-common-translation/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 14b1c43..983137a 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -24,7 +24,6 @@ ruoyi-common-security ruoyi-common-springdoc ruoyi-common-tenant - ruoyi-common-translation ruoyi-common-web diff --git a/ruoyi-common/ruoyi-common-bom/pom.xml b/ruoyi-common/ruoyi-common-bom/pom.xml index 7e31829..210876f 100644 --- a/ruoyi-common/ruoyi-common-bom/pom.xml +++ b/ruoyi-common/ruoyi-common-bom/pom.xml @@ -103,13 +103,6 @@ ${revision} - - - com.ruoyi - ruoyi-common-translation - ${revision} - - com.ruoyi diff --git a/ruoyi-common/ruoyi-common-translation/pom.xml b/ruoyi-common/ruoyi-common-translation/pom.xml deleted file mode 100644 index 51199d5..0000000 --- a/ruoyi-common/ruoyi-common-translation/pom.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - com.ruoyi - ruoyi-common - ${revision} - - 4.0.0 - - ruoyi-common-translation - - - ruoyi-common-translation 通用翻译功能 - - - - - - com.ruoyi - ruoyi-common-json - - - - - diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/annotation/Translation.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/annotation/Translation.java deleted file mode 100644 index 1a74001..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/annotation/Translation.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.ruoyi.common.translation.annotation; - -import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.ruoyi.common.translation.core.handler.TranslationHandler; - -import java.lang.annotation.*; - -/** - * 通用翻译注解 - * - * @author Lion Li - */ -@Inherited -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.FIELD, ElementType.METHOD}) -@Documented -@JacksonAnnotationsInside -@JsonSerialize(using = TranslationHandler.class) -public @interface Translation { - - /** - * 类型 (需与实现类上的 {@link TranslationType} 注解type对应) - *

- * 默认取当前字段的值 如果设置了 @{@link Translation#mapper()} 则取映射字段的值 - */ - String type(); - - /** - * 映射字段 (如果不为空则取此字段的值) - */ - String mapper() default ""; - - /** - * 其他条件 例如: 字典type(sys_user_sex) - */ - String other() default ""; - -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/annotation/TranslationType.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/annotation/TranslationType.java deleted file mode 100644 index e90ee38..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/annotation/TranslationType.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.ruoyi.common.translation.annotation; - -import com.ruoyi.common.translation.core.TranslationInterface; - -import java.lang.annotation.*; - -/** - * 翻译类型注解 (标注到{@link TranslationInterface} 的实现类) - * - * @author Lion Li - */ -@Inherited -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) -@Documented -public @interface TranslationType { - - /** - * 类型 - */ - String type(); - -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/config/TranslationConfig.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/config/TranslationConfig.java deleted file mode 100644 index a9aefe5..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/config/TranslationConfig.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.ruoyi.common.translation.config; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.ruoyi.common.translation.annotation.TranslationType; -import com.ruoyi.common.translation.core.TranslationInterface; -import com.ruoyi.common.translation.core.handler.TranslationBeanSerializerModifier; -import com.ruoyi.common.translation.core.handler.TranslationHandler; -import jakarta.annotation.PostConstruct; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.AutoConfiguration; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 翻译模块配置类 - * - * @author Lion Li - */ -@Slf4j -@AutoConfiguration -public class TranslationConfig { - - @Autowired - private List> list; - - @Autowired - private ObjectMapper objectMapper; - - @PostConstruct - public void init() { - Map> map = new HashMap<>(list.size()); - for (TranslationInterface trans : list) { - if (trans.getClass().isAnnotationPresent(TranslationType.class)) { - TranslationType annotation = trans.getClass().getAnnotation(TranslationType.class); - map.put(annotation.type(), trans); - } else { - log.warn(trans.getClass().getName() + " 翻译实现类未标注 TranslationType 注解!"); - } - } - TranslationHandler.TRANSLATION_MAPPER.putAll(map); - // 设置 Bean 序列化修改器 - objectMapper.setSerializerFactory( - objectMapper.getSerializerFactory() - .withSerializerModifier(new TranslationBeanSerializerModifier())); - } - -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/constant/TransConstant.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/constant/TransConstant.java deleted file mode 100644 index 1cfec58..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/constant/TransConstant.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.ruoyi.common.translation.constant; - -/** - * 翻译常量 - * - * @author Lion Li - */ -public interface TransConstant { - - /** - * 用户id转账号 - */ - String USER_ID_TO_NAME = "user_id_to_name"; - - /** - * 部门id转名称 - */ - String DEPT_ID_TO_NAME = "dept_id_to_name"; - - /** - * 字典type转label - */ - String DICT_TYPE_TO_LABEL = "dict_type_to_label"; - - /** - * ossId转url - */ - String OSS_ID_TO_URL = "oss_id_to_url"; - -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/TranslationInterface.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/TranslationInterface.java deleted file mode 100644 index d5c9ec7..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/TranslationInterface.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.ruoyi.common.translation.core; - -import com.ruoyi.common.translation.annotation.TranslationType; - -/** - * 翻译接口 (实现类需标注 {@link TranslationType} 注解标明翻译类型) - * - * @author Lion Li - */ -public interface TranslationInterface { - - /** - * 翻译 - * - * @param key 需要被翻译的键(不为空) - * @param other 其他参数 - * @return 返回键对应的值 - */ - T translation(Object key, String other); -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/handler/TranslationBeanSerializerModifier.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/handler/TranslationBeanSerializerModifier.java deleted file mode 100644 index fb3f6f7..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/handler/TranslationBeanSerializerModifier.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.ruoyi.common.translation.core.handler; - -import com.fasterxml.jackson.databind.BeanDescription; -import com.fasterxml.jackson.databind.SerializationConfig; -import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; -import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; - -import java.util.List; - -/** - * Bean 序列化修改器 解决 Null 被单独处理问题 - * - * @author Lion Li - */ -public class TranslationBeanSerializerModifier extends BeanSerializerModifier { - - @Override - public List changeProperties(SerializationConfig config, BeanDescription beanDesc, - List beanProperties) { - for (BeanPropertyWriter writer : beanProperties) { - // 如果序列化器为 TranslationHandler 的话 将 Null 值也交给他处理 - if (writer.getSerializer() instanceof TranslationHandler serializer) { - writer.assignNullSerializer(serializer); - } - } - return beanProperties; - } - -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/handler/TranslationHandler.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/handler/TranslationHandler.java deleted file mode 100644 index ac10ee5..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/handler/TranslationHandler.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.ruoyi.common.translation.core.handler; - -import cn.hutool.core.util.ObjectUtil; -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.translation.annotation.Translation; -import com.ruoyi.common.translation.core.TranslationInterface; -import com.ruoyi.common.core.utils.StringUtils; -import com.ruoyi.common.core.utils.reflect.ReflectUtils; -import lombok.extern.slf4j.Slf4j; - -import java.io.IOException; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; - -/** - * 翻译处理器 - * - * @author Lion Li - */ -@Slf4j -public class TranslationHandler extends JsonSerializer implements ContextualSerializer { - - /** - * 全局翻译实现类映射器 - */ - public static final Map> TRANSLATION_MAPPER = new ConcurrentHashMap<>(); - - private Translation translation; - - @Override - public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { - TranslationInterface trans = TRANSLATION_MAPPER.get(translation.type()); - if (ObjectUtil.isNotNull(trans)) { - // 如果映射字段不为空 则取映射字段的值 - if (StringUtils.isNotBlank(translation.mapper())) { - value = ReflectUtils.invokeGetter(gen.getCurrentValue(), translation.mapper()); - } - // 如果为 null 直接写出 - if (ObjectUtil.isNull(value)) { - gen.writeNull(); - return; - } - Object result = trans.translation(value, translation.other()); - gen.writeObject(result); - } else { - gen.writeObject(value); - } - } - - @Override - public JsonSerializer createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException { - Translation translation = property.getAnnotation(Translation.class); - if (Objects.nonNull(translation)) { - this.translation = translation; - return this; - } - return prov.findValueSerializer(property.getType(), property); - } -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DeptNameTranslationImpl.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DeptNameTranslationImpl.java deleted file mode 100644 index 0acf8b6..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DeptNameTranslationImpl.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.ruoyi.common.translation.core.impl; - -import com.ruoyi.common.translation.annotation.TranslationType; -import com.ruoyi.common.core.service.DeptService; -import com.ruoyi.common.translation.constant.TransConstant; -import com.ruoyi.common.translation.core.TranslationInterface; -import lombok.AllArgsConstructor; - -/** - * 部门翻译实现 - * - * @author Lion Li - */ -@AllArgsConstructor -@TranslationType(type = TransConstant.DEPT_ID_TO_NAME) -public class DeptNameTranslationImpl implements TranslationInterface { - - private final DeptService deptService; - - @Override - public String translation(Object key, String other) { - if (key instanceof String ids) { - return deptService.selectDeptNameByIds(ids); - } else if (key instanceof Long id) { - return deptService.selectDeptNameByIds(id.toString()); - } - return null; - } -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DictTypeTranslationImpl.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DictTypeTranslationImpl.java deleted file mode 100644 index 5e54441..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DictTypeTranslationImpl.java +++ /dev/null @@ -1,28 +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 com.ruoyi.common.core.service.DictService; -import com.ruoyi.common.core.utils.StringUtils; -import lombok.AllArgsConstructor; - -/** - * 字典翻译实现 - * - * @author Lion Li - */ -@AllArgsConstructor -@TranslationType(type = TransConstant.DICT_TYPE_TO_LABEL) -public class DictTypeTranslationImpl implements TranslationInterface { - - private final DictService dictService; - - @Override - public String translation(Object key, String other) { - if (key instanceof String dictValue && StringUtils.isNotBlank(other)) { - return dictService.getDictLabel(other, dictValue); - } - return null; - } -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/UserNameTranslationImpl.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/UserNameTranslationImpl.java deleted file mode 100644 index 1ae09c6..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/UserNameTranslationImpl.java +++ /dev/null @@ -1,27 +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 com.ruoyi.common.core.service.UserService; -import lombok.AllArgsConstructor; - -/** - * 用户名翻译实现 - * - * @author Lion Li - */ -@AllArgsConstructor -@TranslationType(type = TransConstant.USER_ID_TO_NAME) -public class UserNameTranslationImpl implements TranslationInterface { - - private final UserService userService; - - @Override - public String translation(Object key, String other) { - if (key instanceof Long id) { - return userService.selectUserNameById(id); - } - return null; - } -} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/ruoyi-common/ruoyi-common-translation/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports deleted file mode 100644 index 0f983f7..0000000 --- a/ruoyi-common/ruoyi-common-translation/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ /dev/null @@ -1,4 +0,0 @@ -com.ruoyi.common.translation.config.TranslationConfig -com.ruoyi.common.translation.core.impl.DeptNameTranslationImpl -com.ruoyi.common.translation.core.impl.DictTypeTranslationImpl -com.ruoyi.common.translation.core.impl.UserNameTranslationImpl diff --git a/ruoyi-modules/ruoyi-system/pom.xml b/ruoyi-modules/ruoyi-system/pom.xml index 7883954..9b6ddc9 100644 --- a/ruoyi-modules/ruoyi-system/pom.xml +++ b/ruoyi-modules/ruoyi-system/pom.xml @@ -75,11 +75,6 @@ ruoyi-common-tenant - - com.ruoyi - ruoyi-common-translation - - com.ruoyi ruoyi-common-web