Skip to content

Commit

Permalink
新增访客量统计功能
Browse files Browse the repository at this point in the history
  • Loading branch information
TreasureJade committed Dec 20, 2019
1 parent a1429b0 commit e9910f4
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.swpu.uchain.blog.enums.ResultEnum;
import com.swpu.uchain.blog.form.*;
import com.swpu.uchain.blog.service.ArticleService;
import com.swpu.uchain.blog.service.VisitorService;
import com.swpu.uchain.blog.util.IpUtil;
import com.swpu.uchain.blog.util.RandomUtil;
import com.swpu.uchain.blog.util.ResultVOUtil;
Expand Down Expand Up @@ -35,6 +36,9 @@ public class ArticleController {
@Autowired
private ArticleService articleService;

@Autowired
private VisitorService visitorService;


private static String uploadPath = "/home/hobo/blog/blog-pic/";

Expand Down Expand Up @@ -84,7 +88,8 @@ public Object updateArticle(@Valid UpdateArticleForm form) {

@ApiOperation("获取所有文章")
@GetMapping(name = "获得所有文章", value = "/selectAll")
public Object selectAllArticle(PageForm form) {
public Object selectAllArticle(PageForm form,HttpServletRequest request) {
visitorService.addVisitorNum(request);
return articleService.selectAll(form.getPageNum(), form.getPageSize());
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/swpu/uchain/blog/dao/VisitorMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.swpu.uchain.blog.dao;

import com.swpu.uchain.blog.entity.Visitor;
import java.util.List;

public interface VisitorMapper {

int updateByPrimaryKey(Visitor record);

Visitor selectByPage(String page);
}
51 changes: 51 additions & 0 deletions src/main/java/com/swpu/uchain/blog/entity/Visitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.swpu.uchain.blog.entity;

import java.io.Serializable;

public class Visitor implements Serializable {
private Integer id;

private String page;

private Long totalNum;

private static final long serialVersionUID = 1L;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getPage() {
return page;
}

public void setPage(String page) {
this.page = page == null ? null : page.trim();
}

public Long getTotalNum() {
return totalNum;
}

public void setTotalNum(Long totalNum) {
this.totalNum = totalNum;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", page=").append(page);
sb.append(", totalNum=").append(totalNum);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
9 changes: 1 addition & 8 deletions src/main/java/com/swpu/uchain/blog/redis/RedisService.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@ public <T> T get(KeyPrefix prefix, String key, Class<T> clazz) {
returnToPool(jedis);
}
}
//
// public <T> T get(KeyPrefix prefix, String key, Object object) {
// Jedis jedis = null;
// jedis = jedisPool.getResource();
// String realKey = prefix.getPrefix() + key;
// String str = jedis.get(realKey);
// T t = stringToBean(str, object);
// }


/**
* 设置单个对象
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/swpu/uchain/blog/redis/key/IpKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.swpu.uchain.blog.redis.key;

/**
* @author hobo
* @description
*/
public class IpKey extends BasePrefix{
public IpKey(int expireSeconds, String prefix) {
super(expireSeconds, prefix);
}
public static IpKey ipKey = new IpKey(300,"Ip");
}
17 changes: 17 additions & 0 deletions src/main/java/com/swpu/uchain/blog/service/VisitorService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.swpu.uchain.blog.service;

import javax.servlet.http.HttpServletRequest;

/**
* @author hobo
* @description
*/
public interface VisitorService {

/**
* 增加网站访客量
* @return
*/
void addVisitorNum(HttpServletRequest request);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.swpu.uchain.blog.service.impl;

import com.swpu.uchain.blog.dao.VisitorMapper;
import com.swpu.uchain.blog.entity.Visitor;
import com.swpu.uchain.blog.redis.RedisService;
import com.swpu.uchain.blog.redis.key.IpKey;
import com.swpu.uchain.blog.service.VisitorService;
import com.swpu.uchain.blog.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
* @author hobo
* @description
*/
@Service
public class VisitorServiceImpl implements VisitorService {


@Autowired
private VisitorMapper visitorMapper;

@Autowired
private RedisService redisService;

@Override
public void addVisitorNum(HttpServletRequest request) {
String Ip = IpUtil.getClient(request);
Visitor visitor = redisService.get(IpKey.ipKey, Ip, Visitor.class);
if (visitor == null) {
visitor = visitorMapper.selectByPage("total_page");
visitor.setTotalNum(visitor.getTotalNum() + 1);
if (visitorMapper.updateByPrimaryKey(visitor)==1){
redisService.set(IpKey.ipKey,Ip,visitor);
}

}
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/swpu/uchain/blog/util/IpUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.swpu.uchain.blog.util;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
Expand All @@ -10,6 +11,10 @@
* @description
*/
public class IpUtil {
/**
* 获取本地ip
* @return
*/
public static String getHostIp() {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
Expand All @@ -31,4 +36,29 @@ public static String getHostIp() {
}
return null;
}

/**
* 获取客户端ip
* @param request
* @return
*/
public static String getClient(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
5 changes: 4 additions & 1 deletion src/main/resources/mybatis-generator/generatorConfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@
</javaClientGenerator>

<!-- <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
<table tableName="article" domainObjectName="Article" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
<!-- <table tableName="article" domainObjectName="Article" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
<!-- <table tableName="tags" domainObjectName="Tags" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
<!-- <table tableName="types" domainObjectName="Types" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
<!-- <table tableName="comment" domainObjectName="Comment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
<!-- <table tableName="user_likes" domainObjectName="UserLikes" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
<!-- <table tableName="visitor" domainObjectName="Visitor" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
<table tableName="leave_message" domainObjectName="LeaveMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>

0 comments on commit e9910f4

Please sign in to comment.