ResBodyAdvice.java 3.12 KB
package vion.advice;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import vion.utils.ResultUtil;
import vion.vo.ResultVO;

/**
 * @author HlQ
 * @date 2023/11/2
 */
@Component
@RestControllerAdvice("vion.controller")
@Slf4j
public class ResBodyAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return !returnType.getMethod().getReturnType().isAssignableFrom(Void.TYPE);
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body instanceof ResultVO || body instanceof ModelAndView) {
            return body;
        }

        ResultVO resultVO = ResultUtil.success(body);
        if (returnType.getParameterType().isAssignableFrom(String.class)) {
            // 字符串类型特殊处理
            try {
                return new ObjectMapper().writeValueAsString(resultVO);
            } catch (JsonProcessingException e) {
            }
        }
        return resultVO;
    }

    @ExceptionHandler(Exception.class)
    public ResultVO exceptionHandler(Exception e) {
        log.error("接口调用出错", e);
        return ResultUtil.error(e.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public ResultVO argExceptionHandler(Exception e) {
        log.error("参数异常", e);
        return ResultUtil.error(e.getMessage());
    }

    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public ResultVO methodArgExceptionHandler(Exception e) {
        log.error("方法参数异常", e);
        return ResultUtil.error("Method parameter error.");
    }

    @ExceptionHandler(NotLoginException.class)
    public ResultVO notLoginExceptionHandler(Exception e) {
        log.error("未登录", e.getMessage());
        return ResultUtil.error(401, "请重新登录");
    }

    @ExceptionHandler(NotPermissionException.class)
    public ResultVO notPermissionExceptionHandler(NotPermissionException e) {
        log.error("无权限", e.getMessage());
        return ResultUtil.error(500, "缺少权限:" + e.getPermission());
    }
}