From 5ee816d6c1e5b55bfdf620a06280b51423c332c3 Mon Sep 17 00:00:00 2001 From: Mrx <18278715334@163.com> Date: Fri, 29 May 2026 21:13:46 +0800 Subject: [PATCH] fix --- .../finance/invoice_application_service.go | 74 ++++++++++++------- .../external/storage/qiniu_storage_service.go | 9 +++ .../http/handlers/finance_handler.go | 4 + 3 files changed, 61 insertions(+), 26 deletions(-) diff --git a/internal/application/finance/invoice_application_service.go b/internal/application/finance/invoice_application_service.go index 9c27e3d..5e9ca61 100644 --- a/internal/application/finance/invoice_application_service.go +++ b/internal/application/finance/invoice_application_service.go @@ -391,27 +391,56 @@ func (s *InvoiceApplicationServiceImpl) DownloadInvoiceFile(ctx context.Context, return nil, fmt.Errorf("发票尚未通过审核") } - // 4. 验证文件信息 - if application.FileURL == nil || *application.FileURL == "" { - return nil, fmt.Errorf("发票文件不存在") - } - - // 5. 从七牛云下载文件内容 - fileContent, err := s.storageService.DownloadFile(ctx, *application.FileURL) + fileContent, fileID, fileName, fileSize, fileURL, err := downloadInvoiceFileFromStorage(ctx, s.storageService, application) if err != nil { - return nil, fmt.Errorf("下载文件失败: %w", err) + return nil, err } - // 6. 构建响应DTO return &dto.FileDownloadResponse{ - FileID: *application.FileID, - FileName: *application.FileName, - FileSize: *application.FileSize, - FileURL: *application.FileURL, + FileID: fileID, + FileName: fileName, + FileSize: fileSize, + FileURL: fileURL, FileContent: fileContent, }, nil } +// downloadInvoiceFileFromStorage 从七牛云下载发票文件(优先用 FileID 生成新 URL,避免私有空间签名过期) +func downloadInvoiceFileFromStorage(ctx context.Context, storageService *storage.QiNiuStorageService, application *entities.InvoiceApplication) ([]byte, string, string, int64, string, error) { + if application.FileID == nil || *application.FileID == "" { + if application.FileURL == nil || *application.FileURL == "" { + return nil, "", "", 0, "", fmt.Errorf("发票文件不存在") + } + fileContent, err := storageService.DownloadFile(ctx, *application.FileURL) + if err != nil { + return nil, "", "", 0, "", fmt.Errorf("下载文件失败: %w", err) + } + return fileContent, derefString(application.FileID), derefString(application.FileName), derefInt64(application.FileSize), *application.FileURL, nil + } + + fileContent, err := storageService.DownloadFileByKey(ctx, *application.FileID) + if err != nil { + return nil, "", "", 0, "", fmt.Errorf("下载文件失败: %w", err) + } + + fileURL := storageService.GetFileURL(ctx, *application.FileID) + return fileContent, *application.FileID, derefString(application.FileName), derefInt64(application.FileSize), fileURL, nil +} + +func derefString(v *string) string { + if v == nil { + return "" + } + return *v +} + +func derefInt64(v *int64) int64 { + if v == nil { + return 0 + } + return *v +} + // GetAvailableAmount 获取可开票金额 func (s *InvoiceApplicationServiceImpl) GetAvailableAmount(ctx context.Context, userID string) (*dto.AvailableAmountResponse, error) { // 1. 验证用户是否存在 @@ -764,23 +793,16 @@ func (s *AdminInvoiceApplicationServiceImpl) DownloadInvoiceFile(ctx context.Con return nil, fmt.Errorf("发票尚未通过审核") } - // 3. 验证文件信息 - if application.FileURL == nil || *application.FileURL == "" { - return nil, fmt.Errorf("发票文件不存在") - } - - // 4. 从七牛云下载文件内容 - fileContent, err := s.storageService.DownloadFile(ctx, *application.FileURL) + fileContent, fileID, fileName, fileSize, fileURL, err := downloadInvoiceFileFromStorage(ctx, s.storageService, application) if err != nil { - return nil, fmt.Errorf("下载文件失败: %w", err) + return nil, err } - // 5. 构建响应DTO return &dto.FileDownloadResponse{ - FileID: *application.FileID, - FileName: *application.FileName, - FileSize: *application.FileSize, - FileURL: *application.FileURL, + FileID: fileID, + FileName: fileName, + FileSize: fileSize, + FileURL: fileURL, FileContent: fileContent, }, nil } diff --git a/internal/infrastructure/external/storage/qiniu_storage_service.go b/internal/infrastructure/external/storage/qiniu_storage_service.go index 73d1749..129e6ac 100644 --- a/internal/infrastructure/external/storage/qiniu_storage_service.go +++ b/internal/infrastructure/external/storage/qiniu_storage_service.go @@ -281,6 +281,15 @@ func (s *QiNiuStorageService) UploadFromReader(ctx context.Context, reader io.Re return s.UploadFile(ctx, fileBytes, fileName) } +// DownloadFileByKey 通过存储 key 下载文件(每次生成最新访问 URL,避免私有空间签名过期) +func (s *QiNiuStorageService) DownloadFileByKey(ctx context.Context, key string) ([]byte, error) { + if key == "" { + return nil, fmt.Errorf("文件 key 不能为空") + } + fileURL := s.GetFileURL(ctx, key) + return s.DownloadFile(ctx, fileURL) +} + // DownloadFile 从七牛云下载文件 func (s *QiNiuStorageService) DownloadFile(ctx context.Context, fileURL string) ([]byte, error) { s.logger.Info("开始从七牛云下载文件", zap.String("file_url", fileURL)) diff --git a/internal/infrastructure/http/handlers/finance_handler.go b/internal/infrastructure/http/handlers/finance_handler.go index c297923..b82e1d6 100644 --- a/internal/infrastructure/http/handlers/finance_handler.go +++ b/internal/infrastructure/http/handlers/finance_handler.go @@ -1070,6 +1070,10 @@ func (h *FinanceHandler) AdminDownloadInvoiceFile(c *gin.Context) { result, err := h.adminInvoiceAppService.DownloadInvoiceFile(c.Request.Context(), applicationID) if err != nil { + h.logger.Error("下载发票文件失败", + zap.String("application_id", applicationID), + zap.Error(err), + ) h.responseBuilder.InternalError(c, "下载发票文件失败") return }