SpringBoot

asp处理点击触发

2021-09-23  本文已影响0人  星钻首席小管家

1.新建注解@ClickAnn

/**
 * 请求明细接口时点击数+1
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ClickAnn {

    int mold() default 0;
}

2.处理逻辑

/**
 * @Description: 处理类
 * @Author sp
 * @Date 2021/9/23
 * @Version 1.0
 */
@Slf4j
@Aspect
@Component
public class ClickAspect {

    @Autowired
    private MisHomeService misHomeService;

    /**
     * 切点
     */
    @Pointcut("@annotation(com.common.annotation.ClickAnn)")
    public void initAspect() {
    }

    /**
     * 后置通知 用于拦截Controller层的明细操作
     *
     * @param joinPoint 切点
     */
    @After("initAspect()")
    public void doAfter(JoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        try {
            if (joinPoint != null) {
                Long id = Long.valueOf(request.getParameter("id"));

                Signature signature = joinPoint.getSignature();
                MethodSignature msg = (MethodSignature) signature;
                Object target = joinPoint.getTarget();
                Method method = target.getClass().getMethod(msg.getName(), msg.getParameterTypes());
                ClickAnn annotation = method.getAnnotation(ClickAnn.class);
                int type = annotation.mold();
                if(type>0 && id>0){
                    misHomeService.clickOnResources(type,id);
                }
            }
        } catch (NoSuchMethodException e) {
            log.error("", e);
        }
    }

}

3.在controller层加注解使用

@GetMapping("/getById")
    @ClickAnn(mold = HomePageConstant.HOME_PAGE_BOOK)
    public JsonResult getById(@RequestParam(name = "id") Long id){
        long userId = CurrentUser.getUserId();
        return misBookService.getById(id,userId);
    }
上一篇 下一篇

猜你喜欢

热点阅读