|
3 | 3 | import cn.hutool.core.util.StrUtil; |
4 | 4 | import io.github.talelin.latticy.common.configuration.CodeMessageConfiguration; |
5 | 5 | import io.github.talelin.latticy.vo.UnifyResponseVO; |
6 | | -import lombok.extern.slf4j.Slf4j; |
7 | 6 | import org.aspectj.lang.annotation.AfterReturning; |
8 | 7 | import org.aspectj.lang.annotation.Aspect; |
9 | | -import org.aspectj.lang.annotation.Pointcut; |
10 | 8 | import org.springframework.stereotype.Component; |
11 | 9 |
|
12 | 10 | /** |
13 | | - * 处理返回结果为 UnifyResponseVO 的视图函数 |
14 | | - * 默认的返回均是英文,在此处通过code替换成中文 |
| 11 | + * 处理返回结果为 UnifyResponseVO 的 Controller |
| 12 | + * message 默认为 null,在此处通过 code 设置为对应消息 |
15 | 13 | * |
16 | 14 | * @author pedro@TaleLin |
17 | 15 | * @author colorful@TaleLin |
| 16 | + * @author Juzi@TaleLin |
18 | 17 | */ |
19 | 18 | @Aspect |
20 | 19 | @Component |
21 | | -@Slf4j |
22 | 20 | public class ResultAspect { |
| 21 | + @AfterReturning(returning = "result", pointcut = "execution(public * io.github.talelin.latticy.controller..*.*(..))") |
| 22 | + public void doAfterReturning(UnifyResponseVO<String> result) { |
| 23 | + int code = result.getCode(); |
| 24 | + String oldMessage = result.getMessage(); |
| 25 | + // code-message.properties 中配置的 message |
| 26 | + String newMessage = CodeMessageConfiguration.getMessage(code); |
23 | 27 |
|
24 | | - |
25 | | - @Pointcut("execution(public * io.github.talelin.latticy.controller..*.*(..))") |
26 | | - public void handlePlaceholder() { |
27 | | - } |
28 | | - |
29 | | - @AfterReturning(returning = "ret", pointcut = "handlePlaceholder()") |
30 | | - public void doAfterReturning(Object ret) throws Throwable { |
31 | | - if (ret instanceof UnifyResponseVO) { |
32 | | - UnifyResponseVO result = (UnifyResponseVO) ret; |
33 | | - int code = result.getCode(); |
34 | | - String message = CodeMessageConfiguration.getMessage(code); |
35 | | - if (StrUtil.isNotBlank(message) && StrUtil.isBlank((CharSequence) result.getMessage())) { |
36 | | - result.setMessage(message); |
37 | | - } |
| 28 | + // 如果 code-message.properties 中指定了相应的 message 并且 UnifyResponseVO 的 message 为null |
| 29 | + // 则使用 newMessage 替换 oldMessage |
| 30 | + if (StrUtil.isNotBlank(newMessage) && StrUtil.isBlank(result.getMessage())) { |
| 31 | + result.setMessage(newMessage); |
38 | 32 | } |
39 | 33 | } |
40 | 34 | } |
0 commit comments