diff --git a/internal/domains/api/services/processors/pdfg/pdfg01gz_processor.go b/internal/domains/api/services/processors/pdfg/pdfg01gz_processor.go index 8ca35d7..490b524 100644 --- a/internal/domains/api/services/processors/pdfg/pdfg01gz_processor.go +++ b/internal/domains/api/services/processors/pdfg/pdfg01gz_processor.go @@ -42,12 +42,14 @@ func ProcessPDFG01GZRequest(ctx context.Context, params []byte, deps *processors // 从context获取config(如果存在) var cacheTTL time.Duration = 24 * time.Hour var cacheDir string + var apiDomain string if cfg, ok := ctx.Value("config").(*config.Config); ok && cfg != nil { cacheTTL = cfg.PDFGen.Cache.TTL if cacheTTL == 0 { cacheTTL = 24 * time.Hour } cacheDir = cfg.PDFGen.Cache.CacheDir + apiDomain = cfg.API.Domain } // 获取最大缓存大小 @@ -111,12 +113,15 @@ func ProcessPDFG01GZRequest(ctx context.Context, params []byte, deps *processors time.Sleep(2 * time.Second) // 生成下载链接(基于报告ID) - downloadURL := generateDownloadURL(reportID) + downloadURL := generateDownloadURL(apiDomain, reportID) + expiresAt := createdAt.Add(cacheTTL) return json.Marshal(map[string]interface{}{ "download_url": downloadURL, "report_id": reportID, "cached": true, "created_at": createdAt.Format(time.RFC3339), + "expires_at": expiresAt.Format(time.RFC3339), + "ttl_seconds": int(cacheTTL.Seconds()), }) } @@ -186,7 +191,8 @@ func ProcessPDFG01GZRequest(ctx context.Context, params []byte, deps *processors } // 生成下载链接(基于报告ID) - downloadURL := generateDownloadURL(reportID) + downloadURL := generateDownloadURL(apiDomain, reportID) + expiresAt := time.Now().Add(cacheTTL) zapLogger.Info("PDF生成成功", zap.String("name", paramsDto.Name), @@ -201,6 +207,8 @@ func ProcessPDFG01GZRequest(ctx context.Context, params []byte, deps *processors "report_id": reportID, "report_number": reportNumber, "cached": false, + "expires_at": expiresAt.Format(time.RFC3339), + "ttl_seconds": int(cacheTTL.Seconds()), }) } @@ -494,9 +502,13 @@ func generateReportNumber() string { } // generateDownloadURL 生成下载链接(基于报告ID/缓存键) -func generateDownloadURL(reportID string) string { - // 这里应该生成实际的下载URL - // 暂时返回一个占位符,实际应该根据服务器配置生成 - return fmt.Sprintf("/api/v1/pdfg/download?id=%s", reportID) +// apiDomain: 外部可访问的API域名,如 api.tianyuanapi.com +func generateDownloadURL(apiDomain, reportID string) string { + if apiDomain == "" { + // 兜底:保留相对路径,方便本地/测试环境使用 + return fmt.Sprintf("/api/v1/pdfg/download?id=%s", reportID) + } + // 生成完整链接,例如:https://api.tianyuanapi.com/api/v1/pdfg/download?id=xxx + return fmt.Sprintf("https://%s/api/v1/pdfg/download?id=%s", apiDomain, reportID) }