mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-20 02:08:15 +08:00
Pre Merge pull request !518 from 秋辞未寒/dev_tranplus
This commit is contained in:
commit
a7d802892f
@ -2,6 +2,7 @@ package org.dromara.common.translation.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import org.dromara.common.translation.core.TranslationInterface;
|
||||
import org.dromara.common.translation.core.handler.TranslationHandler;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
@ -27,9 +28,11 @@ public @interface Translation {
|
||||
String type();
|
||||
|
||||
/**
|
||||
* 映射字段 (如果不为空则取此字段的值)
|
||||
* 映射字段 (如果不为空则取此字段的值,支持多值映射)
|
||||
* <p>
|
||||
* 多值映射模式下 {@link TranslationInterface#translation(Object key, String other)} 方法的第一个参数 key 类型为 {@link cn.hutool.core.lang.Dict}
|
||||
*/
|
||||
String mapper() default "";
|
||||
String[] mapper() default "";
|
||||
|
||||
/**
|
||||
* 其他条件 例如: 字典type(sys_user_sex)
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
package org.dromara.common.translation.core.handler;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.BeanProperty;
|
||||
@ -14,6 +16,7 @@ import org.dromara.common.translation.core.TranslationInterface;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -38,9 +41,21 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
|
||||
TranslationInterface<?> trans = TRANSLATION_MAPPER.get(translation.type());
|
||||
if (ObjectUtil.isNotNull(trans)) {
|
||||
// 如果映射字段不为空 则取映射字段的值
|
||||
if (StringUtils.isNotBlank(translation.mapper())) {
|
||||
value = ReflectUtils.invokeGetter(gen.getCurrentValue(), translation.mapper());
|
||||
List<String> filterMappers = filterMappers();
|
||||
if (CollUtil.isNotEmpty(filterMappers)) {
|
||||
// 适配原有的单值映射
|
||||
if (filterMappers.size() == 1) {
|
||||
value = ReflectUtils.invokeGetter(gen.getCurrentValue(), CollUtil.getFirst(filterMappers));
|
||||
} else {
|
||||
// 多值映射
|
||||
Dict values = Dict.create();
|
||||
for (String mapper : filterMappers) {
|
||||
values.put(mapper, ReflectUtils.invokeGetter(gen.getCurrentValue(), mapper));
|
||||
}
|
||||
value = values;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果为 null 直接写出
|
||||
if (ObjectUtil.isNull(value)) {
|
||||
gen.writeNull();
|
||||
@ -53,6 +68,16 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理映射字段列表
|
||||
*/
|
||||
private List<String> filterMappers() {
|
||||
return CollUtil.newHashSet(translation.mapper())
|
||||
.stream()
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
|
||||
Translation translation = property.getAnnotation(Translation.class);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user