“djh”
2024-12-04 d1549b8445c65a6775cf1ba093f5040ace0f87e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.gkhy.huataiFourierSpecialGasMonitor.aspect;
 
 
import com.gkhy.huataiFourierSpecialGasMonitor.commons.enums.ResultCode;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.exception.BusinessException;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.exception.RepeatedClickException;
import com.gkhy.huataiFourierSpecialGasMonitor.config.authorization.TokenConfig;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.gkhy.huataiFourierSpecialGasMonitor.annotation.RepeatedClick;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
 
@Component
@Aspect
public class RepeatedClickAspect {
 
    @Resource
    protected TokenConfig tokenConfig;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Before("@annotation(com.gkhy.huataiFourierSpecialGasMonitor.annotation.RepeatedClick)")
    @ResponseBody
    public void beforeRepeatedClick(JoinPoint joinPoint){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest arg = requestAttributes.getRequest();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        RepeatedClick annotation = methodSignature.getMethod().getAnnotation(RepeatedClick.class);
        if (annotation != null){
            int clickTime = annotation.clickTime();
            String errorMessage = annotation.errorMessage();
            String userId = arg.getHeader(tokenConfig.getLoginUserHeader());
            if (!StringUtils.isEmpty(userId)) {
                try {
                    Long uid = Long.parseLong(userId);
                    String key = "uid:"+uid+"_"+ arg.getRequestURI() + "_" + arg.getMethod();
                    if (redisTemplate.hasKey(key)){
                        throw new RepeatedClickException(errorMessage);
                    }else {
                        redisTemplate.opsForValue().set(key,"",clickTime, TimeUnit.SECONDS);
                    }
                } catch (NumberFormatException e) {
                    throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(),"数据参数异常");
                }
            }
        }
    }
}