删除ruoyi-common-translation模块

This commit is contained in:
dataprince 2023-12-03 14:37:14 +08:00
parent 41cfdd4f74
commit afd95d2499
15 changed files with 0 additions and 384 deletions

View File

@ -24,7 +24,6 @@
<module>ruoyi-common-security</module> <module>ruoyi-common-security</module>
<module>ruoyi-common-springdoc</module> <module>ruoyi-common-springdoc</module>
<module>ruoyi-common-tenant</module> <module>ruoyi-common-tenant</module>
<module>ruoyi-common-translation</module>
<module>ruoyi-common-web</module> <module>ruoyi-common-web</module>
</modules> </modules>

View File

@ -103,13 +103,6 @@
<version>${revision}</version> <version>${revision}</version>
</dependency> </dependency>
<!-- 翻译模块 -->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-translation</artifactId>
<version>${revision}</version>
</dependency>
<!-- web服务 --> <!-- web服务 -->
<dependency> <dependency>
<groupId>com.ruoyi</groupId> <groupId>com.ruoyi</groupId>

View File

@ -1,27 +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>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ruoyi-common-translation</artifactId>
<description>
ruoyi-common-translation 通用翻译功能
</description>
<dependencies>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-json</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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对应)
* <p>
* 默认取当前字段的值 如果设置了 @{@link Translation#mapper()} 则取映射字段的值
*/
String type();
/**
* 映射字段 (如果不为空则取此字段的值)
*/
String mapper() default "";
/**
* 其他条件 例如: 字典type(sys_user_sex)
*/
String other() default "";
}

View File

@ -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();
}

View File

@ -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<TranslationInterface<?>> list;
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void init() {
Map<String, TranslationInterface<?>> 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()));
}
}

View File

@ -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";
}

View File

@ -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<T> {
/**
* 翻译
*
* @param key 需要被翻译的键(不为空)
* @param other 其他参数
* @return 返回键对应的值
*/
T translation(Object key, String other);
}

View File

@ -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<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
List<BeanPropertyWriter> beanProperties) {
for (BeanPropertyWriter writer : beanProperties) {
// 如果序列化器为 TranslationHandler 的话 Null 值也交给他处理
if (writer.getSerializer() instanceof TranslationHandler serializer) {
writer.assignNullSerializer(serializer);
}
}
return beanProperties;
}
}

View File

@ -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<Object> implements ContextualSerializer {
/**
* 全局翻译实现类映射器
*/
public static final Map<String, TranslationInterface<?>> 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);
}
}

View File

@ -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<String> {
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;
}
}

View File

@ -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<String> {
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;
}
}

View File

@ -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<String> {
private final UserService userService;
@Override
public String translation(Object key, String other) {
if (key instanceof Long id) {
return userService.selectUserNameById(id);
}
return null;
}
}

View File

@ -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

View File

@ -75,11 +75,6 @@
<artifactId>ruoyi-common-tenant</artifactId> <artifactId>ruoyi-common-tenant</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-translation</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.ruoyi</groupId> <groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-web</artifactId> <artifactId>ruoyi-common-web</artifactId>