1.新建注解@ClickAnn
/**
* 請(qǐng)求明細(xì)接口時(shí)點(diǎn)擊數(shù)+1
*/
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ClickAnn {
int mold() default 0;
}
2.處理邏輯
/**
* @Description: 處理類(lèi)
* @Author sp
* @Date 2021/9/23
* @Version 1.0
*/
@Slf4j
@Aspect
@Component
public class ClickAspect {
@Autowired
private MisHomeService misHomeService;
/**
* 切點(diǎn)
*/
@Pointcut("@annotation(com.common.annotation.ClickAnn)")
public void initAspect() {
}
/**
* 后置通知 用于攔截Controller層的明細(xì)操作
*
* @param joinPoint 切點(diǎn)
*/
@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);
}