admin
2019-11-15 534a87e48dfbaff65605995ec2a29211e0a8f45a
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
86
87
88
89
90
91
92
93
94
95
96
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();
        
    }
 
}