ResBodyAdvice.java 3.28 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.dromara.hutool.core.util.ObjUtil;
import org.springframework.core.MethodParameter;
import org.springframework.core.io.Resource;
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 (ObjUtil.isNull(body) || body instanceof ResultVO || body instanceof ModelAndView || body instanceof Resource) {
            return body;
        }

        var resultVO = ResultUtil.success(body);
        if (returnType.getParameterType().isAssignableFrom(String.class)) {
            // 字符串类型特殊处理
            try {
                response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
                return new ObjectMapper().writeValueAsString(resultVO);
            } catch (JsonProcessingException e) {
                log.error("Json 序列化失败");
            }
        }
        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("方法参数异常");
    }

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

    @ExceptionHandler(NotPermissionException.class)
    public ResultVO notPermissionExceptionHandler(NotPermissionException e) {
        return ResultUtil.error(512, "您没有该功能使用权限!");
    }
}