This commit is contained in:
2025-12-04 12:56:39 +08:00
parent 4ce8fe4023
commit f12c3fb8ad
12 changed files with 86 additions and 227 deletions

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"tyapi-server/internal/domains/product/entities"
@@ -55,50 +54,20 @@ func NewPDFGeneratorRefactored(logger *zap.Logger) *PDFGeneratorRefactored {
return gen
}
// findLogo 查找logo文件
// findLogo 查找logo文件仅从resources/pdf加载
func (g *PDFGeneratorRefactored) findLogo() {
// 获取当前文件所在目录
_, filename, _, _ := runtime.Caller(0)
baseDir := filepath.Dir(filename)
// 获取resources/pdf目录使用统一的资源路径查找函数
resourcesPDFDir := GetResourcesPDFDir()
logoPath := filepath.Join(resourcesPDFDir, "logo.png")
// 优先使用 baseDir最可靠基于当前文件位置
logoPaths := []string{
filepath.Join(baseDir, "天远数据.png"), // 相对当前文件(最优先)
}
// 尝试相对于工作目录的路径
if workDir, err := os.Getwd(); err == nil {
logoPaths = append(logoPaths,
filepath.Join(workDir, "internal", "shared", "pdf", "天远数据.png"),
filepath.Join(workDir, "tyapi-server", "internal", "shared", "pdf", "天远数据.png"),
)
}
// 尝试服务器绝对路径Linux环境优先查找 /www/tyapi-server
if runtime.GOOS == "linux" {
serverPaths := []string{
"/www/tyapi-server/internal/shared/pdf/天远数据.png",
"/app/internal/shared/pdf/天远数据.png",
}
logoPaths = append(logoPaths, serverPaths...)
}
// 尝试相对路径(作为最后的后备方案)
logoPaths = append(logoPaths,
"internal/shared/pdf/天远数据.png",
"./internal/shared/pdf/天远数据.png",
)
// 尝试所有路径
for _, logoPath := range logoPaths {
if _, err := os.Stat(logoPath); err == nil {
g.logoPath = logoPath
return
}
// 检查文件是否存在
if _, err := os.Stat(logoPath); err == nil {
g.logoPath = logoPath
return
}
// 只记录关键错误
g.logger.Warn("未找到logo文件")
g.logger.Warn("未找到logo文件", zap.String("path", logoPath))
}
// GenerateProductPDF 为产品生成PDF文档接受响应类型内部转换