This commit is contained in:
2026-01-14 15:59:15 +08:00
parent 7a589a9c13
commit 6618e35869
3 changed files with 93 additions and 66 deletions

View File

@@ -571,6 +571,7 @@ func (s *ComponentReportOrderService) createFreeOrder(
ProductName: product.Name, ProductName: product.Name,
OrderID: &createdPurchaseOrder.ID, // 关联购买订单ID OrderID: &createdPurchaseOrder.ID, // 关联购买订单ID
OrderNumber: &createdPurchaseOrder.OrderNo, // 外部订单号 OrderNumber: &createdPurchaseOrder.OrderNo, // 外部订单号
DownloadPrice: finalPrice, // 设置下载价格
ExpiresAt: calculateExpiryTime(), // 30天后过期 ExpiresAt: calculateExpiryTime(), // 30天后过期
} }
@@ -785,6 +786,7 @@ func (s *ComponentReportOrderService) DownloadFile(ctx context.Context, orderID
ProductName: product.Name, ProductName: product.Name,
OrderID: &purchaseOrder.ID, OrderID: &purchaseOrder.ID,
OrderNumber: &purchaseOrder.OrderNo, OrderNumber: &purchaseOrder.OrderNo,
DownloadPrice: purchaseOrder.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(), ExpiresAt: calculateExpiryTime(),
} }
@@ -924,6 +926,7 @@ func (s *ComponentReportOrderService) createDownloadRecordForPaidOrder(ctx conte
ProductName: product.Name, ProductName: product.Name,
OrderID: &purchaseOrder.ID, OrderID: &purchaseOrder.ID,
OrderNumber: &purchaseOrder.OrderNo, OrderNumber: &purchaseOrder.OrderNo,
DownloadPrice: purchaseOrder.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(), // 30天后过期 ExpiresAt: calculateExpiryTime(), // 30天后过期
} }

View File

@@ -4,6 +4,7 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/shopspring/decimal"
"gorm.io/gorm" "gorm.io/gorm"
) )
@@ -22,6 +23,9 @@ type ComponentReportDownload struct {
SubProductIDs string `gorm:"type:text" comment:"子产品ID列表JSON数组组合包使用"` SubProductIDs string `gorm:"type:text" comment:"子产品ID列表JSON数组组合包使用"`
SubProductCodes string `gorm:"type:text" comment:"子产品编号列表JSON数组"` SubProductCodes string `gorm:"type:text" comment:"子产品编号列表JSON数组"`
// 价格相关字段
DownloadPrice decimal.Decimal `gorm:"type:decimal(10,2);not null" comment:"实际支付价格"`
// 下载相关信息 // 下载相关信息
FilePath *string `gorm:"type:varchar(500)" comment:"生成的ZIP文件路径用于二次下载"` FilePath *string `gorm:"type:varchar(500)" comment:"生成的ZIP文件路径用于二次下载"`
FileHash *string `gorm:"type:varchar(64)" comment:"文件哈希值(用于缓存验证)"` FileHash *string `gorm:"type:varchar(64)" comment:"文件哈希值(用于缓存验证)"`

View File

@@ -397,9 +397,14 @@ func (h *ComponentReportHandler) GenerateAndDownloadZip(c *gin.Context) {
} }
}() }()
// 创建带超时的上下文避免ZIP生成时间过长导致网关超时
// 设置超时时间为 25 秒,略小于服务器的 write_timeout (30秒)
ctx, cancel := context.WithTimeout(c.Request.Context(), 25*time.Second)
defer cancel()
// 生成ZIP文件 // 生成ZIP文件
zipPath, err := h.zipGenerator.GenerateZipFile( zipPath, err := h.zipGenerator.GenerateZipFile(
c.Request.Context(), ctx,
req.ProductID, req.ProductID,
req.SubProductCodes, req.SubProductCodes,
h.exampleJSONGenerator, h.exampleJSONGenerator,
@@ -407,6 +412,18 @@ func (h *ComponentReportHandler) GenerateAndDownloadZip(c *gin.Context) {
) )
if err != nil { if err != nil {
h.logger.Error("生成ZIP文件失败", zap.Error(err), zap.String("product_id", req.ProductID)) h.logger.Error("生成ZIP文件失败", zap.Error(err), zap.String("product_id", req.ProductID))
// 检查是否是超时错误
if ctx.Err() == context.DeadlineExceeded {
h.logger.Error("ZIP文件生成超时", zap.String("product_id", req.ProductID))
c.JSON(http.StatusRequestTimeout, gin.H{
"code": 504,
"message": "文件生成超时,请稍后重试",
"error": "请求处理时间过长",
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"code": 500, "code": 500,
"message": "生成ZIP文件失败", "message": "生成ZIP文件失败",
@@ -989,6 +1006,7 @@ func (h *ComponentReportHandler) CreatePaymentOrder(c *gin.Context) {
// 关联购买订单ID // 关联购买订单ID
OrderID: &createdPurchaseOrder.ID, OrderID: &createdPurchaseOrder.ID,
OrderNumber: &outTradeNo, OrderNumber: &outTradeNo,
DownloadPrice: finalPrice, // 设置下载价格
} }
// 记录创建前的详细信息用于调试 // 记录创建前的详细信息用于调试
@@ -1390,6 +1408,7 @@ func (h *ComponentReportHandler) createDownloadRecordIfEligible(ctx context.Cont
ProductName: product.Name, ProductName: product.Name,
OrderID: &validOrder.ID, // 添加OrderID字段 OrderID: &validOrder.ID, // 添加OrderID字段
OrderNumber: &validOrder.OrderNo, // 使用OrderNumber字段 OrderNumber: &validOrder.OrderNo, // 使用OrderNumber字段
DownloadPrice: validOrder.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(), // 从创建日起30天 ExpiresAt: calculateExpiryTime(), // 从创建日起30天
} }
@@ -1568,6 +1587,7 @@ func (h *ComponentReportHandler) createDownloadRecordForPaidOrder(ctx context.Co
ProductName: order.ProductName, ProductName: order.ProductName,
OrderID: &order.ID, OrderID: &order.ID,
OrderNumber: &order.OrderNo, OrderNumber: &order.OrderNo,
DownloadPrice: order.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(), ExpiresAt: calculateExpiryTime(),
} }