This commit is contained in:
2025-12-04 10:35:11 +08:00
parent b0e8974d6c
commit 68def7e08b
7 changed files with 184 additions and 154 deletions

View File

@@ -61,23 +61,37 @@ func (g *PDFGeneratorRefactored) findLogo() {
_, filename, _, _ := runtime.Caller(0)
baseDir := filepath.Dir(filename)
// 优先使用相对路径Linux风格使用正斜杠
logoPaths := []string{
"internal/shared/pdf/天远数据.png", // 相对于项目根目录(最常用)
"./internal/shared/pdf/天远数据.png", // 当前目录下的相对路径
filepath.Join(baseDir, "天远数据.png"), // 相对当前文件
"天远数据.png", // 当前目录
filepath.Join(baseDir, "..", "天远数据.png"), // 上一级目录
filepath.Join(baseDir, "..", "..", "天远数据.png"), // 上两级目录
}
// 尝试相对路径
for _, logoPath := range logoPaths {
if _, err := os.Stat(logoPath); err == nil {
// 直接使用相对路径,不转换为绝对路径
g.logoPath = logoPath
g.logger.Info("找到logo文件", zap.String("logo_path", logoPath))
return
}
}
g.logger.Warn("未找到logo文件", zap.Strings("尝试的路径", logoPaths))
// 尝试服务器绝对路径(后备方案)
if runtime.GOOS == "linux" {
serverPaths := []string{
"/www/tyapi-server/internal/shared/pdf/天远数据.png",
"/app/internal/shared/pdf/天远数据.png",
}
for _, logoPath := range serverPaths {
if _, err := os.Stat(logoPath); err == nil {
g.logoPath = logoPath
return
}
}
}
// 只记录关键错误
g.logger.Warn("未找到logo文件")
}
// GenerateProductPDF 为产品生成PDF文档接受响应类型内部转换
@@ -108,11 +122,6 @@ func (g *PDFGeneratorRefactored) GenerateProductPDFFromEntity(ctx context.Contex
func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *entities.ProductDocumentation) (result []byte, err error) {
defer func() {
if r := recover(); r != nil {
g.logger.Error("PDF生成过程中发生panic",
zap.String("product_id", product.ID),
zap.String("product_name", product.Name),
zap.Any("panic_value", r),
)
// 将panic转换为error而不是重新抛出
if e, ok := r.(error); ok {
err = fmt.Errorf("PDF生成panic: %w", e)
@@ -169,7 +178,7 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
var buf bytes.Buffer
err = pdf.Output(&buf)
if err != nil {
g.logger.Error("PDF输出失败", zap.Error(err))
// 错误已返回,不记录日志
return nil, fmt.Errorf("生成PDF失败: %w", err)
}
@@ -181,3 +190,4 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
return pdfBytes, nil
}