IP 解析提交

main
lyp 10 months ago
parent c7901bc7d8
commit 0f354723a6

@ -1,5 +1,7 @@
package com.luoo.comment.enums; package com.luoo.comment.enums;
import cn.hutool.core.util.ObjectUtil;
public enum Province { public enum Province {
BEIJING("Beijing", "北京"), BEIJING("Beijing", "北京"),
TIANJIN("Tianjin", "天津"), TIANJIN("Tianjin", "天津"),
@ -49,6 +51,9 @@ public enum Province {
} }
public static String getCityName(String englishName) { public static String getCityName(String englishName) {
if (ObjectUtil.isEmpty(englishName)) {
return "未知IP";
}
for (Province province : Province.values()) { for (Province province : Province.values()) {
if (englishName.equalsIgnoreCase(province.englishName)) { if (englishName.equalsIgnoreCase(province.englishName)) {
return province.chineseName; return province.chineseName;

@ -6,10 +6,14 @@ import client.vo.SimpleUser;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.luoo.comment.dao.CommentDao; import com.luoo.comment.dao.CommentDao;
import com.luoo.comment.enums.CommentStatusEnum; import com.luoo.comment.enums.CommentStatusEnum;
import com.luoo.comment.enums.Province;
import com.luoo.comment.pojo.Comment; import com.luoo.comment.pojo.Comment;
import com.luoo.comment.pojo.CommentDto; import com.luoo.comment.pojo.CommentDto;
import com.luoo.comment.pojo.CommentVo; import com.luoo.comment.pojo.CommentVo;
import com.luoo.comment.service.CommentService; 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.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*; import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
@ -20,6 +24,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import util.IdWorker; import util.IdWorker;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -29,6 +34,7 @@ import java.util.stream.Collectors;
@Service @Service
@Transactional @Transactional
@Slf4j
public class CommentServiceImpl implements CommentService { public class CommentServiceImpl implements CommentService {
// @Autowired // @Autowired
@ -48,6 +54,20 @@ public class CommentServiceImpl implements CommentService {
return commentDao.findById(id).get(); 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){ public void save(CommentDto commentDto){
Comment comment = new Comment(); Comment comment = new Comment();
comment.setContent(commentDto.getContent()); comment.setContent(commentDto.getContent());
@ -59,7 +79,7 @@ public class CommentServiceImpl implements CommentService {
comment.setLevel(1); comment.setLevel(1);
comment.setTargetId(commentDto.getTargetId()); comment.setTargetId(commentDto.getTargetId());
comment.setType(0); comment.setType(0);
comment.setCity("上海"); comment.setCity(getCityNameByIp(commentDto.getIpAddress()));
comment.setId(String.valueOf(idWorker.nextId())); comment.setId(String.valueOf(idWorker.nextId()));
comment.setShare(0); comment.setShare(0);
comment.setThumbup(0); comment.setThumbup(0);

Loading…
Cancel
Save