This commit is contained in:
2026-01-29 15:48:07 +08:00
parent 167dd63a7a
commit 2325a110b6
3 changed files with 43 additions and 13 deletions

View File

@@ -90,6 +90,8 @@ services:
volumes:
- ./logs:/app/logs
- ./resources/Pure_Component:/app/resources/Pure_Component
# 持久化PDF缓存目录确保生成的PDF在容器重启后仍然存在
- ./storage/pdfg-cache:/app/storage/pdfg-cache
# user: "1001:1001" # 注释掉使用root权限运行
networks:
- tyapi-network

View File

@@ -94,13 +94,16 @@ func ProcessPDFG01GZRequest(ctx context.Context, params []byte, deps *processors
pdfGenService = pdfgen.NewPDFGenService(defaultCfg, zapLogger)
}
// 计算缓存键(内部使用,不直接暴露给用户)
cacheKey := cacheManager.GetCacheKey(paramsDto.Name, paramsDto.IDCard)
// 检查缓存(基于姓名+身份证)
_, hit, createdAt, err := cacheManager.Get(paramsDto.Name, paramsDto.IDCard)
if err != nil {
zapLogger.Warn("检查缓存失败继续生成PDF", zap.Error(err))
} else if hit {
// 计算缓存键作为报告ID可持久化
reportID := cacheManager.GetCacheKey(paramsDto.Name, paramsDto.IDCard)
// 生成对外可见的报告ID包含随机前缀防止用户直接推断缓存键
reportID := generateReportID(cacheKey)
// 缓存命中模拟生成耗时约4秒
zapLogger.Info("PDF缓存命中返回缓存文件",
@@ -158,9 +161,6 @@ func ProcessPDFG01GZRequest(ctx context.Context, params []byte, deps *processors
reportNumber = generateReportNumber()
}
// 计算报告ID与缓存键一致便于通过ID直接下载
reportID := cacheManager.GetCacheKey(paramsDto.Name, paramsDto.IDCard)
// 构建PDF生成请求
pdfReq := &pdfgen.GeneratePDFRequest{
Data: formattedData,
@@ -189,6 +189,9 @@ func ProcessPDFG01GZRequest(ctx context.Context, params []byte, deps *processors
// 不影响返回结果,只记录警告
}
// 生成报告ID与内部缓存键解耦对外只暴露带前缀的ID
reportID := generateReportID(cacheKey)
// 生成下载链接基于报告ID
downloadURL := generateDownloadURL(apiDomain, reportID)
expiresAt := time.Now().Add(cacheTTL)
@@ -499,6 +502,13 @@ func generateReportNumber() string {
return fmt.Sprintf("RPT%s", time.Now().Format("20060102150405"))
}
// generateReportID 生成对外可见的报告ID
// 结构:{report_number}-{cacheKey},前缀变化以避免用户轻易看出是否命中缓存
func generateReportID(cacheKey string) string {
reportNumber := generateReportNumber()
return fmt.Sprintf("%s-%s", reportNumber, cacheKey)
}
// generateDownloadURL 生成下载链接基于报告ID/缓存键)
// apiDomain: 外部可访问的API域名如 api.tianyuanapi.com
func generateDownloadURL(apiDomain, reportID string) string {

View File

@@ -3,6 +3,7 @@ package handlers
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
@@ -35,18 +36,25 @@ func NewPDFGHandler(
// DownloadPDF 下载PDF文件
// GET /api/v1/pdfg/download?id=报告ID
func (h *PDFGHandler) DownloadPDF(c *gin.Context) {
reportID := c.Query("id")
rawReportID := c.Query("id")
if reportID == "" {
if rawReportID == "" {
h.responseBuilder.BadRequest(c, "报告ID不能为空")
return
}
// 从对外报告ID中解析内部缓存键使用最后一个'-'后的部分作为缓存键)
cacheKey := rawReportID
if idx := strings.LastIndex(rawReportID, "-"); idx > 0 && idx < len(rawReportID)-1 {
cacheKey = rawReportID[idx+1:]
}
// 从缓存获取PDF
pdfBytes, hit, createdAt, err := h.cacheManager.GetByCacheKey(reportID)
pdfBytes, hit, createdAt, err := h.cacheManager.GetByCacheKey(cacheKey)
if err != nil {
h.logger.Error("获取PDF缓存失败",
zap.String("report_id", reportID),
zap.String("report_id", rawReportID),
zap.String("cache_key", cacheKey),
zap.Error(err),
)
h.responseBuilder.InternalError(c, "获取PDF文件失败")
@@ -55,7 +63,8 @@ func (h *PDFGHandler) DownloadPDF(c *gin.Context) {
if !hit {
h.logger.Warn("PDF文件不存在或已过期",
zap.String("report_id", reportID),
zap.String("report_id", rawReportID),
zap.String("cache_key", cacheKey),
)
h.responseBuilder.NotFound(c, "PDF文件不存在或已过期请重新生成")
return
@@ -65,7 +74,8 @@ func (h *PDFGHandler) DownloadPDF(c *gin.Context) {
expiresAt := createdAt.Add(24 * time.Hour)
if time.Now().After(expiresAt) {
h.logger.Warn("PDF文件已过期",
zap.String("report_id", reportID),
zap.String("report_id", rawReportID),
zap.String("cache_key", cacheKey),
zap.Time("expires_at", expiresAt),
)
h.responseBuilder.NotFound(c, "PDF文件已过期请重新生成")
@@ -74,14 +84,22 @@ func (h *PDFGHandler) DownloadPDF(c *gin.Context) {
// 设置响应头
c.Header("Content-Type", "application/pdf")
c.Header("Content-Disposition", `attachment; filename="大数据租赁风险报告.pdf"`)
// 使用报告ID前缀作为下载文件名的一部分避免泄露内部缓存键
filename := "大数据租赁风险报告.pdf"
if idx := strings.LastIndex(rawReportID, "-"); idx > 0 {
// 使用前缀(报告编号部分)作为文件名的一部分
prefix := rawReportID[:idx]
filename = fmt.Sprintf("大数据租赁风险报告_%s.pdf", prefix)
}
c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
c.Header("Content-Length", fmt.Sprintf("%d", len(pdfBytes)))
// 发送PDF文件
c.Data(http.StatusOK, "application/pdf", pdfBytes)
h.logger.Info("PDF文件下载成功",
zap.String("report_id", reportID),
zap.String("report_id", rawReportID),
zap.String("cache_key", cacheKey),
zap.Int("file_size", len(pdfBytes)),
)
}