自定义注解拦截

@Slf4j
@Aspect
@Component
public class CheckCorpTypeAspect {

    private QyWechatAuthService qyWechatAuthService;

    @Autowired
    private void setManage(QyWechatAuthService qyWechatAuthService){
        this.qyWechatAuthService = qyWechatAuthService;
    }

    @Pointcut("@annotation(com.edu.archive.common.annotation.CheckCorpType)")
    public void pointcut(){
    }

    @Around(value = "pointcut()")
    public Object checkCorpType(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("#### 进入校验授权企业类型方法 ####");

        // 获取注解参数事件类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        CheckCorpType annotation = signature.getMethod().getAnnotation(CheckCorpType.class);

        // 获取参数
        Object[] args = joinPoint.getArgs();

        HomeSchoolEvent eventParams = (HomeSchoolEvent)args[0];
        log.info("授权企业事件回调参数!事件类型 eventType={}, eventParams={} ", annotation.eventType().name(),JSON.toJSONString(eventParams));

        // 授权企业ID
        String authCorpId = eventParams.getAuthCorpId();

        log.info("事件回调授权企业ID={}", authCorpId);
        if (StringUtils.isBlank(authCorpId)) {
            log.error("回调事件请求参数!corpId is null lock fail");
            throw new BusinessException("回调事件请求参数!corpId is null lock fail");
        }

        if (this.checkCorpType(authCorpId)){
            return joinPoint.proceed();
        }
        return null;
    }

    /**
     * 校验企业类型
     */
    private boolean checkCorpType(String authCorpId){
        // 校验企业类型
        QyWechatAuth qyWechatAuth = qyWechatAuthService.selectByCorpid(authCorpId);
        if (ObjectUtil.isEmpty(qyWechatAuth)){
            log.info("未查询到该企业的授权信息!corpId={}", authCorpId);
            return false;
        }
        if (QyWeChatTypeEnum.NON_EDUCATIONAL.getCode().equals(qyWechatAuth.getType())){
            log.info("非教育类企业事件回调不做处理! corpId={}", authCorpId);
            return false;
        }
        return true;
    }


}

备注:环绕通知=前置+目标方法执行+后置通知,proceed方法就是用于启动目标方法执行的

  

posted @ 2021-09-26 15:46  蓝鲸也是鲸  阅读(69)  评论(0编辑  收藏  举报