♻️ 代码结构优化
This commit is contained in:
parent
a29959fbd1
commit
302620425d
@ -16,6 +16,8 @@ import org.aspectj.lang.annotation.AfterReturning;
|
|||||||
import org.aspectj.lang.annotation.AfterThrowing;
|
import org.aspectj.lang.annotation.AfterThrowing;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Pointcut;
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
@ -36,6 +38,8 @@ import java.util.Map;
|
|||||||
@Component
|
@Component
|
||||||
public class VisitAspect {
|
public class VisitAspect {
|
||||||
|
|
||||||
|
Logger logger = LoggerFactory.getLogger(VisitAspect.class);
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ArticleService articleService;
|
private ArticleService articleService;
|
||||||
@Resource
|
@Resource
|
||||||
@ -53,26 +57,12 @@ public class VisitAspect {
|
|||||||
*/
|
*/
|
||||||
@AfterReturning(value = "pointCut()", returning="obj")
|
@AfterReturning(value = "pointCut()", returning="obj")
|
||||||
public void save(JoinPoint joinPoint, Object obj) {
|
public void save(JoinPoint joinPoint, Object obj) {
|
||||||
|
logger.info("保存访问记录 start ...");
|
||||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||||
String ip = Utils.getIpAddress(request);
|
String ip = Utils.getIpAddress(request);
|
||||||
String url = request.getRequestURL().toString();
|
String url = request.getRequestURL().toString();
|
||||||
String ua = request.getHeader("user-agent");
|
String ua = request.getHeader("user-agent");
|
||||||
String referer = request.getHeader("Referer");
|
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 visit = new Visit();
|
||||||
visit.setVisitUrl(url);
|
visit.setVisitUrl(url);
|
||||||
visit.setVisitIp(ip);
|
visit.setVisitIp(ip);
|
||||||
@ -87,6 +77,27 @@ public class VisitAspect {
|
|||||||
visit.setVisitUserId(tokenUser.getIdUser());
|
visit.setVisitUserId(tokenUser.getIdUser());
|
||||||
}
|
}
|
||||||
visitService.save(visit);
|
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<String, String> getParams(HttpServletRequest request) {
|
private Map<String, String> getParams(HttpServletRequest request) {
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package com.rymcu.vertical.entity;
|
package com.rymcu.vertical.entity;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
@ -19,24 +21,36 @@ public class Visit implements Serializable,Cloneable {
|
|||||||
/** 主键 */
|
/** 主键 */
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(generator = "JDBC")
|
@GeneratedValue(generator = "JDBC")
|
||||||
|
@Column(name = "id")
|
||||||
private Integer id;
|
private Integer id;
|
||||||
/** 浏览链接 */
|
/** 浏览链接 */
|
||||||
|
@Column(name = "visit_url")
|
||||||
private String visitUrl;
|
private String visitUrl;
|
||||||
/** IP */
|
/** IP */
|
||||||
|
@Column(name = "visit_ip")
|
||||||
private String visitIp;
|
private String visitIp;
|
||||||
/** User-Agent */
|
/** User-Agent */
|
||||||
|
@Column(name = "visit_ua")
|
||||||
private String visitUa;
|
private String visitUa;
|
||||||
/** 城市 */
|
/** 城市 */
|
||||||
|
@Column(name = "visit_city")
|
||||||
private String visitCity;
|
private String visitCity;
|
||||||
/** 设备唯一标识 */
|
/** 设备唯一标识 */
|
||||||
|
@Column(name = "visit_device_id")
|
||||||
private String visitDeviceId;
|
private String visitDeviceId;
|
||||||
/** 浏览者 id */
|
/** 浏览者 id */
|
||||||
|
@Column(name = "visit_user_id")
|
||||||
private Integer visitUserId;
|
private Integer visitUserId;
|
||||||
/** 上游链接 */
|
/** 上游链接 */
|
||||||
|
@Column(name = "visit_referer_url")
|
||||||
private String visitRefererUrl;
|
private String visitRefererUrl;
|
||||||
/** 创建时间 */
|
/** 创建时间 */
|
||||||
|
@Column(name = "created_time")
|
||||||
|
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
|
||||||
private Date createdTime;
|
private Date createdTime;
|
||||||
/** 过期时间 */
|
/** 过期时间 */
|
||||||
|
@Column(name = "expired_time")
|
||||||
|
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
|
||||||
private Date expiredTime;
|
private Date expiredTime;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user