diff --git a/src/main/java/com/swpu/uchain/blog/controller/ArticleController.java b/src/main/java/com/swpu/uchain/blog/controller/ArticleController.java index 9765bf1..63692a2 100644 --- a/src/main/java/com/swpu/uchain/blog/controller/ArticleController.java +++ b/src/main/java/com/swpu/uchain/blog/controller/ArticleController.java @@ -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; @@ -35,6 +36,9 @@ public class ArticleController { @Autowired private ArticleService articleService; + @Autowired + private VisitorService visitorService; + private static String uploadPath = "/home/hobo/blog/blog-pic/"; @@ -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()); } diff --git a/src/main/java/com/swpu/uchain/blog/dao/VisitorMapper.java b/src/main/java/com/swpu/uchain/blog/dao/VisitorMapper.java new file mode 100644 index 0000000..07d9983 --- /dev/null +++ b/src/main/java/com/swpu/uchain/blog/dao/VisitorMapper.java @@ -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); +} \ No newline at end of file diff --git a/src/main/java/com/swpu/uchain/blog/entity/Visitor.java b/src/main/java/com/swpu/uchain/blog/entity/Visitor.java new file mode 100644 index 0000000..66f2965 --- /dev/null +++ b/src/main/java/com/swpu/uchain/blog/entity/Visitor.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/main/java/com/swpu/uchain/blog/redis/RedisService.java b/src/main/java/com/swpu/uchain/blog/redis/RedisService.java index 1a8561d..0078141 100644 --- a/src/main/java/com/swpu/uchain/blog/redis/RedisService.java +++ b/src/main/java/com/swpu/uchain/blog/redis/RedisService.java @@ -57,14 +57,7 @@ public T get(KeyPrefix prefix, String key, Class clazz) { returnToPool(jedis); } } -// -// public 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); -// } + /** * 设置单个对象 diff --git a/src/main/java/com/swpu/uchain/blog/redis/key/IpKey.java b/src/main/java/com/swpu/uchain/blog/redis/key/IpKey.java new file mode 100644 index 0000000..2024a53 --- /dev/null +++ b/src/main/java/com/swpu/uchain/blog/redis/key/IpKey.java @@ -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"); +} diff --git a/src/main/java/com/swpu/uchain/blog/service/VisitorService.java b/src/main/java/com/swpu/uchain/blog/service/VisitorService.java new file mode 100644 index 0000000..5998eff --- /dev/null +++ b/src/main/java/com/swpu/uchain/blog/service/VisitorService.java @@ -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); + +} diff --git a/src/main/java/com/swpu/uchain/blog/service/impl/VisitorServiceImpl.java b/src/main/java/com/swpu/uchain/blog/service/impl/VisitorServiceImpl.java new file mode 100644 index 0000000..379eee7 --- /dev/null +++ b/src/main/java/com/swpu/uchain/blog/service/impl/VisitorServiceImpl.java @@ -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); + } + + } + } +} diff --git a/src/main/java/com/swpu/uchain/blog/util/IpUtil.java b/src/main/java/com/swpu/uchain/blog/util/IpUtil.java index 82ce17d..798281f 100644 --- a/src/main/java/com/swpu/uchain/blog/util/IpUtil.java +++ b/src/main/java/com/swpu/uchain/blog/util/IpUtil.java @@ -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; @@ -10,6 +11,10 @@ * @description */ public class IpUtil { + /** + * 获取本地ip + * @return + */ public static String getHostIp() { try { Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces(); @@ -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; + } } diff --git a/src/main/resources/mybatis-generator/generatorConfig.xml b/src/main/resources/mybatis-generator/generatorConfig.xml index 35631ce..0ade2e1 100644 --- a/src/main/resources/mybatis-generator/generatorConfig.xml +++ b/src/main/resources/mybatis-generator/generatorConfig.xml @@ -59,9 +59,12 @@ - + + + +
\ No newline at end of file