Compare commits

...

9 Commits
v2.3.0 ... dev

Author SHA1 Message Date
禾几海
0b40522e93 feat: 默认返回位置信息 2020-09-05 16:35:45 +08:00
禾几海
9026ba0732 refactor: 简化代码 2020-09-05 15:43:43 +08:00
禾几海
f5f2437fa4 refactor: 简化代码 2020-09-05 15:42:03 +08:00
禾几海
0bcca091e1 refactor: 简化代码 2020-09-04 13:02:36 +08:00
禾几海
3c69baa4ad Merge branch 'feature-ip2region' 2020-09-04 12:29:18 +08:00
禾几海
baf5b1bbb7 feat: 添加对jdk8以上环境的支持 2020-09-04 12:23:15 +08:00
禾几海
38621bcfa4 refactor: 换用ip2region 2020-09-04 12:11:14 +08:00
禾几海
cf435a588f refactor: 添加ip2region 2020-09-04 09:42:55 +08:00
禾几海
feab26f4f7 docs: add some badge 2020-09-01 08:30:26 +08:00
14 changed files with 227 additions and 136 deletions

View File

@@ -8,7 +8,11 @@
[![Build](https://github.com/xiaohai2271/blog-backEnd/workflows/Build/badge.svg)](https://github.com/xiaohai2271/blog-backEnd)
[![Test](https://github.com/xiaohai2271/blog-backEnd/workflows/Test/badge.svg)](https://github.com/xiaohai2271/blog-backEnd)
[![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/xiaohai2271/blog-backEnd)](https://github.com/xiaohai2271/blog-backEnd)
[![GitHub](https://img.shields.io/github/license/xiaohai2271/blog-backEnd)](https://github.com/xiaohai2271/blog-backEnd)
[![GitHub top language](https://img.shields.io/github/languages/top/xiaohai2271/blog-backEnd)](https://github.com/xiaohai2271/blog-backEnd)
[![GitHub commit activity](https://img.shields.io/github/commit-activity/m/xiaohai2271/blog-backEnd)](https://github.com/xiaohai2271/blog-backEnd)
[![GitHub last commit](https://img.shields.io/github/last-commit/xiaohai2271/blog-backEnd)](https://github.com/xiaohai2271/blog-backEnd)
[![Website](https://img.shields.io/website?up_message=%E5%B0%8F%E6%B5%B7%E5%8D%9A%E5%AE%A2&url=https%3A%2F%2Fwww.celess.cn)](https://www.celess.cn)
</div>

28
pom.xml
View File

@@ -175,6 +175,34 @@
</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>
<build>

View File

@@ -27,7 +27,7 @@ public class VisitorController {
@GetMapping("/admin/visitor/page")
public Response page(@RequestParam(value = "count", required = false, defaultValue = "10") int count,
@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));
}

View File

@@ -304,16 +304,12 @@ public class ArticleServiceImpl implements ArticleService {
public PageData<ArticleModel> retrievePageForOpen(int count, int page) {
PageHelper.startPage(page, count);
List<Article> articleList = articleMapper.findAllByOpen(true);
PageData<ArticleModel> pageData = new PageData<>(new PageInfo<Article>(articleList));
List<ArticleModel> articleModelList = new ArrayList<>();
articleList.forEach(article -> {
ArticleModel model = ModalTrans.article(article, true);
setPreAndNextArticle(model);
articleModelList.add(model);
});
PageData<ArticleModel> pageData = new PageData<>(new PageInfo<>(articleList));
List<ArticleModel> articleModelList = articleList
.stream()
.map(article -> setPreAndNextArticle(ModalTrans.article(article, true)))
.collect(Collectors.toList());
pageData.setList(articleModelList);
return pageData;
}
@@ -327,17 +323,14 @@ public class ArticleServiceImpl implements ArticleService {
PageHelper.startPage(page, count);
List<Article> open = articleMapper.findAllByCategoryIdAndOpen(category.getId());
List<ArticleModel> modelList = new ArrayList<>();
open.forEach(article -> {
ArticleModel model = ModalTrans.article(article, true);
model.setTags(null);
// setPreAndNextArticle(model);
model.setNextArticle(null);
model.setPreArticle(null);
modelList.add(model);
});
return new PageData<ArticleModel>(new PageInfo<Article>(open), modelList);
List<ArticleModel> modelList = open.stream()
.map(article -> ModalTrans.article(article, true))
.peek(articleModel -> {
articleModel.setNextArticle(null);
articleModel.setPreArticle(null);
})
.collect(Collectors.toList());
return new PageData<>(new PageInfo<>(open), modelList);
}
@Override
@@ -348,21 +341,22 @@ public class ArticleServiceImpl implements ArticleService {
}
PageHelper.startPage(page, count);
List<ArticleTag> articleByTag = articleTagMapper.findArticleByTagAndOpen(tag.getId());
List<ArticleModel> modelList = new ArrayList<>();
articleByTag.forEach(articleTag -> {
ArticleModel model = ModalTrans.article(articleTag.getArticle(), true);
model.setNextArticle(null);
model.setPreArticle(null);
modelList.add(model);
});
return new PageData<ArticleModel>(new PageInfo<ArticleTag>(articleByTag), modelList);
List<ArticleModel> modelList = articleByTag
.stream()
.map(articleTag -> ModalTrans.article(articleTag.getArticle(), true))
.peek(articleModel -> {
articleModel.setNextArticle(null);
articleModel.setPreArticle(null);
}).collect(Collectors.toList());
return new PageData<>(new PageInfo<>(articleByTag), modelList);
}
private void setPreAndNextArticle(ArticleModel articleModel) {
private ArticleModel setPreAndNextArticle(ArticleModel articleModel) {
if (articleModel == null) {
return;
return null;
}
articleModel.setPreArticle(ModalTrans.article(articleMapper.getPreArticle(articleModel.getId()), true));
articleModel.setNextArticle(ModalTrans.article(articleMapper.getNextArticle(articleModel.getId()), true));
return articleModel;
}
}

View File

@@ -17,8 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author : xiaohai
@@ -68,22 +68,25 @@ public class CategoryServiceImpl implements CategoryService {
public PageData<CategoryModel> retrievePage(int page, int count) {
PageHelper.startPage(page, count);
List<Category> all = categoryMapper.findAll();
List<CategoryModel> modelList = new ArrayList<>();
all.forEach(e -> {
CategoryModel model = ModalTrans.category(e);
List<Article> allByCategoryId = articleMapper.findAllByCategoryId(e.getId());
List<ArticleModel> articleModelList = new ArrayList<>();
allByCategoryId.forEach(article -> {
ArticleModel articleModel = ModalTrans.article(article, true);
// 遍历没一个category
List<CategoryModel> modelList = all
.stream()
.map(ModalTrans::category)
.peek(categoryModel -> {
// 根据category去查article并赋值给categoryModel
List<Article> allByCategoryId = articleMapper.findAllByCategoryId(categoryModel.getId());
List<ArticleModel> articleModelList = allByCategoryId
.stream()
.map(article -> ModalTrans.article(article, true))
.peek(articleModel -> {
// 去除不必要的字段
articleModel.setPreArticle(null);
articleModel.setNextArticle(null);
articleModel.setTags(null);
articleModelList.add(articleModel);
});
model.setArticles(articleModelList);
modelList.add(model);
});
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);
}
}

View File

@@ -23,6 +23,7 @@ import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author : xiaohai
@@ -107,14 +108,11 @@ public class CommentServiceImpl implements CommentService {
@Override
public List<CommentModel> retrievePageByPid(long pid) {
List<Comment> allByPagePath = commentMapper.findAllByPid(pid);
List<CommentModel> commentModels = new ArrayList<>();
allByPagePath.forEach(comment -> {
if (comment.getStatus() != CommentStatusEnum.DELETED.getCode()) {
commentModels.add(ModalTrans.comment(comment));
}
});
return commentModels;
return allByPagePath
.stream()
.filter(comment -> comment.getStatus() != CommentStatusEnum.DELETED.getCode())
.map(ModalTrans::comment)
.collect(Collectors.toList());
}
@Override
@@ -145,14 +143,11 @@ public class CommentServiceImpl implements CommentService {
private PageData<CommentModel> pageTrans(List<Comment> commentList, boolean noResponseList) {
PageInfo<Comment> p = PageInfo.of(commentList);
List<CommentModel> modelList = new ArrayList<>();
commentList.forEach(l -> {
CommentModel model = ModalTrans.comment(l);
if (!noResponseList) {
model.setRespComment(this.retrievePageByPid(model.getId()));
}
modelList.add(model);
});
return new PageData<CommentModel>(p, modelList);
List<CommentModel> modelList = commentList
.stream()
.map(ModalTrans::comment)
.peek(commentModel -> commentModel.setRespComment(this.retrievePageByPid(commentModel.getId())))
.collect(Collectors.toList());
return new PageData<>(p, modelList);
}
}

View File

@@ -21,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author : xiaohai
@@ -81,20 +82,22 @@ public class TagServiceImpl implements TagService {
List<Tag> tagList = tagMapper.findAll();
List<TagModel> modelList = new ArrayList<>();
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
public List<TagModel> findAll() {
List<TagModel> list = new ArrayList<>();
tagMapper.findAll().forEach(e -> {
TagModel model = ModalTrans.tag(e);
List<ArticleTag> articleByTagAndOpen = articleTagMapper.findArticleByTagAndOpen(e.getId());
List<ArticleModel> articleModelList = new ArrayList<>();
articleByTagAndOpen.forEach(articleTag -> articleModelList.add(ModalTrans.article(articleTag.getArticle(), true)));
model.setArticles(articleModelList);
list.add(model);
});
return list;
return tagMapper.findAll().stream()
.map(ModalTrans::tag)
.peek(tagModel -> {
List<ArticleTag> articleByTagAndOpen = articleTagMapper.findArticleByTagAndOpen(tagModel.getId());
tagModel.setArticles(
articleByTagAndOpen
.stream()
.map(articleTag -> ModalTrans.article(articleTag.getArticle(), true))
.collect(Collectors.toList())
);
})
.collect(Collectors.toList());
}
}

View File

@@ -7,6 +7,7 @@ import cn.celess.blog.entity.model.VisitorModel;
import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.VisitorMapper;
import cn.celess.blog.service.VisitorService;
import cn.celess.blog.util.AddressUtil;
import cn.celess.blog.util.DateFormatUtil;
import cn.celess.blog.util.RedisUtil;
import com.github.pagehelper.PageHelper;
@@ -50,7 +51,7 @@ public class VisitorServiceImpl implements VisitorService {
public PageData<VisitorModel> visitorPage(int page, int count, boolean showLocation) {
PageHelper.startPage(page, count);
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
@@ -151,60 +152,7 @@ public class VisitorServiceImpl implements VisitorService {
* @return
*/
private String getLocation(String ip) {
StringBuilder result = new StringBuilder();
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();
return AddressUtil.getCityInfo(ip);
}

View File

@@ -22,6 +22,7 @@ import org.springframework.stereotype.Service;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author : xiaohai
@@ -72,7 +73,7 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
public PageData<WebUpdateModel> pages(int count, int page) {
PageHelper.startPage(page, count);
List<WebUpdate> updateList = webUpdateInfoMapper.findAll();
return new PageData<WebUpdateModel>(new PageInfo<WebUpdate>(updateList), list2List(updateList));
return new PageData<>(new PageInfo<>(updateList), list2List(updateList));
}
@Override
@@ -108,8 +109,6 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
}
private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) {
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
webUpdates.forEach(update -> webUpdateModels.add(ModalTrans.webUpdate(update)));
return webUpdateModels;
return webUpdates.stream().map(ModalTrans::webUpdate).collect(Collectors.toList());
}
}

View 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;
}
}

Binary file not shown.

View File

@@ -31,6 +31,7 @@ public class VisitorControllerTest extends BaseTest {
public void page() throws Exception {
int count = 10;
int page = 1;
// 默认显示location
getMockData(get("/admin/visitor/page?count=" + count + "&page=" + page), adminLogin()).andDo(result -> {
Response<PageData<VisitorModel>> response = getResponse(result, VISITOR_PAGE_TYPE);
assertEquals(SUCCESS.getCode(), response.getCode());
@@ -41,6 +42,7 @@ public class VisitorControllerTest extends BaseTest {
for (VisitorModel v : pageData.getList()) {
assertNotEquals(0, v.getId());
assertNotNull(v.getDate());
assertNotNull(v.getLocation());
}
});
}

View 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())));
}
}

View 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"));
}
}