fix Authorization

This commit is contained in:
2025-11-11 17:47:08 +08:00
parent 64c0a10193
commit c40be9daf8
10 changed files with 326 additions and 29 deletions

View File

@@ -5,8 +5,10 @@ import (
"context"
"database/sql"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"tydata-server/app/main/api/internal/config"
"tydata-server/app/main/model"
@@ -25,11 +27,13 @@ type AuthorizationService struct {
// NewAuthorizationService 创建授权书服务实例
func NewAuthorizationService(c config.Config, authDocModel model.AuthorizationDocumentModel) *AuthorizationService {
absStoragePath := determineStoragePath()
return &AuthorizationService{
config: c,
authDocModel: authDocModel,
fileStoragePath: "data/authorization_docs", // 使用相对路径,兼容开发环境
fileBaseURL: c.Authorization.FileBaseURL, // 从配置文件读取
fileStoragePath: absStoragePath,
fileBaseURL: c.Authorization.FileBaseURL, // 从配置文件读取
}
}
@@ -101,6 +105,109 @@ func (s *AuthorizationService) GetFullFileURL(relativePath string) string {
return fmt.Sprintf("%s/%s", s.fileBaseURL, relativePath)
}
// ResolveFilePath 根据存储的路径信息解析出本地文件的绝对路径
func (s *AuthorizationService) ResolveFilePath(filePath string, relativePath string) string {
candidates := []string{}
cleanRelative := func(p string) string {
p = strings.TrimPrefix(p, "/")
p = strings.TrimPrefix(p, "\\")
normalized := filepath.ToSlash(p)
if strings.HasPrefix(normalized, "http://") || strings.HasPrefix(normalized, "https://") {
if parsed, err := url.Parse(normalized); err == nil {
normalized = filepath.ToSlash(strings.TrimPrefix(parsed.Path, "/"))
}
}
normalized = strings.TrimPrefix(normalized, "data/authorization_docs/")
normalized = strings.TrimPrefix(normalized, "authorization_docs/")
normalized = strings.TrimPrefix(normalized, "./")
normalized = strings.TrimPrefix(normalized, "../")
return normalized
}
if filePath != "" {
candidates = append(candidates, filePath)
}
if relativePath != "" {
candidates = append(candidates, filepath.Join(s.fileStoragePath, filepath.FromSlash(cleanRelative(relativePath))))
}
if filePath != "" {
cleaned := filepath.Clean(filePath)
if strings.HasPrefix(cleaned, s.fileStoragePath) {
candidates = append(candidates, cleaned)
} else {
trimmed := strings.TrimPrefix(cleaned, "data"+string(os.PathSeparator)+"authorization_docs"+string(os.PathSeparator))
trimmed = strings.TrimPrefix(trimmed, "data/authorization_docs/")
if trimmed != cleaned {
candidates = append(candidates, filepath.Join(s.fileStoragePath, filepath.FromSlash(cleanRelative(trimmed))))
}
if !filepath.IsAbs(cleaned) {
candidates = append(candidates, filepath.Join(s.fileStoragePath, filepath.FromSlash(cleanRelative(cleaned))))
}
}
}
for _, candidate := range candidates {
if candidate == "" {
continue
}
pathCandidate := candidate
if !filepath.IsAbs(pathCandidate) {
if absPath, err := filepath.Abs(pathCandidate); err == nil {
pathCandidate = absPath
}
}
if statErr := checkFileExists(pathCandidate); statErr == nil {
return pathCandidate
}
}
logx.Errorf("授权书文件路径解析失败 filePath=%s fileUrl=%s candidates=%v", filePath, relativePath, candidates)
return ""
}
func determineStoragePath() string {
candidatePaths := []string{
"data/authorization_docs",
"../data/authorization_docs",
"../../data/authorization_docs",
"../../../data/authorization_docs",
}
for _, candidate := range candidatePaths {
absPath, err := filepath.Abs(candidate)
if err != nil {
logx.Errorf("解析授权书存储路径失败: %s, err=%v", candidate, err)
continue
}
if info, err := os.Stat(absPath); err == nil && info.IsDir() {
logx.Infof("授权书存储路径选择: %s", absPath)
return absPath
}
}
// 如果没有现成的目录,使用第一个候选的绝对路径
absPath, err := filepath.Abs(candidatePaths[0])
if err != nil {
logx.Errorf("解析默认授权书存储路径失败,使用相对路径: %s, err=%v", candidatePaths[0], err)
return candidatePaths[0]
}
logx.Infof("授权书存储路径创建: %s", absPath)
return absPath
}
func checkFileExists(path string) error {
info, err := os.Stat(path)
if err != nil {
return err
}
if info.IsDir() {
return fmt.Errorf("path %s is directory", path)
}
return nil
}
// generatePDFContent 生成PDF内容
func (s *AuthorizationService) generatePDFContent(userInfo map[string]interface{}) ([]byte, error) {
// 创建PDF文档
@@ -113,7 +220,7 @@ func (s *AuthorizationService) generatePDFContent(userInfo map[string]interface{
"/app/static/SIMHEI.TTF", // Docker容器内的字体文件
"app/main/api/static/SIMHEI.TTF", // 开发环境备用路径
}
// 尝试添加字体
fontAdded := false
for _, fontPath := range fontPaths {
@@ -150,17 +257,17 @@ func (s *AuthorizationService) generatePDFContent(userInfo map[string]interface{
pdf.SetFont("Arial", "", 20)
}
pdf.CellFormat(0, 15, "授权书", "", 1, "C", false, 0, "")
// 添加空行
pdf.Ln(5)
// 设置正文样式 - 正常字体
if fontAdded {
pdf.SetFont("ChineseFont", "", 12)
} else {
pdf.SetFont("Arial", "", 12)
}
// 构建授权书内容(去掉标题部分)
content := fmt.Sprintf(`海南天远大数据科技有限公司:
本人%s拟向贵司申请大数据分析报告查询业务贵司需要了解本人相关状况用于查询大数据分析报告因此本人同意向贵司提供本人的姓名和手机号等个人信息并同意贵司向第三方传送上述信息。第三方将使用上述信息核实信息真实情况查询信用记录并生成报告。