From 1d8945ea7ba27eb6bd8bec14579586e1c88b756e Mon Sep 17 00:00:00 2001 From: wzy <wzy19931122ai@163.com> Date: Fri, 16 Oct 2020 00:01:24 +0800 Subject: [PATCH] modify --- src/main/java/com/xcong/excoin/common/aspect/SubmitRepeatAspect.java | 72 ++++++++++++++++++++++++++++++++++++ src/main/java/com/xcong/excoin/common/annotation/SubmitRepeat.java | 13 ++++++ src/main/java/com/xcong/excoin/modules/agent/controller/AgentController.java | 2 + 3 files changed, 87 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/xcong/excoin/common/annotation/SubmitRepeat.java b/src/main/java/com/xcong/excoin/common/annotation/SubmitRepeat.java new file mode 100644 index 0000000..cf122c1 --- /dev/null +++ b/src/main/java/com/xcong/excoin/common/annotation/SubmitRepeat.java @@ -0,0 +1,13 @@ +package com.xcong.excoin.common.annotation; + +import java.lang.annotation.*; + +/** + * + * @author helius + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface SubmitRepeat { +} diff --git a/src/main/java/com/xcong/excoin/common/aspect/SubmitRepeatAspect.java b/src/main/java/com/xcong/excoin/common/aspect/SubmitRepeatAspect.java new file mode 100644 index 0000000..052afb3 --- /dev/null +++ b/src/main/java/com/xcong/excoin/common/aspect/SubmitRepeatAspect.java @@ -0,0 +1,72 @@ +package com.xcong.excoin.common.aspect; + +import com.xcong.excoin.common.annotation.SubmitRepeat; +import com.xcong.excoin.common.entity.FebsResponse; +import com.xcong.excoin.common.exception.FebsException; +import com.xcong.excoin.common.utils.RedisUtils; +import com.xcong.excoin.system.entity.User; +import lombok.extern.slf4j.Slf4j; +import org.apache.shiro.SecurityUtils; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.*; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; + +/** + * @author wzy + * @date 2020-04-16 22:01 + **/ +@Slf4j +@Aspect +@Component +public class SubmitRepeatAspect { + + @Resource + private RedisUtils redisUtil; + + private String key; + + @Pointcut("@annotation(submitRepeat)") + public void submitRepeatPointCut(SubmitRepeat submitRepeat) { + + } + + @Before("submitRepeatPointCut(submitRepeat)") + public void before(SubmitRepeat submitRepeat) { + } + + @Around("submitRepeatPointCut(submitRepeat)") + public Object around(ProceedingJoinPoint joinPoint, SubmitRepeat submitRepeat) throws Throwable { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = attributes.getRequest(); + String uri = request.getRequestURI(); + User user = (User) SecurityUtils.getSubject().getPrincipal(); + key = user.getId() + "_" + uri; + boolean flag = redisUtil.setNotExist(key, "1", 5); + log.info("#mid : {}, flag : {}#", user.getId(), flag); + if (flag) { + Object result = joinPoint.proceed(); + redisUtil.del(key); + return result; + } else { + return new FebsResponse().fail().message("请勿重复点击"); + } + } + + @After("submitRepeatPointCut(submitRepeat)") + public void after(SubmitRepeat submitRepeat) { + + } + + @AfterThrowing(throwing = "ex", pointcut = "submitRepeatPointCut(submitRepeat)") + public void afterThrows(JoinPoint jp, Exception ex, SubmitRepeat submitRepeat) throws Exception { + log.error("#submit repeat error:#", ex); + redisUtil.del(key); + throw new Exception("系统繁忙"); + } +} diff --git a/src/main/java/com/xcong/excoin/modules/agent/controller/AgentController.java b/src/main/java/com/xcong/excoin/modules/agent/controller/AgentController.java index 1ab053b..2357a43 100644 --- a/src/main/java/com/xcong/excoin/modules/agent/controller/AgentController.java +++ b/src/main/java/com/xcong/excoin/modules/agent/controller/AgentController.java @@ -2,6 +2,7 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.xcong.excoin.common.annotation.ControllerEndpoint; +import com.xcong.excoin.common.annotation.SubmitRepeat; import com.xcong.excoin.common.controller.BaseController; import com.xcong.excoin.common.entity.FebsResponse; import com.xcong.excoin.common.entity.QueryRequest; @@ -41,6 +42,7 @@ return new FebsResponse().success().data(map); } + @SubmitRepeat @PostMapping("add") @RequiresPermissions("agent:add") @ControllerEndpoint(operation = "新增代理商", exceptionMessage = "新增代理商失败") -- Gitblit v1.9.1