f
This commit is contained in:
@@ -41,7 +41,7 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
|
||||
# - tzdata: 时区
|
||||
# - curl: 健康检查
|
||||
# - chromium: 无头浏览器,用于 chromedp 生成 HTML 报告 PDF
|
||||
# - nss、freetype、harfbuzz、ttf-freefont: 字体及渲染依赖,避免中文/图标丢失
|
||||
# - nss、freetype、harfbuzz、ttf-freefont、font-noto-cjk: 字体及渲染依赖,避免中文/图标丢失和乱码
|
||||
RUN apk --no-cache add \
|
||||
tzdata \
|
||||
curl \
|
||||
@@ -49,7 +49,8 @@ RUN apk --no-cache add \
|
||||
nss \
|
||||
freetype \
|
||||
harfbuzz \
|
||||
ttf-freefont
|
||||
ttf-freefont \
|
||||
font-noto-cjk
|
||||
|
||||
# 设置时区
|
||||
ENV TZ=Asia/Shanghai
|
||||
|
||||
@@ -25,7 +25,8 @@ type QYGLReportHandler struct {
|
||||
apiRequestService *api_services.ApiRequestService
|
||||
logger *zap.Logger
|
||||
|
||||
reportRepo api_repositories.ReportRepository
|
||||
reportRepo api_repositories.ReportRepository
|
||||
pdfCacheManager *pdf.PDFCacheManager
|
||||
}
|
||||
|
||||
// NewQYGLReportHandler 创建企业报告页面处理器
|
||||
@@ -33,11 +34,13 @@ func NewQYGLReportHandler(
|
||||
apiRequestService *api_services.ApiRequestService,
|
||||
logger *zap.Logger,
|
||||
reportRepo api_repositories.ReportRepository,
|
||||
pdfCacheManager *pdf.PDFCacheManager,
|
||||
) *QYGLReportHandler {
|
||||
return &QYGLReportHandler{
|
||||
apiRequestService: apiRequestService,
|
||||
logger: logger,
|
||||
reportRepo: reportRepo,
|
||||
pdfCacheManager: pdfCacheManager,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +160,21 @@ func (h *QYGLReportHandler) GetQYGLReportPDFByID(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 先尝试从缓存中读取(基于报告ID)
|
||||
if h.pdfCacheManager != nil {
|
||||
if cachedBytes, hit, _, err := h.pdfCacheManager.GetByReportID(id); err == nil && hit && len(cachedBytes) > 0 {
|
||||
h.logger.Info("企业全景报告 PDF 缓存命中",
|
||||
zap.String("report_id", id),
|
||||
zap.Int("pdf_size", len(cachedBytes)),
|
||||
)
|
||||
encodedFileName := url.QueryEscape(fileName)
|
||||
c.Header("Content-Type", "application/pdf")
|
||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", encodedFileName))
|
||||
c.Data(http.StatusOK, "application/pdf", cachedBytes)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 根据当前请求推断访问协议(支持通过反向代理的 X-Forwarded-Proto)
|
||||
scheme := "http"
|
||||
if c.Request.TLS != nil {
|
||||
@@ -187,6 +205,16 @@ func (h *QYGLReportHandler) GetQYGLReportPDFByID(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 将生成结果写入缓存(忽略写入错误,不影响主流程)
|
||||
if h.pdfCacheManager != nil {
|
||||
if err := h.pdfCacheManager.SetByReportID(id, pdfBytes); err != nil {
|
||||
h.logger.Warn("保存企业全景报告 PDF 到缓存失败",
|
||||
zap.String("report_id", id),
|
||||
zap.Error(err),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
encodedFileName := url.QueryEscape(fileName)
|
||||
c.Header("Content-Type", "application/pdf")
|
||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", encodedFileName))
|
||||
|
||||
Reference in New Issue
Block a user