Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b40522e93 | ||
|
|
9026ba0732 | ||
|
|
f5f2437fa4 | ||
|
|
0bcca091e1 | ||
|
|
3c69baa4ad | ||
|
|
baf5b1bbb7 | ||
|
|
38621bcfa4 | ||
|
|
cf435a588f | ||
|
|
feab26f4f7 |
@@ -8,7 +8,11 @@
|
|||||||
|
|
||||||
[](https://github.com/xiaohai2271/blog-backEnd)
|
[](https://github.com/xiaohai2271/blog-backEnd)
|
||||||
[](https://github.com/xiaohai2271/blog-backEnd)
|
[](https://github.com/xiaohai2271/blog-backEnd)
|
||||||
|
[](https://github.com/xiaohai2271/blog-backEnd)
|
||||||
[](https://github.com/xiaohai2271/blog-backEnd)
|
[](https://github.com/xiaohai2271/blog-backEnd)
|
||||||
|
[](https://github.com/xiaohai2271/blog-backEnd)
|
||||||
|
[](https://github.com/xiaohai2271/blog-backEnd)
|
||||||
|
[](https://github.com/xiaohai2271/blog-backEnd)
|
||||||
[](https://www.celess.cn)
|
[](https://www.celess.cn)
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
28
pom.xml
28
pom.xml
@@ -175,6 +175,34 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-api</artifactId>
|
||||||
|
<version>2.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-impl</artifactId>
|
||||||
|
<version>2.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-core</artifactId>
|
||||||
|
<version>2.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.activation</groupId>
|
||||||
|
<artifactId>activation</artifactId>
|
||||||
|
<version>1.1.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.lionsoul</groupId>
|
||||||
|
<artifactId>ip2region</artifactId>
|
||||||
|
<version>1.7.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public class VisitorController {
|
|||||||
@GetMapping("/admin/visitor/page")
|
@GetMapping("/admin/visitor/page")
|
||||||
public Response page(@RequestParam(value = "count", required = false, defaultValue = "10") int count,
|
public Response page(@RequestParam(value = "count", required = false, defaultValue = "10") int count,
|
||||||
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
|
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
|
||||||
@RequestParam(value = "showLocation", required = false, defaultValue = "false") boolean showLocation) {
|
@RequestParam(value = "showLocation", required = false, defaultValue = "true") boolean showLocation) {
|
||||||
return Response.success(visitorService.visitorPage(page, count, showLocation));
|
return Response.success(visitorService.visitorPage(page, count, showLocation));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -304,16 +304,12 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
public PageData<ArticleModel> retrievePageForOpen(int count, int page) {
|
public PageData<ArticleModel> retrievePageForOpen(int count, int page) {
|
||||||
PageHelper.startPage(page, count);
|
PageHelper.startPage(page, count);
|
||||||
List<Article> articleList = articleMapper.findAllByOpen(true);
|
List<Article> articleList = articleMapper.findAllByOpen(true);
|
||||||
PageData<ArticleModel> pageData = new PageData<>(new PageInfo<Article>(articleList));
|
PageData<ArticleModel> pageData = new PageData<>(new PageInfo<>(articleList));
|
||||||
|
|
||||||
List<ArticleModel> articleModelList = new ArrayList<>();
|
|
||||||
|
|
||||||
articleList.forEach(article -> {
|
|
||||||
ArticleModel model = ModalTrans.article(article, true);
|
|
||||||
setPreAndNextArticle(model);
|
|
||||||
articleModelList.add(model);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
List<ArticleModel> articleModelList = articleList
|
||||||
|
.stream()
|
||||||
|
.map(article -> setPreAndNextArticle(ModalTrans.article(article, true)))
|
||||||
|
.collect(Collectors.toList());
|
||||||
pageData.setList(articleModelList);
|
pageData.setList(articleModelList);
|
||||||
return pageData;
|
return pageData;
|
||||||
}
|
}
|
||||||
@@ -327,17 +323,14 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
PageHelper.startPage(page, count);
|
PageHelper.startPage(page, count);
|
||||||
List<Article> open = articleMapper.findAllByCategoryIdAndOpen(category.getId());
|
List<Article> open = articleMapper.findAllByCategoryIdAndOpen(category.getId());
|
||||||
|
|
||||||
List<ArticleModel> modelList = new ArrayList<>();
|
List<ArticleModel> modelList = open.stream()
|
||||||
|
.map(article -> ModalTrans.article(article, true))
|
||||||
open.forEach(article -> {
|
.peek(articleModel -> {
|
||||||
ArticleModel model = ModalTrans.article(article, true);
|
articleModel.setNextArticle(null);
|
||||||
model.setTags(null);
|
articleModel.setPreArticle(null);
|
||||||
// setPreAndNextArticle(model);
|
})
|
||||||
model.setNextArticle(null);
|
.collect(Collectors.toList());
|
||||||
model.setPreArticle(null);
|
return new PageData<>(new PageInfo<>(open), modelList);
|
||||||
modelList.add(model);
|
|
||||||
});
|
|
||||||
return new PageData<ArticleModel>(new PageInfo<Article>(open), modelList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -348,21 +341,22 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
}
|
}
|
||||||
PageHelper.startPage(page, count);
|
PageHelper.startPage(page, count);
|
||||||
List<ArticleTag> articleByTag = articleTagMapper.findArticleByTagAndOpen(tag.getId());
|
List<ArticleTag> articleByTag = articleTagMapper.findArticleByTagAndOpen(tag.getId());
|
||||||
List<ArticleModel> modelList = new ArrayList<>();
|
List<ArticleModel> modelList = articleByTag
|
||||||
articleByTag.forEach(articleTag -> {
|
.stream()
|
||||||
ArticleModel model = ModalTrans.article(articleTag.getArticle(), true);
|
.map(articleTag -> ModalTrans.article(articleTag.getArticle(), true))
|
||||||
model.setNextArticle(null);
|
.peek(articleModel -> {
|
||||||
model.setPreArticle(null);
|
articleModel.setNextArticle(null);
|
||||||
modelList.add(model);
|
articleModel.setPreArticle(null);
|
||||||
});
|
}).collect(Collectors.toList());
|
||||||
return new PageData<ArticleModel>(new PageInfo<ArticleTag>(articleByTag), modelList);
|
return new PageData<>(new PageInfo<>(articleByTag), modelList);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setPreAndNextArticle(ArticleModel articleModel) {
|
private ArticleModel setPreAndNextArticle(ArticleModel articleModel) {
|
||||||
if (articleModel == null) {
|
if (articleModel == null) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
articleModel.setPreArticle(ModalTrans.article(articleMapper.getPreArticle(articleModel.getId()), true));
|
articleModel.setPreArticle(ModalTrans.article(articleMapper.getPreArticle(articleModel.getId()), true));
|
||||||
articleModel.setNextArticle(ModalTrans.article(articleMapper.getNextArticle(articleModel.getId()), true));
|
articleModel.setNextArticle(ModalTrans.article(articleMapper.getNextArticle(articleModel.getId()), true));
|
||||||
|
return articleModel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : xiaohai
|
* @author : xiaohai
|
||||||
@@ -68,22 +68,25 @@ public class CategoryServiceImpl implements CategoryService {
|
|||||||
public PageData<CategoryModel> retrievePage(int page, int count) {
|
public PageData<CategoryModel> retrievePage(int page, int count) {
|
||||||
PageHelper.startPage(page, count);
|
PageHelper.startPage(page, count);
|
||||||
List<Category> all = categoryMapper.findAll();
|
List<Category> all = categoryMapper.findAll();
|
||||||
List<CategoryModel> modelList = new ArrayList<>();
|
// 遍历没一个category
|
||||||
all.forEach(e -> {
|
List<CategoryModel> modelList = all
|
||||||
CategoryModel model = ModalTrans.category(e);
|
.stream()
|
||||||
List<Article> allByCategoryId = articleMapper.findAllByCategoryId(e.getId());
|
.map(ModalTrans::category)
|
||||||
List<ArticleModel> articleModelList = new ArrayList<>();
|
.peek(categoryModel -> {
|
||||||
allByCategoryId.forEach(article -> {
|
// 根据category去查article,并赋值给categoryModel
|
||||||
ArticleModel articleModel = ModalTrans.article(article, true);
|
List<Article> allByCategoryId = articleMapper.findAllByCategoryId(categoryModel.getId());
|
||||||
articleModel.setPreArticle(null);
|
List<ArticleModel> articleModelList = allByCategoryId
|
||||||
articleModel.setNextArticle(null);
|
.stream()
|
||||||
articleModel.setTags(null);
|
.map(article -> ModalTrans.article(article, true))
|
||||||
articleModelList.add(articleModel);
|
.peek(articleModel -> {
|
||||||
});
|
// 去除不必要的字段
|
||||||
model.setArticles(articleModelList);
|
articleModel.setPreArticle(null);
|
||||||
modelList.add(model);
|
articleModel.setNextArticle(null);
|
||||||
});
|
articleModel.setTags(null);
|
||||||
|
})
|
||||||
return new PageData<CategoryModel>(new PageInfo<Category>(all), modelList);
|
.collect(Collectors.toList());
|
||||||
|
categoryModel.setArticles(articleModelList);
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
return new PageData<>(new PageInfo<>(all), modelList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : xiaohai
|
* @author : xiaohai
|
||||||
@@ -107,14 +108,11 @@ public class CommentServiceImpl implements CommentService {
|
|||||||
@Override
|
@Override
|
||||||
public List<CommentModel> retrievePageByPid(long pid) {
|
public List<CommentModel> retrievePageByPid(long pid) {
|
||||||
List<Comment> allByPagePath = commentMapper.findAllByPid(pid);
|
List<Comment> allByPagePath = commentMapper.findAllByPid(pid);
|
||||||
List<CommentModel> commentModels = new ArrayList<>();
|
return allByPagePath
|
||||||
allByPagePath.forEach(comment -> {
|
.stream()
|
||||||
if (comment.getStatus() != CommentStatusEnum.DELETED.getCode()) {
|
.filter(comment -> comment.getStatus() != CommentStatusEnum.DELETED.getCode())
|
||||||
commentModels.add(ModalTrans.comment(comment));
|
.map(ModalTrans::comment)
|
||||||
}
|
.collect(Collectors.toList());
|
||||||
});
|
|
||||||
|
|
||||||
return commentModels;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -145,14 +143,11 @@ public class CommentServiceImpl implements CommentService {
|
|||||||
|
|
||||||
private PageData<CommentModel> pageTrans(List<Comment> commentList, boolean noResponseList) {
|
private PageData<CommentModel> pageTrans(List<Comment> commentList, boolean noResponseList) {
|
||||||
PageInfo<Comment> p = PageInfo.of(commentList);
|
PageInfo<Comment> p = PageInfo.of(commentList);
|
||||||
List<CommentModel> modelList = new ArrayList<>();
|
List<CommentModel> modelList = commentList
|
||||||
commentList.forEach(l -> {
|
.stream()
|
||||||
CommentModel model = ModalTrans.comment(l);
|
.map(ModalTrans::comment)
|
||||||
if (!noResponseList) {
|
.peek(commentModel -> commentModel.setRespComment(this.retrievePageByPid(commentModel.getId())))
|
||||||
model.setRespComment(this.retrievePageByPid(model.getId()));
|
.collect(Collectors.toList());
|
||||||
}
|
return new PageData<>(p, modelList);
|
||||||
modelList.add(model);
|
|
||||||
});
|
|
||||||
return new PageData<CommentModel>(p, modelList);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : xiaohai
|
* @author : xiaohai
|
||||||
@@ -81,20 +82,22 @@ public class TagServiceImpl implements TagService {
|
|||||||
List<Tag> tagList = tagMapper.findAll();
|
List<Tag> tagList = tagMapper.findAll();
|
||||||
List<TagModel> modelList = new ArrayList<>();
|
List<TagModel> modelList = new ArrayList<>();
|
||||||
tagList.forEach(tag -> modelList.add(ModalTrans.tag(tag)));
|
tagList.forEach(tag -> modelList.add(ModalTrans.tag(tag)));
|
||||||
return new PageData<TagModel>(new PageInfo<Tag>(tagList), modelList);
|
return new PageData<>(new PageInfo<>(tagList), modelList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<TagModel> findAll() {
|
public List<TagModel> findAll() {
|
||||||
List<TagModel> list = new ArrayList<>();
|
return tagMapper.findAll().stream()
|
||||||
tagMapper.findAll().forEach(e -> {
|
.map(ModalTrans::tag)
|
||||||
TagModel model = ModalTrans.tag(e);
|
.peek(tagModel -> {
|
||||||
List<ArticleTag> articleByTagAndOpen = articleTagMapper.findArticleByTagAndOpen(e.getId());
|
List<ArticleTag> articleByTagAndOpen = articleTagMapper.findArticleByTagAndOpen(tagModel.getId());
|
||||||
List<ArticleModel> articleModelList = new ArrayList<>();
|
tagModel.setArticles(
|
||||||
articleByTagAndOpen.forEach(articleTag -> articleModelList.add(ModalTrans.article(articleTag.getArticle(), true)));
|
articleByTagAndOpen
|
||||||
model.setArticles(articleModelList);
|
.stream()
|
||||||
list.add(model);
|
.map(articleTag -> ModalTrans.article(articleTag.getArticle(), true))
|
||||||
});
|
.collect(Collectors.toList())
|
||||||
return list;
|
);
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import cn.celess.blog.entity.model.VisitorModel;
|
|||||||
import cn.celess.blog.exception.MyException;
|
import cn.celess.blog.exception.MyException;
|
||||||
import cn.celess.blog.mapper.VisitorMapper;
|
import cn.celess.blog.mapper.VisitorMapper;
|
||||||
import cn.celess.blog.service.VisitorService;
|
import cn.celess.blog.service.VisitorService;
|
||||||
|
import cn.celess.blog.util.AddressUtil;
|
||||||
import cn.celess.blog.util.DateFormatUtil;
|
import cn.celess.blog.util.DateFormatUtil;
|
||||||
import cn.celess.blog.util.RedisUtil;
|
import cn.celess.blog.util.RedisUtil;
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
@@ -50,7 +51,7 @@ public class VisitorServiceImpl implements VisitorService {
|
|||||||
public PageData<VisitorModel> visitorPage(int page, int count, boolean showLocation) {
|
public PageData<VisitorModel> visitorPage(int page, int count, boolean showLocation) {
|
||||||
PageHelper.startPage(page, count);
|
PageHelper.startPage(page, count);
|
||||||
List<Visitor> visitorList = visitorMapper.findAll();
|
List<Visitor> visitorList = visitorMapper.findAll();
|
||||||
return new PageData<VisitorModel>(new PageInfo<Visitor>(visitorList), list2List(visitorList, showLocation));
|
return new PageData<>(new PageInfo<>(visitorList), list2List(visitorList, showLocation));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -151,60 +152,7 @@ public class VisitorServiceImpl implements VisitorService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private String getLocation(String ip) {
|
private String getLocation(String ip) {
|
||||||
StringBuilder result = new StringBuilder();
|
return AddressUtil.getCityInfo(ip);
|
||||||
URL url;
|
|
||||||
HttpURLConnection conn = null;
|
|
||||||
InputStream inputStream = null;
|
|
||||||
InputStreamReader inputStreamReader = null;
|
|
||||||
BufferedReader bufferedReader = null;
|
|
||||||
try {
|
|
||||||
url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
|
|
||||||
conn = (HttpURLConnection) url.openConnection();
|
|
||||||
conn.setConnectTimeout(3000);
|
|
||||||
conn.setDoInput(true);
|
|
||||||
conn.setRequestMethod("GET");
|
|
||||||
inputStream = conn.getInputStream();
|
|
||||||
inputStreamReader = new InputStreamReader(inputStream);
|
|
||||||
bufferedReader = new BufferedReader(inputStreamReader);
|
|
||||||
String tmp;
|
|
||||||
while ((tmp = bufferedReader.readLine()) != null) {
|
|
||||||
result.append(tmp);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
// ignore
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if (conn != null) {
|
|
||||||
conn.disconnect();
|
|
||||||
}
|
|
||||||
if (inputStream != null) {
|
|
||||||
inputStream.close();
|
|
||||||
}
|
|
||||||
if (inputStreamReader != null) {
|
|
||||||
inputStreamReader.close();
|
|
||||||
}
|
|
||||||
if (bufferedReader != null) {
|
|
||||||
bufferedReader.close();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
if ("".equals(result.toString())) {
|
|
||||||
throw new MyException(ResponseEnum.FAILURE);
|
|
||||||
}
|
|
||||||
Map<String, Object> stringObjectMap = JsonParserFactory.getJsonParser().parseMap(result.toString());
|
|
||||||
if ((Integer) stringObjectMap.get("code") == 0) {
|
|
||||||
LinkedHashMap data = (LinkedHashMap) stringObjectMap.get("data");
|
|
||||||
sb.append(data.get("country"))
|
|
||||||
.append("-")
|
|
||||||
.append(data.get("region"))
|
|
||||||
.append("-")
|
|
||||||
.append(data.get("city"));
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : xiaohai
|
* @author : xiaohai
|
||||||
@@ -72,7 +73,7 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
|
|||||||
public PageData<WebUpdateModel> pages(int count, int page) {
|
public PageData<WebUpdateModel> pages(int count, int page) {
|
||||||
PageHelper.startPage(page, count);
|
PageHelper.startPage(page, count);
|
||||||
List<WebUpdate> updateList = webUpdateInfoMapper.findAll();
|
List<WebUpdate> updateList = webUpdateInfoMapper.findAll();
|
||||||
return new PageData<WebUpdateModel>(new PageInfo<WebUpdate>(updateList), list2List(updateList));
|
return new PageData<>(new PageInfo<>(updateList), list2List(updateList));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -108,8 +109,6 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) {
|
private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) {
|
||||||
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
|
return webUpdates.stream().map(ModalTrans::webUpdate).collect(Collectors.toList());
|
||||||
webUpdates.forEach(update -> webUpdateModels.add(ModalTrans.webUpdate(update)));
|
|
||||||
return webUpdateModels;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
71
src/main/java/cn/celess/blog/util/AddressUtil.java
Normal file
71
src/main/java/cn/celess/blog/util/AddressUtil.java
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package cn.celess.blog.util;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.lionsoul.ip2region.DataBlock;
|
||||||
|
import org.lionsoul.ip2region.DbConfig;
|
||||||
|
import org.lionsoul.ip2region.DbSearcher;
|
||||||
|
import org.lionsoul.ip2region.Util;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : xiaohai
|
||||||
|
* @date : 2020/09/04 9:36
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class AddressUtil {
|
||||||
|
|
||||||
|
public static String getCityInfo(String ip) {
|
||||||
|
File file;
|
||||||
|
try {
|
||||||
|
//db
|
||||||
|
String dbPath = AddressUtil.class.getResource("/ip2region/ip2region.db").getPath();
|
||||||
|
file = new File(dbPath);
|
||||||
|
if (!file.exists()) {
|
||||||
|
String tmpDir = System.getProperties().getProperty("java.io.tmpdir");
|
||||||
|
dbPath = tmpDir + "ip.db";
|
||||||
|
file = new File(dbPath);
|
||||||
|
FileUtils.copyInputStreamToFile(Objects.requireNonNull(AddressUtil.class.getClassLoader().getResourceAsStream("classpath:ip2region/ip2region.db")), file);
|
||||||
|
}
|
||||||
|
//查询算法
|
||||||
|
//B-tree
|
||||||
|
int algorithm = DbSearcher.BTREE_ALGORITHM;
|
||||||
|
try {
|
||||||
|
DbConfig config = new DbConfig();
|
||||||
|
DbSearcher searcher = new DbSearcher(config, dbPath);
|
||||||
|
Method method = null;
|
||||||
|
switch (algorithm) {
|
||||||
|
case DbSearcher.BTREE_ALGORITHM:
|
||||||
|
method = searcher.getClass().getMethod("btreeSearch", String.class);
|
||||||
|
break;
|
||||||
|
case DbSearcher.BINARY_ALGORITHM:
|
||||||
|
method = searcher.getClass().getMethod("binarySearch", String.class);
|
||||||
|
break;
|
||||||
|
case DbSearcher.MEMORY_ALGORITYM:
|
||||||
|
method = searcher.getClass().getMethod("memorySearch", String.class);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
DataBlock dataBlock;
|
||||||
|
if (!Util.isIpAddress(ip)) {
|
||||||
|
System.out.println("Error: Invalid ip address");
|
||||||
|
}
|
||||||
|
|
||||||
|
dataBlock = (DataBlock) method.invoke(searcher, ip);
|
||||||
|
|
||||||
|
return dataBlock.getRegion();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/main/resources/ip2region/ip2region.db
Normal file
BIN
src/main/resources/ip2region/ip2region.db
Normal file
Binary file not shown.
@@ -31,6 +31,7 @@ public class VisitorControllerTest extends BaseTest {
|
|||||||
public void page() throws Exception {
|
public void page() throws Exception {
|
||||||
int count = 10;
|
int count = 10;
|
||||||
int page = 1;
|
int page = 1;
|
||||||
|
// 默认显示location
|
||||||
getMockData(get("/admin/visitor/page?count=" + count + "&page=" + page), adminLogin()).andDo(result -> {
|
getMockData(get("/admin/visitor/page?count=" + count + "&page=" + page), adminLogin()).andDo(result -> {
|
||||||
Response<PageData<VisitorModel>> response = getResponse(result, VISITOR_PAGE_TYPE);
|
Response<PageData<VisitorModel>> response = getResponse(result, VISITOR_PAGE_TYPE);
|
||||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
@@ -41,6 +42,7 @@ public class VisitorControllerTest extends BaseTest {
|
|||||||
for (VisitorModel v : pageData.getList()) {
|
for (VisitorModel v : pageData.getList()) {
|
||||||
assertNotEquals(0, v.getId());
|
assertNotEquals(0, v.getId());
|
||||||
assertNotNull(v.getDate());
|
assertNotNull(v.getDate());
|
||||||
|
assertNotNull(v.getLocation());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
29
src/test/java/cn/celess/blog/service/VisitorServiceTest.java
Normal file
29
src/test/java/cn/celess/blog/service/VisitorServiceTest.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package cn.celess.blog.service;
|
||||||
|
|
||||||
|
import cn.celess.blog.BaseTest;
|
||||||
|
import cn.celess.blog.entity.model.PageData;
|
||||||
|
import cn.celess.blog.entity.model.VisitorModel;
|
||||||
|
import com.alibaba.druid.util.StringUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class VisitorServiceTest extends BaseTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
VisitorService visitorService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void location() {
|
||||||
|
assertEquals("0|0|0|内网IP|内网IP", visitorService.location("127.0.0.1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void visitorPage() {
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
PageData<VisitorModel> visitorModelPageData = visitorService.visitorPage(1, 10, true);
|
||||||
|
assertTrue(System.currentTimeMillis() - start <= 1500);
|
||||||
|
assertTrue(visitorModelPageData.getList().stream().noneMatch(visitor -> StringUtils.isEmpty(visitor.getLocation())));
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/test/java/cn/celess/blog/util/AddressUtilTest.java
Normal file
15
src/test/java/cn/celess/blog/util/AddressUtilTest.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package cn.celess.blog.util;
|
||||||
|
|
||||||
|
import cn.celess.blog.BaseTest;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class AddressUtilTest extends BaseTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getCityInfo() {
|
||||||
|
assertEquals("0|0|0|内网IP|内网IP", AddressUtil.getCityInfo("127.0.0.1"));
|
||||||
|
assertEquals("中国|0|上海|上海市|阿里云", AddressUtil.getCityInfo("106.15.205.190"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user