From 302620425dac0cb9d6ef4aa53c6696de5dfe57cd Mon Sep 17 00:00:00 2001 From: ronger Date: Wed, 15 Jan 2020 17:35:34 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/service/log/VisitAspect.java | 41 ++++++++++++------- .../java/com/rymcu/vertical/entity/Visit.java | 14 +++++++ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/rymcu/vertical/core/service/log/VisitAspect.java b/src/main/java/com/rymcu/vertical/core/service/log/VisitAspect.java index c3f0986..01bdeff 100644 --- a/src/main/java/com/rymcu/vertical/core/service/log/VisitAspect.java +++ b/src/main/java/com/rymcu/vertical/core/service/log/VisitAspect.java @@ -16,6 +16,8 @@ import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -36,6 +38,8 @@ import java.util.Map; @Component public class VisitAspect { + Logger logger = LoggerFactory.getLogger(VisitAspect.class); + @Resource private ArticleService articleService; @Resource @@ -53,26 +57,12 @@ public class VisitAspect { */ @AfterReturning(value = "pointCut()", returning="obj") public void save(JoinPoint joinPoint, Object obj) { + logger.info("保存访问记录 start ..."); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String ip = Utils.getIpAddress(request); String url = request.getRequestURL().toString(); String ua = request.getHeader("user-agent"); String referer = request.getHeader("Referer"); - String methodName = joinPoint.getSignature().getName(); - Map params = getParams(request); - if (params.isEmpty()) { - params = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); - } else { - params.putAll((Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)); - } - switch (methodName) { - case LoggerConstant.ARTICLE: - Integer id = Integer.parseInt(String.valueOf(params.get("id"))); - articleService.incrementArticleViewCount(id); - break; - default: - break; - } Visit visit = new Visit(); visit.setVisitUrl(url); visit.setVisitIp(ip); @@ -87,6 +77,27 @@ public class VisitAspect { visit.setVisitUserId(tokenUser.getIdUser()); } visitService.save(visit); + + String methodName = joinPoint.getSignature().getName(); + Map params = getParams(request); + if (params.isEmpty()) { + params = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); + } else { + params.putAll((Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)); + } + switch (methodName) { + case LoggerConstant.ARTICLE: + String param = String.valueOf(params.get("id")); + if (StringUtils.isBlank(param) || "undefined".equals(param) || "null".equals(param)) { + break; + } + Integer id = Integer.parseInt(param); + articleService.incrementArticleViewCount(id); + break; + default: + break; + } + logger.info("保存访问记录 end ..."); } private Map getParams(HttpServletRequest request) { diff --git a/src/main/java/com/rymcu/vertical/entity/Visit.java b/src/main/java/com/rymcu/vertical/entity/Visit.java index d62c1ea..6cc1f24 100644 --- a/src/main/java/com/rymcu/vertical/entity/Visit.java +++ b/src/main/java/com/rymcu/vertical/entity/Visit.java @@ -1,7 +1,9 @@ package com.rymcu.vertical.entity; +import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; +import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @@ -19,24 +21,36 @@ public class Visit implements Serializable,Cloneable { /** 主键 */ @Id @GeneratedValue(generator = "JDBC") + @Column(name = "id") private Integer id; /** 浏览链接 */ + @Column(name = "visit_url") private String visitUrl; /** IP */ + @Column(name = "visit_ip") private String visitIp; /** User-Agent */ + @Column(name = "visit_ua") private String visitUa; /** 城市 */ + @Column(name = "visit_city") private String visitCity; /** 设备唯一标识 */ + @Column(name = "visit_device_id") private String visitDeviceId; /** 浏览者 id */ + @Column(name = "visit_user_id") private Integer visitUserId; /** 上游链接 */ + @Column(name = "visit_referer_url") private String visitRefererUrl; /** 创建时间 */ + @Column(name = "created_time") + @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date createdTime; /** 过期时间 */ + @Column(name = "expired_time") + @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date expiredTime; }