From e6764831a890de26fcfebb4fb8f3a22e9f8cc816 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 27 八月 2019 16:09:42 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/div' into div --- fanli/src/main/java/com/yeshi/fanli/aspect/ActiveUserAspect.java | 137 ++++++++++++++++++++++++--------------------- 1 files changed, 72 insertions(+), 65 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/aspect/ActiveUserAspect.java b/fanli/src/main/java/com/yeshi/fanli/aspect/ActiveUserAspect.java index 71ecd98..660e2f9 100644 --- a/fanli/src/main/java/com/yeshi/fanli/aspect/ActiveUserAspect.java +++ b/fanli/src/main/java/com/yeshi/fanli/aspect/ActiveUserAspect.java @@ -1,20 +1,27 @@ package com.yeshi.fanli.aspect; +import java.lang.reflect.Method; + import javax.annotation.Resource; -import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.springframework.core.annotation.Order; -import org.springframework.core.task.TaskExecutor; -import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; +import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.Before; +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.yeshi.utils.NumberUtil; -import com.yeshi.fanli.service.inter.user.UserSystemCouponService; -import com.yeshi.fanli.util.Constant; -import com.yeshi.fanli.util.RedisManager; +import com.yeshi.fanli.util.StringUtil; +import com.yeshi.fanli.util.ThreadUtil; +import com.yeshi.fanli.util.annotation.UserActive; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; /** * 娲昏穬鐢ㄦ埛澶勭悊 @@ -22,69 +29,69 @@ * @author Administrator * */ -@Component -@Aspect -@Order(6) +// @Component +// @Aspect +// @Order(6) public class ActiveUserAspect { - - @Resource(name = "taskExecutor") - private TaskExecutor executor; @Resource - private RedisManager redisManager; + private JedisPool jedisPool; - @Resource - private UserSystemCouponService userSystemCouponService; + private ExpressionParser parser = new SpelExpressionParser(); - - public static final String EDP = "execution(* com.yeshi.fanli.controller.client.*.*(..))"; + private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); - @Around(EDP) - public Object activeAround(ProceedingJoinPoint joinPoint) throws Throwable { - - Long uid = null; - - ServletRequestAttributes servletContainer = (ServletRequestAttributes) RequestContextHolder - .getRequestAttributes(); - HttpServletRequest request = servletContainer.getRequest(); - - String str_uid = request.getParameter("uid"); - if (str_uid != null && str_uid.trim().length() > 0) { - uid = Long.parseLong(str_uid); + 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]); } - - - final Long uuid = uid; - - executor.execute(new Runnable() { - @Override - public void run() { - - if (uuid == null) { - return; - } - - if (Constant.IS_OUTNET) { - String key = "activeUid_" + uuid; - // 缂撳瓨涓槸鍚﹀瓨鍦╱id - String cacheValue = redisManager.getCommonString(key); - if (cacheValue != null && cacheValue.trim().length() > 0) { - return; - } - // 鍔犲叆缂撳瓨 20鍒嗛挓 - redisManager.cacheCommonString(key, uuid.toString(), 60 * 20); - } - - // 鎺ユ敹鍒� - try { - userSystemCouponService.receivedCoupon(uuid); - } catch (Exception e) { - e.printStackTrace(); + return expression.getValue(context).toString(); + } + + public static final String EDP = "execution(* com.yeshi.fanli.controller.client.*.*.*(..))"; + + @Before(EDP) + public void activeBefore(ProceedingJoinPoint joinPoint) { + try { + Signature signature = joinPoint.getSignature(); + MethodSignature methodSignature = (MethodSignature) signature; + Method targetMethod = methodSignature.getMethod(); + Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(), + targetMethod.getParameterTypes()); + if (realMethod.isAnnotationPresent(UserActive.class)) { + UserActive rs = realMethod.getAnnotation(UserActive.class); + String uidStr = generateKeyBySpEL(rs.uid(), joinPoint); + if (!StringUtil.isNullOrEmpty(uidStr) && NumberUtil.isNumeric(uidStr)) { + ThreadUtil.run(new Runnable() { + @Override + public void run() { + String key = "useractive-" + uidStr; + Jedis jedis = jedisPool.getResource(); + try { + + if (jedis.setnx(key, "1") > 0) { + jedis.expire(key, 60);// 60s鍐呬笉澶勭悊 + try { + // TODO 鐢ㄦ埛娲昏穬澶勭悊 + + } catch (Exception e) { + } + } + } finally { + jedisPool.returnResource(jedis); + } + } + }); } } - }); - - return joinPoint.proceed(joinPoint.getArgs()); + } catch (Exception e) { + + } } } -- Gitblit v1.8.0