admin
2024-01-23 81da61b828e29b7745e1382dfbbaeb685dc083ef
fanli/src/main/java/com/yeshi/fanli/aspect/VersionLimitAspect.java
@@ -1,97 +1,97 @@
package com.yeshi.fanli.aspect;
import java.lang.reflect.Method;
import javax.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.yeshi.utils.NumberUtil;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.annotation.integral.IntegralGetVersionLimit;
import com.yeshi.fanli.util.annotation.redpack.RedPackGetVersionLimit;
@Component
@Aspect
public class VersionLimitAspect {
   @Resource
   private UserActiveLogService userActiveLogService;
   private ExpressionParser parser = new SpelExpressionParser();
   private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
   public String generateKeyBySpEL(String spELString, ProceedingJoinPoint joinPoint) {
      MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
      String[] paramNames = nameDiscoverer.getParameterNames(methodSignature.getMethod());
      Expression expression = parser.parseExpression(spELString);
      EvaluationContext context = new StandardEvaluationContext();
      Object[] args = joinPoint.getArgs();
      for (int i = 0; i < args.length; i++) {
         context.setVariable(paramNames[i], args[i]);
      }
      return expression.getValue(context).toString();
   }
   @Around("execution(public * com.yeshi.fanli.service.impl..*.*(..))")
   public Object requestSerializable(ProceedingJoinPoint joinPoint) throws Throwable {
      Signature signature = joinPoint.getSignature();
      MethodSignature methodSignature = (MethodSignature) signature;
      Method targetMethod = methodSignature.getMethod();
      try {
         Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(),
               targetMethod.getParameterTypes());
         if (realMethod.isAnnotationPresent(IntegralGetVersionLimit.class)) {
            IntegralGetVersionLimit rs = realMethod.getAnnotation(IntegralGetVersionLimit.class);
            String key = rs.uid();
            String uid = generateKeyBySpEL(key, joinPoint);
            if (uid != null && NumberUtil.isNumeric(uid)) {
               UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(Long.parseLong(uid));
               if (activeLog != null) {
                  // 小于1.6.5版本不增加积分
                  if (!VersionUtil.greaterThan_1_6_5("appstore".equalsIgnoreCase(activeLog.getChannel()) ? "ios" : "android",
                        activeLog.getVersionCode()))
                     return null;
               }
            }
         }
         if (realMethod.isAnnotationPresent(RedPackGetVersionLimit.class)) {
            RedPackGetVersionLimit rs = realMethod.getAnnotation(RedPackGetVersionLimit.class);
            String key = rs.uid();
            String uid = generateKeyBySpEL(key, joinPoint);
            if (uid != null && NumberUtil.isNumeric(uid)) {
               UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(Long.parseLong(uid));
               if (activeLog != null) {
                  // 小于2.1版本不增加红包
                  if (!VersionUtil.greaterThan_2_1("appstore".equalsIgnoreCase(activeLog.getChannel()) ? "ios" : "android",
                        activeLog.getVersionCode()))
                     return null;
               }
            }
         }
      } catch (NoSuchMethodException e) {
         e.printStackTrace();
      } catch (SecurityException e) {
         e.printStackTrace();
      }
      return joinPoint.proceed();
   }
}
package com.yeshi.fanli.aspect;
import java.lang.reflect.Method;
import javax.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.yeshi.utils.NumberUtil;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.annotation.integral.IntegralGetVersionLimit;
import com.yeshi.fanli.util.annotation.redpack.RedPackGetVersionLimit;
@Component
@Aspect
public class VersionLimitAspect {
   @Resource
   private UserActiveLogService userActiveLogService;
   private ExpressionParser parser = new SpelExpressionParser();
   private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
   public String generateKeyBySpEL(String spELString, ProceedingJoinPoint joinPoint) {
      MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
      String[] paramNames = nameDiscoverer.getParameterNames(methodSignature.getMethod());
      Expression expression = parser.parseExpression(spELString);
      EvaluationContext context = new StandardEvaluationContext();
      Object[] args = joinPoint.getArgs();
      for (int i = 0; i < args.length; i++) {
         context.setVariable(paramNames[i], args[i]);
      }
      return expression.getValue(context).toString();
   }
   @Around("execution(public * com.yeshi.fanli.service.impl..*.*(..))")
   public Object requestSerializable(ProceedingJoinPoint joinPoint) throws Throwable {
      Signature signature = joinPoint.getSignature();
      MethodSignature methodSignature = (MethodSignature) signature;
      Method targetMethod = methodSignature.getMethod();
      try {
         Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(),
               targetMethod.getParameterTypes());
         if (realMethod.isAnnotationPresent(IntegralGetVersionLimit.class)) {
            IntegralGetVersionLimit rs = realMethod.getAnnotation(IntegralGetVersionLimit.class);
            String key = rs.uid();
            String uid = generateKeyBySpEL(key, joinPoint);
            if (uid != null && NumberUtil.isNumeric(uid)) {
               UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(Long.parseLong(uid));
               if (activeLog != null) {
                  // 小于1.6.5版本不增加积分
                  if (!VersionUtil.greaterThan_1_6_5("appstore".equalsIgnoreCase(activeLog.getChannel()) ? "ios" : "android",
                        activeLog.getVersionCode()))
                     return null;
               }
            }
         }
         if (realMethod.isAnnotationPresent(RedPackGetVersionLimit.class)) {
            RedPackGetVersionLimit rs = realMethod.getAnnotation(RedPackGetVersionLimit.class);
            String key = rs.uid();
            String uid = generateKeyBySpEL(key, joinPoint);
            if (uid != null && NumberUtil.isNumeric(uid)) {
               UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(Long.parseLong(uid));
               if (activeLog != null) {
                  // 小于2.1版本不增加红包
                  if (!VersionUtil.greaterThan_2_0_5("appstore".equalsIgnoreCase(activeLog.getChannel()) ? "ios" : "android",
                        activeLog.getVersionCode()))
                     return null;
               }
            }
         }
      } catch (NoSuchMethodException e) {
         e.printStackTrace();
      } catch (SecurityException e) {
         e.printStackTrace();
      }
      return joinPoint.proceed();
   }
}