package com.ibeetl.admin.console.util; import com.ibeetl.admin.console.service.RedisService; import com.ibeetl.admin.core.util.PlatformException; import com.ibeetl.admin.core.web.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; import java.lang.reflect.Parameter; @Aspect @Component @Slf4j public class UserLockAspect { public static final String USER_LOCK="LOCK_RED_MONEY_"; @Autowired RedisService redisService; @Around("@annotation(com.ibeetl.admin.console.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.ibeetl.admin.console.util.UserLock)") public void after(JoinPoint point) { } @AfterReturning(returning="rvt", pointcut = "@annotation(com.ibeetl.admin.console.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(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); } }