diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/VisitCountController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/VisitCountController.java new file mode 100644 index 0000000..2c73a96 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/VisitCountController.java @@ -0,0 +1,35 @@ +package com.luoo.comment.controller; + +import api.Result; +import com.luoo.comment.service.VisitCountService; +import controller.BaseController; +import io.swagger.annotations.Api; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +/** + * @program: luoo_parent + * @description: + * @author: yawei.huang + * @create: 2024-10-31 12:37 + **/ +@RestController +@CrossOrigin +@RequestMapping("/visit") +@Api(tags = "埋点") +public class VisitCountController extends BaseController { + @Autowired + private VisitCountService visitCountService; + + @GetMapping("/count/o2o") + public Result addNewVisit(HttpServletRequest req){ + String ipAddr = getIpAddr(req); + visitCountService.addNewVisit(ipAddr); + return Result.success(); + } +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/dao/VisitCountDao.java b/luoo_comment/src/main/java/com/luoo/comment/dao/VisitCountDao.java new file mode 100644 index 0000000..4e6ca0d --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/dao/VisitCountDao.java @@ -0,0 +1,7 @@ +package com.luoo.comment.dao; + +import com.luoo.comment.pojo.VisitCount; +import org.springframework.data.mongodb.repository.MongoRepository; + +public interface VisitCountDao extends MongoRepository { +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/enums/VisitTypeEnums.java b/luoo_comment/src/main/java/com/luoo/comment/enums/VisitTypeEnums.java new file mode 100644 index 0000000..06f9e37 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/enums/VisitTypeEnums.java @@ -0,0 +1,18 @@ +package com.luoo.comment.enums; + +import lombok.Getter; + +@Getter +public enum VisitTypeEnums { + O2O("o2o","o2o"), + WEB("web","web"); + + private String type; + private String desc; + + VisitTypeEnums(String type, String desc) { + this.type = type; + this.desc = desc; + } + +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/VisitCount.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/VisitCount.java new file mode 100644 index 0000000..d18c22e --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/VisitCount.java @@ -0,0 +1,45 @@ +package com.luoo.comment.pojo; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Builder; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * @program: luoo_parent + * @description: 访问次数 + * @author: yawei.huang + * @create: 2024-10-31 12:28 + **/ +@Builder +@Data +public class VisitCount implements Serializable { + + private static final long serialVersionUID = 1161582568237312816L; + + /** + * 访问方式,如o2o, web + */ + private String type; + + /** + * 访问时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime visitTime; + + /** + * ip地址 + */ + private String ip; + + /** + * 地理位置 + */ + private String location; + +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/VisitCountService.java b/luoo_comment/src/main/java/com/luoo/comment/service/VisitCountService.java new file mode 100644 index 0000000..d4b80ab --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/service/VisitCountService.java @@ -0,0 +1,43 @@ +package com.luoo.comment.service; + +import com.luoo.comment.dao.VisitCountDao; +import com.luoo.comment.enums.VisitTypeEnums; +import com.luoo.comment.pojo.VisitCount; +import com.luoo.comment.util.IpUtil; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.util.List; + +/** + * @program: luoo_parent + * @description: 访问页面service层 + * @author: yawei.huang + * @create: 2024-10-31 12:30 + **/ +@Service +public class VisitCountService { + + private final VisitCountDao visitCountDao; + + public VisitCountService(VisitCountDao visitCountDao) { + this.visitCountDao = visitCountDao; + } + + /** + * 新增一次访问 + */ + public void addNewVisit(String ipAddr) { + String ipLocation = IpUtil.getIpLocation(ipAddr); + VisitCount visitCount = VisitCount.builder() + .ip(ipAddr) + .type(VisitTypeEnums.O2O.getType()) + .visitTime(LocalDateTime.now()) + .location(ipLocation) + .build(); + visitCountDao.save(visitCount); + } +}