diff --git a/luoo_comment/src/main/java/com/luoo/comment/enums/Province.java b/luoo_comment/src/main/java/com/luoo/comment/enums/Province.java index 8088cac..5f8c5ba 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/enums/Province.java +++ b/luoo_comment/src/main/java/com/luoo/comment/enums/Province.java @@ -1,5 +1,7 @@ package com.luoo.comment.enums; +import cn.hutool.core.util.ObjectUtil; + public enum Province { BEIJING("Beijing", "北京"), TIANJIN("Tianjin", "天津"), @@ -49,6 +51,9 @@ public enum Province { } public static String getCityName(String englishName) { + if (ObjectUtil.isEmpty(englishName)) { + return "未知IP"; + } for (Province province : Province.values()) { if (englishName.equalsIgnoreCase(province.englishName)) { return province.chineseName; diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java b/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java index a4ec8ac..526ee57 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java +++ b/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java @@ -6,10 +6,14 @@ import client.vo.SimpleUser; import cn.hutool.core.util.ObjectUtil; import com.luoo.comment.dao.CommentDao; import com.luoo.comment.enums.CommentStatusEnum; +import com.luoo.comment.enums.Province; import com.luoo.comment.pojo.Comment; import com.luoo.comment.pojo.CommentDto; import com.luoo.comment.pojo.CommentVo; import com.luoo.comment.service.CommentService; +import lombok.extern.slf4j.Slf4j; +import net.renfei.ip2location.IP2Location; +import net.renfei.ip2location.IPResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.*; import org.springframework.data.mongodb.core.MongoTemplate; @@ -20,6 +24,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; +import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -29,6 +34,7 @@ import java.util.stream.Collectors; @Service @Transactional +@Slf4j public class CommentServiceImpl implements CommentService { // @Autowired @@ -48,6 +54,20 @@ public class CommentServiceImpl implements CommentService { return commentDao.findById(id).get(); } + public String getCityNameByIp(String ip) { + IP2Location loc = new IP2Location(); + String binFile = "classpath:/IP2LOCATION-LITE-DB11.BIN"; + try { + loc.Open(binFile, true); + IPResult rec = loc.IPQuery(ip); + String city = rec.getCity(); + return Province.getCityName(city); + } catch (IOException e) { + log.info("getCityNameByIp error {}",ip,e); + return "未知 IP"; + } + } + public void save(CommentDto commentDto){ Comment comment = new Comment(); comment.setContent(commentDto.getContent()); @@ -59,7 +79,7 @@ public class CommentServiceImpl implements CommentService { comment.setLevel(1); comment.setTargetId(commentDto.getTargetId()); comment.setType(0); - comment.setCity("上海"); + comment.setCity(getCityNameByIp(commentDto.getIpAddress())); comment.setId(String.valueOf(idWorker.nextId())); comment.setShare(0); comment.setThumbup(0);