forked from rongchao/epmet-cloud-rizhao
				
			
				 2 changed files with 87 additions and 0 deletions
			
			
		@ -0,0 +1,13 @@ | 
				
			|||
package com.epmet.commons.tools.annotation; | 
				
			|||
 | 
				
			|||
import java.lang.annotation.*; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * 标记一个接口,它会被报表服务所调用 | 
				
			|||
 */ | 
				
			|||
@Target(ElementType.METHOD) | 
				
			|||
@Retention(RetentionPolicy.RUNTIME) | 
				
			|||
@Documented | 
				
			|||
public @interface ReportRequest { | 
				
			|||
 | 
				
			|||
} | 
				
			|||
@ -0,0 +1,74 @@ | 
				
			|||
package com.epmet.commons.tools.aspect; | 
				
			|||
 | 
				
			|||
import cn.hutool.core.bean.BeanUtil; | 
				
			|||
import com.epmet.commons.tools.redis.RedisUtils; | 
				
			|||
import org.apache.commons.lang3.StringUtils; | 
				
			|||
import org.aspectj.lang.JoinPoint; | 
				
			|||
import org.aspectj.lang.annotation.Aspect; | 
				
			|||
import org.aspectj.lang.annotation.Before; | 
				
			|||
import org.aspectj.lang.reflect.MethodSignature; | 
				
			|||
import org.springframework.beans.factory.annotation.Autowired; | 
				
			|||
import org.springframework.core.annotation.Order; | 
				
			|||
import org.springframework.stereotype.Component; | 
				
			|||
import org.springframework.web.bind.annotation.RequestBody; | 
				
			|||
import org.springframework.web.context.request.RequestAttributes; | 
				
			|||
import org.springframework.web.context.request.RequestContextHolder; | 
				
			|||
import org.springframework.web.context.request.ServletRequestAttributes; | 
				
			|||
 | 
				
			|||
import java.lang.reflect.Parameter; | 
				
			|||
import java.util.Map; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * 需要被报表服务请求的api,需要加上这个注解 | 
				
			|||
 * 1.该注解会取url中固定的key,去redis获取参数,给入参dto复制 | 
				
			|||
 */ | 
				
			|||
@Aspect | 
				
			|||
@Component | 
				
			|||
@Order(20) | 
				
			|||
public class ReportRequestAspect { | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 从redis中取参数用 | 
				
			|||
     */ | 
				
			|||
    public static final String REPORT_REDIS_KEY = "paramKey"; | 
				
			|||
 | 
				
			|||
    @Autowired | 
				
			|||
    private RedisUtils redisUtils; | 
				
			|||
 | 
				
			|||
    @Before("@annotation(com.epmet.commons.tools.annotation.ReportRequest)") | 
				
			|||
    public void before(JoinPoint point) { | 
				
			|||
        RequestAttributes ra = RequestContextHolder.getRequestAttributes(); | 
				
			|||
        ServletRequestAttributes sra = (ServletRequestAttributes) ra; | 
				
			|||
        String paramKey = sra.getRequest().getParameter(REPORT_REDIS_KEY); | 
				
			|||
        if (StringUtils.isBlank(paramKey)) { | 
				
			|||
            // 没有携带key参数,直接跳过
 | 
				
			|||
            return; | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
        Map<String, Object> cachedParams = redisUtils.hGetAll(paramKey); | 
				
			|||
 | 
				
			|||
        // 使用方法签名获取出参数class列表
 | 
				
			|||
        Object[] args = point.getArgs(); | 
				
			|||
        MethodSignature signature = (MethodSignature) point.getSignature(); | 
				
			|||
        Parameter[] parameters = signature.getMethod().getParameters(); | 
				
			|||
        fillArgsToRequestBody(args, cachedParams, parameters); | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 将redis中取出的参数,赋值到指定的入参dto上 | 
				
			|||
     * @param args | 
				
			|||
     * @param storedParams | 
				
			|||
     */ | 
				
			|||
    private void fillArgsToRequestBody(Object[] args, Map<String, Object> storedParams, Parameter[] parameters) { | 
				
			|||
        for (int i = 0; i < args.length; i++) { | 
				
			|||
            Object arg = args[i]; | 
				
			|||
            Class<?> argClazz = arg.getClass(); | 
				
			|||
            RequestBody requestBodyAnno = parameters[i].getAnnotation(RequestBody.class); | 
				
			|||
            if (arg != null && requestBodyAnno != null) { | 
				
			|||
                Object argBean = BeanUtil.mapToBean(storedParams, arg.getClass(), true); | 
				
			|||
                BeanUtil.copyProperties(argBean, arg); | 
				
			|||
                return; | 
				
			|||
            } | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue