This commit is contained in:
2026-01-16 18:37:47 +08:00
parent 96dfa3d758
commit 96d530a67d
12 changed files with 959 additions and 156 deletions

View File

@@ -1,102 +0,0 @@
package component_report
import (
"net/http"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
finance_entities "tyapi-server/internal/domains/finance/entities"
)
// CheckPaymentStatusFixed 修复版检查支付状态方法
func (h *ComponentReportHandler) CheckPaymentStatusFixed(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"code": 401,
"message": "用户未登录",
})
return
}
orderID := c.Param("orderId")
if orderID == "" {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "订单ID不能为空",
})
return
}
// 根据订单ID查询下载记录
download, err := h.componentReportRepo.GetDownloadByID(c.Request.Context(), orderID)
if err != nil {
h.logger.Error("查询下载记录失败", zap.Error(err), zap.String("order_id", orderID))
c.JSON(http.StatusNotFound, gin.H{
"code": 404,
"message": "订单不存在",
})
return
}
// 验证订单是否属于当前用户
if download.UserID != userID {
c.JSON(http.StatusForbidden, gin.H{
"code": 403,
"message": "无权访问此订单",
})
return
}
// 使用购买订单状态来判断支付状态
var paymentStatus string
var canDownload bool
// 优先使用OrderID查询购买订单状态
if download.OrderID != nil {
// 查询购买订单状态
purchaseOrder, err := h.purchaseOrderRepo.GetByID(c.Request.Context(), *download.OrderID)
if err != nil {
h.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", *download.OrderID))
paymentStatus = "unknown"
} else {
// 根据购买订单状态设置支付状态
switch purchaseOrder.Status {
case finance_entities.PurchaseOrderStatusPaid:
paymentStatus = "success"
canDownload = true
case finance_entities.PurchaseOrderStatusCreated:
paymentStatus = "pending"
canDownload = false
case finance_entities.PurchaseOrderStatusCancelled:
paymentStatus = "cancelled"
canDownload = false
case finance_entities.PurchaseOrderStatusFailed:
paymentStatus = "failed"
canDownload = false
default:
paymentStatus = "unknown"
canDownload = false
}
}
} else if download.OrderNumber != nil {
// 兼容旧的支付订单逻辑
paymentStatus = "success" // 简化处理,有支付订单号就认为已支付
canDownload = true
} else {
paymentStatus = "pending"
canDownload = false
}
// 检查是否过期
if download.IsExpired() {
canDownload = false
}
c.JSON(http.StatusOK, CheckPaymentStatusResponse{
OrderID: download.ID,
PaymentStatus: paymentStatus,
CanDownload: canDownload,
})
}

View File

@@ -300,7 +300,7 @@ func (h *ComponentReportHandler) GenerateAndDownloadZip(c *gin.Context) {
return
}
// 直接检查用户是否有已支付的购买记录,而不是查询下载记录
// 直接检查用户是否有已支付的购买记录
orders, _, err := h.purchaseOrderRepo.GetByUserID(c.Request.Context(), userID, 100, 0)
if err != nil {
h.logger.Error("查询用户购买记录失败", zap.Error(err), zap.String("user_id", userID), zap.String("product_id", req.ProductID))
@@ -384,10 +384,12 @@ func (h *ComponentReportHandler) GenerateAndDownloadZip(c *gin.Context) {
}
// 更新下载次数和最后下载时间
err = h.componentReportRepo.IncrementDownloadCount(c.Request.Context(), download.ID)
if err != nil {
h.logger.Warn("更新下载次数失败", zap.Error(err))
// 不影响下载流程,只记录警告
if download != nil {
err = h.componentReportRepo.IncrementDownloadCount(c.Request.Context(), download.ID)
if err != nil {
h.logger.Warn("更新下载次数失败", zap.Error(err))
// 不影响下载流程,只记录警告
}
}
// 清理过期的缓存(非阻塞方式)
@@ -979,6 +981,13 @@ func (h *ComponentReportHandler) CreatePaymentOrder(c *gin.Context) {
}
// 创建下载记录
// 设置原始价格组合包使用UIComponentPrice单品使用Price
var originalPrice decimal.Decimal
if product.IsPackage {
originalPrice = product.UIComponentPrice
} else {
originalPrice = product.Price
}
download := &entities.ComponentReportDownload{
UserID: userID,
ProductID: productID,
@@ -989,7 +998,8 @@ func (h *ComponentReportHandler) CreatePaymentOrder(c *gin.Context) {
// 关联购买订单ID
OrderID: &createdPurchaseOrder.ID,
OrderNumber: &outTradeNo,
DownloadPrice: finalPrice, // 设置下载价格
OriginalPrice: originalPrice, // 设置原始价格
DownloadPrice: finalPrice, // 设置下载价格
}
// 记录创建前的详细信息用于调试
@@ -1384,6 +1394,13 @@ func (h *ComponentReportHandler) createDownloadRecordIfEligible(ctx context.Cont
}
// 3. 创建下载记录
// 设置原始价格组合包使用UIComponentPrice单品使用Price
var originalPrice decimal.Decimal
if product.IsPackage {
originalPrice = product.UIComponentPrice
} else {
originalPrice = product.Price
}
download := &entities.ComponentReportDownload{
UserID: userID,
ProductID: productID,
@@ -1391,6 +1408,7 @@ func (h *ComponentReportHandler) createDownloadRecordIfEligible(ctx context.Cont
ProductName: product.Name,
OrderID: &validOrder.ID, // 添加OrderID字段
OrderNumber: &validOrder.OrderNo, // 使用OrderNumber字段
OriginalPrice: originalPrice, // 设置原始价格
DownloadPrice: validOrder.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(), // 从创建日起30天
}
@@ -1563,6 +1581,13 @@ func (h *ComponentReportHandler) createDownloadRecordForPaidOrder(ctx context.Co
}
// 创建下载记录
// 设置原始价格组合包使用UIComponentPrice单品使用Price
var originalPrice decimal.Decimal
if product.IsPackage {
originalPrice = product.UIComponentPrice
} else {
originalPrice = product.Price
}
download := &entities.ComponentReportDownload{
UserID: order.UserID,
ProductID: order.ProductID,
@@ -1570,7 +1595,8 @@ func (h *ComponentReportHandler) createDownloadRecordForPaidOrder(ctx context.Co
ProductName: order.ProductName,
OrderID: &order.ID,
OrderNumber: &order.OrderNo,
DownloadPrice: order.Amount, // 设置下载价格(从订单获取)
OriginalPrice: originalPrice, // 设置原始价格
DownloadPrice: order.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(),
}

View File

@@ -124,39 +124,30 @@ func (h *ComponentReportHandlerFixed) CheckPaymentStatusFixed(c *gin.Context) {
var paymentStatus string
var canDownload bool
if download.OrderID != nil {
// 查询购买订单状态
purchaseOrder, err := h.purchaseOrderRepo.GetByID(c.Request.Context(), *download.OrderID)
if err != nil {
h.logger.Error("查询购买订单失败", zap.Error(err), zap.String("OrderID", *download.OrderID))
paymentStatus = "unknown"
} else {
// 根据购买订单状态设置支付状态
switch purchaseOrder.Status {
case finance_entities.PurchaseOrderStatusPaid:
paymentStatus = "success"
canDownload = true
case finance_entities.PurchaseOrderStatusCreated:
paymentStatus = "pending"
canDownload = false
case finance_entities.PurchaseOrderStatusCancelled:
paymentStatus = "cancelled"
canDownload = false
case finance_entities.PurchaseOrderStatusFailed:
paymentStatus = "failed"
canDownload = false
default:
paymentStatus = "unknown"
canDownload = false
}
}
} else if download.OrderNumber != nil {
// 兼容旧的支付订单逻辑
paymentStatus = "success" // 简化处理,有支付订单号就认为已支付
canDownload = true
// 查询购买订单状态
purchaseOrder, err := h.purchaseOrderRepo.GetByID(c.Request.Context(), *download.OrderID)
if err != nil {
h.logger.Error("查询购买订单失败", zap.Error(err), zap.String("OrderID", *download.OrderID))
paymentStatus = "unknown"
} else {
paymentStatus = "pending"
canDownload = false
// 根据购买订单状态设置支付状态
switch purchaseOrder.Status {
case finance_entities.PurchaseOrderStatusPaid:
paymentStatus = "success"
canDownload = true
case finance_entities.PurchaseOrderStatusCreated:
paymentStatus = "pending"
canDownload = false
case finance_entities.PurchaseOrderStatusCancelled:
paymentStatus = "cancelled"
canDownload = false
case finance_entities.PurchaseOrderStatusFailed:
paymentStatus = "failed"
canDownload = false
default:
paymentStatus = "unknown"
canDownload = false
}
}
// 检查是否过期