| 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);  | 
|     }  | 
|   | 
| }  |