diff --git a/blog-api/pom.xml b/blog-api/pom.xml
index aed43a72..8e2d6224 100644
--- a/blog-api/pom.xml
+++ b/blog-api/pom.xml
@@ -150,6 +150,18 @@
hutool-crypto
5.8.11
+
+
+ cn.hutool
+ hutool-all
+ 5.8.11
+
+
+
+ org.apache.poi
+ poi-ooxml
+ 5.2.3
+
diff --git a/blog-api/src/main/java/top/naccl/controller/admin/VisitLogController.java b/blog-api/src/main/java/top/naccl/controller/admin/VisitLogController.java
index afe31056..c988f303 100644
--- a/blog-api/src/main/java/top/naccl/controller/admin/VisitLogController.java
+++ b/blog-api/src/main/java/top/naccl/controller/admin/VisitLogController.java
@@ -13,6 +13,8 @@
import top.naccl.model.vo.Result;
import top.naccl.service.VisitLogService;
+import javax.servlet.http.HttpServletResponse;
+
/**
* @Description: 访问日志后台管理
* @Author: Naccl
@@ -61,4 +63,24 @@ public Result delete(@RequestParam Long id) {
visitLogService.deleteVisitLogById(id);
return Result.ok("删除成功");
}
+
+ /**
+ * 导出访问日志到Excel
+ *
+ * @param uuid 访客标识码
+ * @param date 访问时间范围
+ * @param response HTTP响应
+ */
+ @GetMapping("/visitLog/export")
+ public void exportVisitLog(@RequestParam(defaultValue = "") String uuid,
+ @RequestParam(defaultValue = "") String[] date,
+ HttpServletResponse response) {
+ String startDate = null;
+ String endDate = null;
+ if (date.length == 2) {
+ startDate = date[0];
+ endDate = date[1];
+ }
+ visitLogService.exportVisitLogToExcel(StringUtils.trim(uuid), startDate, endDate, response);
+ }
}
diff --git a/blog-api/src/main/java/top/naccl/service/VisitLogService.java b/blog-api/src/main/java/top/naccl/service/VisitLogService.java
index fd3b4d41..2b20b7c2 100644
--- a/blog-api/src/main/java/top/naccl/service/VisitLogService.java
+++ b/blog-api/src/main/java/top/naccl/service/VisitLogService.java
@@ -4,6 +4,7 @@
import top.naccl.entity.VisitLog;
import top.naccl.model.dto.VisitLogUuidTime;
+import javax.servlet.http.HttpServletResponse;
import java.util.List;
public interface VisitLogService {
@@ -15,4 +16,6 @@ public interface VisitLogService {
void saveVisitLog(VisitLog log);
void deleteVisitLogById(Long id);
+
+ void exportVisitLogToExcel(String uuid, String startDate, String endDate, HttpServletResponse response);
}
diff --git a/blog-api/src/main/java/top/naccl/service/impl/VisitLogServiceImpl.java b/blog-api/src/main/java/top/naccl/service/impl/VisitLogServiceImpl.java
index bf8a5613..4ab34c24 100644
--- a/blog-api/src/main/java/top/naccl/service/impl/VisitLogServiceImpl.java
+++ b/blog-api/src/main/java/top/naccl/service/impl/VisitLogServiceImpl.java
@@ -1,5 +1,8 @@
package top.naccl.service.impl;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.poi.excel.ExcelUtil;
+import cn.hutool.poi.excel.ExcelWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -12,7 +15,15 @@
import top.naccl.util.IpAddressUtils;
import top.naccl.util.UserAgentUtils;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
/**
* @Description: 访问日志业务层实现
@@ -56,4 +67,59 @@ public void deleteVisitLogById(Long id) {
throw new PersistenceException("删除日志失败");
}
}
+
+ @Override
+ public void exportVisitLogToExcel(String uuid, String startDate, String endDate, HttpServletResponse response) {
+ // 如果没有设置时间范围,默认导出最近7天的数据
+ if ((startDate == null || startDate.trim().isEmpty()) && (endDate == null || endDate.trim().isEmpty())) {
+ Date endTime = new Date();
+ Date startTime = DateUtil.offsetDay(endTime, -7);
+ startDate = DateUtil.formatDateTime(startTime);
+ endDate = DateUtil.formatDateTime(endTime);
+ }
+
+ // 获取符合条件的访问日志列表
+ List visitLogList = visitLogMapper.getVisitLogListByUUIDAndDate(uuid, startDate, endDate);
+
+ // 创建Excel数据
+ List