Helius
2021-06-29 5252d1396e21a16774be699a5ba1c8d39c14a22e
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.xzx.gc.util;
 
 
import com.xzx.gc.common.utils.RedisUtil;
import com.xzx.gc.model.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
 
@Aspect
@Component
@Slf4j
public class UserLockAspect {
 
    public static final String USER_LOCK="LOCK_RED_MONEY_";
 
    @Autowired
    RedisUtil redisService;
 
    @Around("@annotation(com.xzx.gc.util.UserLock)")
    public Object around(ProceedingJoinPoint point) throws Throwable{
        MethodSignature signature = (MethodSignature)point.getSignature();
        // 所有值的集合
        Object[] args = point.getArgs();
        Method method = signature.getMethod();
 
        String [] parameters = signature.getParameterNames();
        int indexof = ArrayUtils.indexOf(parameters, "userId");
        String userId = (String) args[indexof];
        if(StringUtils.isEmpty(userId)){
            return JsonResult.failMessage("userId不能为空");
        }
 
        log.info("--------用户id: {} 开始上琐-------", userId);
        if(!redisService.preventManyCommit(USER_LOCK+userId, System.currentTimeMillis()+"", 300L)){
            // throw new PlatformException("当前用户已有红包正在发生,请稍后再试");
            return JsonResult.failMessage("当前用户已有红包正在发生,请稍后再试");
        }
        return point.proceed();
    }
 
    @After("@annotation(com.xzx.gc.util.UserLock)")
    public void after(JoinPoint point) {
    }
 
    @AfterReturning(returning="rvt", pointcut = "@annotation(com.xzx.gc.util.UserLock)")
    public void afterReturning(JoinPoint joinPoint, Object rvt) {
        if(rvt.toString().indexOf("当前用户已有红包正在发生,请稍后再试") != -1){
            return;
        }
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        // 所有值的集合
        Object[] args = joinPoint.getArgs();
 
        String [] parameters = signature.getParameterNames();
        int indexof = ArrayUtils.indexOf(parameters, "userId");
        String userId = (String) args[indexof];
 
        log.info("--------用户id: {} 开始解锁-------", userId);
        redisService.remove(USER_LOCK+userId);
    }
 
    @AfterThrowing(pointcut = "@annotation(com.xzx.gc.util.UserLock)",throwing = "ex")
    public void AfterThrowing(JoinPoint joinPoint,Throwable ex){
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        // 所有值的集合
        Object[] args = joinPoint.getArgs();
 
        String [] parameters = signature.getParameterNames();
        int indexof = ArrayUtils.indexOf(parameters, "userId");
        String userId = (String) args[indexof];
 
        log.info("--------用户id: {} 开始解锁-------", userId);
        redisService.remove(USER_LOCK+userId);
    }
 
}