mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-18 06:13:39 +00:00
v3.0.0 init
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
<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>org.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package org.ruoyi.common.translation.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.common.translation.annotation.TranslationType;
|
||||
import org.ruoyi.common.translation.core.TranslationInterface;
|
||||
import org.ruoyi.common.translation.core.handler.TranslationBeanSerializerModifier;
|
||||
import org.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;
|
||||
|
||||
@@ -43,8 +43,8 @@ public class TranslationConfig {
|
||||
TranslationHandler.TRANSLATION_MAPPER.putAll(map);
|
||||
// 设置 Bean 序列化修改器
|
||||
objectMapper.setSerializerFactory(
|
||||
objectMapper.getSerializerFactory()
|
||||
.withSerializerModifier(new TranslationBeanSerializerModifier()));
|
||||
objectMapper.getSerializerFactory()
|
||||
.withSerializerModifier(new TranslationBeanSerializerModifier()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,11 @@ public interface TransConstant {
|
||||
*/
|
||||
String USER_ID_TO_NAME = "user_id_to_name";
|
||||
|
||||
/**
|
||||
* 用户id转用户名称
|
||||
*/
|
||||
String USER_ID_TO_NICKNAME = "user_id_to_nickname";
|
||||
|
||||
/**
|
||||
* 部门id转名称
|
||||
*/
|
||||
|
||||
@@ -7,11 +7,11 @@ 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 lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.ruoyi.common.core.utils.reflect.ReflectUtils;
|
||||
import org.ruoyi.common.translation.annotation.Translation;
|
||||
import org.ruoyi.common.translation.core.TranslationInterface;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
@@ -39,15 +39,21 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
|
||||
if (ObjectUtil.isNotNull(trans)) {
|
||||
// 如果映射字段不为空 则取映射字段的值
|
||||
if (StringUtils.isNotBlank(translation.mapper())) {
|
||||
value = ReflectUtils.invokeGetter(gen.getCurrentValue(), translation.mapper());
|
||||
value = ReflectUtils.invokeGetter(gen.currentValue(), translation.mapper());
|
||||
}
|
||||
// 如果为 null 直接写出
|
||||
if (ObjectUtil.isNull(value)) {
|
||||
gen.writeNull();
|
||||
return;
|
||||
}
|
||||
Object result = trans.translation(value, translation.other());
|
||||
gen.writeObject(result);
|
||||
try {
|
||||
Object result = trans.translation(value, translation.other());
|
||||
gen.writeObject(result);
|
||||
} catch (Exception e) {
|
||||
log.error("翻译处理异常,type: {}, value: {}", translation.type(), value, e);
|
||||
// 出现异常时输出原始值而不是中断序列化
|
||||
gen.writeObject(value);
|
||||
}
|
||||
} else {
|
||||
gen.writeObject(value);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.ruoyi.common.translation.core.impl;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.ruoyi.common.core.service.DeptService;
|
||||
import org.ruoyi.common.translation.annotation.TranslationType;
|
||||
import org.ruoyi.common.translation.constant.TransConstant;
|
||||
import org.ruoyi.common.translation.core.TranslationInterface;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
/**
|
||||
* 部门翻译实现
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package org.ruoyi.common.translation.core.impl;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.ruoyi.common.core.service.DictService;
|
||||
import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.ruoyi.common.translation.annotation.TranslationType;
|
||||
import org.ruoyi.common.translation.constant.TransConstant;
|
||||
import org.ruoyi.common.translation.core.TranslationInterface;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
/**
|
||||
* 字典翻译实现
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.ruoyi.common.translation.core.impl;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.ruoyi.common.core.service.UserService;
|
||||
import org.ruoyi.common.translation.annotation.TranslationType;
|
||||
import org.ruoyi.common.translation.constant.TransConstant;
|
||||
import org.ruoyi.common.translation.core.TranslationInterface;
|
||||
|
||||
/**
|
||||
* 用户名称翻译实现
|
||||
*
|
||||
* @author may
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@TranslationType(type = TransConstant.USER_ID_TO_NICKNAME)
|
||||
public class NicknameTranslationImpl implements TranslationInterface<String> {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
@Override
|
||||
public String translation(Object key, String other) {
|
||||
if (key instanceof Long id) {
|
||||
return userService.selectNicknameByIds(id.toString());
|
||||
} else if (key instanceof String ids) {
|
||||
return userService.selectNicknameByIds(ids);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.ruoyi.common.translation.core.impl;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.ruoyi.common.core.service.OssService;
|
||||
import org.ruoyi.common.translation.annotation.TranslationType;
|
||||
import org.ruoyi.common.translation.constant.TransConstant;
|
||||
import org.ruoyi.common.translation.core.TranslationInterface;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
/**
|
||||
* OSS翻译实现
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.ruoyi.common.translation.core.impl;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.ruoyi.common.core.service.UserService;
|
||||
import org.ruoyi.common.translation.annotation.TranslationType;
|
||||
import org.ruoyi.common.translation.constant.TransConstant;
|
||||
import org.ruoyi.common.translation.core.TranslationInterface;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
/**
|
||||
* 用户名翻译实现
|
||||
|
||||
@@ -3,3 +3,4 @@ org.ruoyi.common.translation.core.impl.DeptNameTranslationImpl
|
||||
org.ruoyi.common.translation.core.impl.DictTypeTranslationImpl
|
||||
org.ruoyi.common.translation.core.impl.OssUrlTranslationImpl
|
||||
org.ruoyi.common.translation.core.impl.UserNameTranslationImpl
|
||||
org.ruoyi.common.translation.core.impl.NicknameTranslationImpl
|
||||
|
||||
Reference in New Issue
Block a user