This commit is contained in:
2025-12-24 12:32:25 +08:00
parent ce45ce3ed0
commit 311d7a9b01
7 changed files with 350 additions and 134 deletions

View File

@@ -524,8 +524,9 @@ func (s *ComponentReportOrderService) CreatePaymentOrder(ctx context.Context, re
}
// 返回支付响应包含支付URL
// 使用购买订单ID而不是下载记录ID以便前端可以正确检查支付状态
response := &CreatePaymentOrderResponse{
OrderID: download.ID,
OrderID: createdPurchaseOrder.ID, // 修改为购买订单ID
OrderNo: createdPurchaseOrder.OrderNo,
PaymentType: req.PaymentType,
Amount: finalPrice.String(),
@@ -601,7 +602,7 @@ func (s *ComponentReportOrderService) createFreeOrder(
}
return &CreatePaymentOrderResponse{
OrderID: download.ID,
OrderID: createdPurchaseOrder.ID, // 修改为购买订单ID
OrderNo: createdPurchaseOrder.OrderNo,
PaymentType: "free",
Amount: "0.00",
@@ -650,106 +651,90 @@ func (s *ComponentReportOrderService) CheckPaymentStatus(ctx context.Context, or
zap.Time("check_time", time.Now()),
)
// 获取下载记录信息
download, err := s.componentReportRepo.GetDownloadByID(ctx, orderID)
// 直接查询购买订单
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, orderID)
if err != nil {
s.logger.Error("获取下载记录信息失败",
zap.String("order_id", orderID),
zap.Error(err))
return nil, fmt.Errorf("获取下载记录信息失败: %w", err)
s.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", orderID))
return &CheckPaymentStatusResponse{
OrderID: orderID,
PaymentStatus: "unknown",
CanDownload: false,
}, nil
}
s.logger.Info("下载记录信息",
zap.String("order_id", orderID),
zap.String("user_id", download.UserID),
zap.String("product_id", download.ProductID),
zap.String("product_code", download.ProductCode),
zap.String("product_name", download.ProductName),
zap.Any("order_id", download.OrderID),
zap.Any("order_number", download.OrderNumber),
)
// 如果订单不存在,返回未知状态
if purchaseOrder == nil {
s.logger.Error("购买订单不存在", zap.String("order_id", orderID))
return &CheckPaymentStatusResponse{
OrderID: orderID,
PaymentStatus: "unknown",
CanDownload: false,
}, nil
}
// 使用OrderID查询购买订单状态来判断支付状态
// 如果购买订单状态是 Created待支付需要主动查询支付状态
if purchaseOrder.Status == entities.PurchaseOrderStatusCreated {
s.logger.Info("购买订单状态为待支付,主动查询支付状态",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.String("order_no", purchaseOrder.OrderNo),
zap.String("pay_channel", purchaseOrder.PayChannel),
zap.String("payment_type", purchaseOrder.PaymentType),
zap.String("amount", purchaseOrder.Amount.String()),
zap.Time("query_start_time", time.Now()))
// 根据支付渠道查询支付状态
if purchaseOrder.PayChannel == "alipay" && s.aliPayService != nil {
s.logger.Info("开始查询支付宝订单状态",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.String("order_no", purchaseOrder.OrderNo))
err = s.queryAlipayOrderStatusAndUpdate(ctx, purchaseOrder.ID)
if err != nil {
s.logger.Error("查询支付宝订单状态失败",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.Error(err))
}
} else if purchaseOrder.PayChannel == "wechat" && s.wechatPayService != nil {
s.logger.Info("开始查询微信订单状态",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.String("order_no", purchaseOrder.OrderNo))
err = s.queryWechatOrderStatusAndUpdate(ctx, purchaseOrder.ID)
if err != nil {
s.logger.Error("查询微信订单状态失败",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.Error(err))
}
}
// 重新获取更新后的购买订单信息
updatedOrder, err := s.purchaseOrderRepo.GetByID(ctx, purchaseOrder.ID)
if err == nil && updatedOrder != nil {
s.logger.Info("获取更新后的购买订单信息",
zap.String("purchase_order_id", updatedOrder.ID),
zap.String("order_no", updatedOrder.OrderNo),
zap.String("status", string(updatedOrder.Status)))
purchaseOrder = updatedOrder
}
}
// 根据购买订单状态设置支付状态
var paymentStatus string
var canDownload bool
if download.OrderID != nil {
// 查询购买订单状态
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, *download.OrderID)
if err != nil {
s.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", *download.OrderID))
paymentStatus = "unknown"
} else {
// 如果购买订单状态是 Created待支付需要主动查询支付状态
if purchaseOrder.Status == entities.PurchaseOrderStatusCreated {
s.logger.Info("购买订单状态为待支付,主动查询支付状态",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.String("order_no", purchaseOrder.OrderNo),
zap.String("pay_channel", purchaseOrder.PayChannel),
zap.String("payment_type", purchaseOrder.PaymentType),
zap.String("amount", purchaseOrder.Amount.String()),
zap.Time("query_start_time", time.Now()))
// 根据支付渠道查询支付状态
if purchaseOrder.PayChannel == "alipay" && s.aliPayService != nil {
s.logger.Info("开始查询支付宝订单状态",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.String("order_no", purchaseOrder.OrderNo))
err = s.queryAlipayOrderStatusAndUpdate(ctx, purchaseOrder.ID)
if err != nil {
s.logger.Error("查询支付宝订单状态失败",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.Error(err))
}
} else if purchaseOrder.PayChannel == "wechat" && s.wechatPayService != nil {
s.logger.Info("开始查询微信订单状态",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.String("order_no", purchaseOrder.OrderNo))
err = s.queryWechatOrderStatusAndUpdate(ctx, purchaseOrder.ID)
if err != nil {
s.logger.Error("查询微信订单状态失败",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.Error(err))
}
}
// 重新获取更新后的购买订单信息
updatedOrder, err := s.purchaseOrderRepo.GetByID(ctx, *download.OrderID)
if err == nil && updatedOrder != nil {
s.logger.Info("获取更新后的购买订单信息",
zap.String("purchase_order_id", updatedOrder.ID),
zap.String("order_no", updatedOrder.OrderNo),
zap.String("status", string(updatedOrder.Status)))
purchaseOrder = updatedOrder
}
}
// 根据购买订单状态设置支付状态
switch purchaseOrder.Status {
case entities.PurchaseOrderStatusPaid:
paymentStatus = "success"
canDownload = true
case entities.PurchaseOrderStatusCreated:
paymentStatus = "pending"
canDownload = false
case entities.PurchaseOrderStatusCancelled:
paymentStatus = "cancelled"
canDownload = false
case entities.PurchaseOrderStatusFailed:
paymentStatus = "failed"
canDownload = false
default:
paymentStatus = "unknown"
canDownload = false
}
}
} else {
switch purchaseOrder.Status {
case entities.PurchaseOrderStatusPaid:
paymentStatus = "success"
canDownload = true
case entities.PurchaseOrderStatusCreated:
paymentStatus = "pending"
canDownload = false
}
// 检查是否过期
if download.IsExpired() {
case entities.PurchaseOrderStatusCancelled:
paymentStatus = "cancelled"
canDownload = false
case entities.PurchaseOrderStatusFailed:
paymentStatus = "failed"
canDownload = false
default:
paymentStatus = "unknown"
canDownload = false
}
@@ -762,7 +747,7 @@ func (s *ComponentReportOrderService) CheckPaymentStatus(ctx context.Context, or
)
return &CheckPaymentStatusResponse{
OrderID: download.ID,
OrderID: orderID,
PaymentStatus: paymentStatus,
CanDownload: canDownload,
}, nil
@@ -770,39 +755,81 @@ func (s *ComponentReportOrderService) CheckPaymentStatus(ctx context.Context, or
// DownloadFile 下载文件
func (s *ComponentReportOrderService) DownloadFile(ctx context.Context, orderID string) (string, error) {
// 获取下载记录信息
download, err := s.componentReportRepo.GetDownloadByID(ctx, orderID)
s.logger.Info("开始下载文件", zap.String("order_id", orderID))
// 首先通过orderID查询购买订单
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, orderID)
if err != nil {
return "", fmt.Errorf("获取下载记录信息失败: %w", err)
s.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", orderID))
return "", fmt.Errorf("查询购买订单失败: %w", err)
}
// 使用OrderID查询购买订单状态来判断支付状态
var canDownload bool
if purchaseOrder == nil {
s.logger.Error("购买订单不存在", zap.String("order_id", orderID))
return "", fmt.Errorf("购买订单不存在")
}
if download.OrderID != nil {
// 查询购买订单状态
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, *download.OrderID)
if err != nil {
s.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", *download.OrderID))
canDownload = false
} else {
// 检查购买订单状态
canDownload = purchaseOrder.Status == entities.PurchaseOrderStatusPaid
// 检查购买订单状态
if purchaseOrder.Status != entities.PurchaseOrderStatusPaid {
s.logger.Error("订单未支付,无法下载文件",
zap.String("order_id", orderID),
zap.String("status", string(purchaseOrder.Status)))
return "", fmt.Errorf("订单未支付,无法下载文件")
}
// 获取产品信息
product, err := s.productRepo.GetByID(ctx, purchaseOrder.ProductID)
if err != nil {
s.logger.Error("获取产品信息失败", zap.Error(err), zap.String("product_id", purchaseOrder.ProductID))
return "", fmt.Errorf("获取产品信息失败: %w", err)
}
// 检查是否已有下载记录
download, err := s.componentReportRepo.GetDownloadByPaymentOrderID(ctx, orderID)
if err != nil {
s.logger.Warn("查询下载记录失败,将创建新记录", zap.Error(err), zap.String("order_id", orderID))
download = nil
}
// 如果没有下载记录,创建一个新的
if download == nil {
s.logger.Info("创建新的下载记录",
zap.String("order_id", orderID),
zap.String("user_id", purchaseOrder.UserID),
zap.String("product_id", purchaseOrder.ProductID))
// 创建新的下载记录
newDownload := &productEntities.ComponentReportDownload{
UserID: purchaseOrder.UserID,
ProductID: purchaseOrder.ProductID,
ProductCode: product.Code,
ProductName: product.Name,
OrderID: &purchaseOrder.ID,
OrderNumber: &purchaseOrder.OrderNo,
ExpiresAt: calculateExpiryTime(),
}
} else if download.OrderNumber != nil {
// 兼容旧的支付订单逻辑
canDownload = true // 简化处理,有支付订单号就认为已支付
} else {
canDownload = false
// 保存下载记录
err = s.componentReportRepo.Create(ctx, newDownload)
if err != nil {
s.logger.Error("创建下载记录失败", zap.Error(err))
return "", fmt.Errorf("创建下载记录失败: %w", err)
}
s.logger.Info("成功创建下载记录",
zap.String("order_id", orderID),
zap.String("download_id", newDownload.ID),
zap.String("product_id", newDownload.ProductID))
download = newDownload
}
// 检查是否过期
if download.IsExpired() {
canDownload = false
}
if !canDownload {
return "", fmt.Errorf("订单未支付或已过期,无法下载文件")
s.logger.Error("下载链接已过期",
zap.String("order_id", orderID),
zap.Time("expires_at", *download.ExpiresAt))
return "", fmt.Errorf("下载链接已过期,无法下载文件")
}
// 检查文件是否已存在
@@ -815,8 +842,10 @@ func (s *ComponentReportOrderService) DownloadFile(ctx context.Context, orderID
}
// 文件不存在,生成文件
s.logger.Info("开始生成报告文件", zap.String("order_id", orderID))
filePath, err := s.generateReportFile(ctx, download)
if err != nil {
s.logger.Error("生成报告文件失败", zap.Error(err), zap.String("order_id", orderID))
return "", fmt.Errorf("生成报告文件失败: %w", err)
}
@@ -882,6 +911,60 @@ func (s *ComponentReportOrderService) GetUserOrders(ctx context.Context, userID
return result, int64(len(result)), nil
}
// createDownloadRecordForPaidOrder 为已支付订单创建下载记录
func (s *ComponentReportOrderService) createDownloadRecordForPaidOrder(ctx context.Context, purchaseOrder *entities.PurchaseOrder) error {
s.logger.Info("开始为已支付订单创建下载记录",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.String("order_no", purchaseOrder.OrderNo),
zap.String("user_id", purchaseOrder.UserID),
zap.String("product_id", purchaseOrder.ProductID))
// 检查是否已有下载记录
existingDownload, err := s.componentReportRepo.GetDownloadByPaymentOrderID(ctx, purchaseOrder.ID)
if err == nil && existingDownload != nil {
s.logger.Info("下载记录已存在,跳过创建",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.String("download_id", existingDownload.ID))
return nil
}
// 获取产品信息
product, err := s.productRepo.GetByID(ctx, purchaseOrder.ProductID)
if err != nil {
s.logger.Error("获取产品信息失败",
zap.Error(err),
zap.String("product_id", purchaseOrder.ProductID))
return fmt.Errorf("获取产品信息失败: %w", err)
}
// 创建新的下载记录
download := &productEntities.ComponentReportDownload{
UserID: purchaseOrder.UserID,
ProductID: purchaseOrder.ProductID,
ProductCode: product.Code,
ProductName: product.Name,
OrderID: &purchaseOrder.ID,
OrderNumber: &purchaseOrder.OrderNo,
ExpiresAt: calculateExpiryTime(), // 30天后过期
}
// 保存下载记录
err = s.componentReportRepo.Create(ctx, download)
if err != nil {
s.logger.Error("创建下载记录失败", zap.Error(err))
return fmt.Errorf("创建下载记录失败: %w", err)
}
s.logger.Info("成功为已支付订单创建下载记录",
zap.String("purchase_order_id", purchaseOrder.ID),
zap.String("order_no", purchaseOrder.OrderNo),
zap.String("download_id", download.ID),
zap.String("product_id", download.ProductID),
zap.String("user_id", download.UserID))
return nil
}
// calculateExpiryTime 计算下载有效期从创建日起30天
func calculateExpiryTime() *time.Time {
now := time.Now()
@@ -944,18 +1027,33 @@ func (s *ComponentReportOrderService) queryAlipayOrderStatusAndUpdate(ctx contex
zap.String("purchase_order_id", purchaseOrderID),
)
// 查询支付宝订单 - 使用RechargeID字段关联
alipayOrder, err := s.alipayOrderRepo.GetByRechargeID(ctx, purchaseOrderID)
// 首先获取购买订单信息
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, purchaseOrderID)
if err != nil {
s.logger.Error("查找购买订单失败",
zap.String("purchase_order_id", purchaseOrderID),
zap.Error(err))
return fmt.Errorf("查找购买订单失败: %w", err)
}
if purchaseOrder == nil {
s.logger.Error("购买订单不存在",
zap.String("purchase_order_id", purchaseOrderID))
return fmt.Errorf("购买订单不存在")
}
// 使用购买订单ID查询支付宝订单RechargeID字段存储的是购买订单ID
alipayOrder, err := s.alipayOrderRepo.GetByRechargeID(ctx, purchaseOrder.ID)
if err != nil {
s.logger.Error("查找支付宝订单失败",
zap.String("purchase_order_id", purchaseOrderID),
zap.String("purchase_order_id", purchaseOrder.ID),
zap.Error(err))
return fmt.Errorf("查找支付宝订单失败: %w", err)
}
if alipayOrder == nil {
s.logger.Error("支付宝订单不存在",
zap.String("purchase_order_id", purchaseOrderID))
zap.String("purchase_order_id", purchaseOrder.ID))
return fmt.Errorf("支付宝订单不存在")
}
@@ -1044,6 +1142,17 @@ func (s *ComponentReportOrderService) queryAlipayOrderStatusAndUpdate(ctx contex
zap.String("order_no", purchaseOrder.OrderNo),
zap.String("status", "paid"),
)
// 支付成功后,自动创建下载记录
err = s.createDownloadRecordForPaidOrder(ctx, purchaseOrder)
if err != nil {
s.logger.Error("创建下载记录失败",
zap.String("purchase_order_id", purchaseOrderID),
zap.Error(err))
} else {
s.logger.Info("自动创建下载记录成功",
zap.String("purchase_order_id", purchaseOrderID))
}
}
}
} else if alipayStatus == "TRADE_CLOSED" {
@@ -1086,18 +1195,33 @@ func (s *ComponentReportOrderService) queryWechatOrderStatusAndUpdate(ctx contex
zap.String("purchase_order_id", purchaseOrderID),
)
// 查询微信订单 - 使用RechargeID字段关联
wechatOrder, err := s.wechatOrderRepo.GetByRechargeID(ctx, purchaseOrderID)
// 首先获取购买订单信息
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, purchaseOrderID)
if err != nil {
s.logger.Error("查找购买订单失败",
zap.String("purchase_order_id", purchaseOrderID),
zap.Error(err))
return fmt.Errorf("查找购买订单失败: %w", err)
}
if purchaseOrder == nil {
s.logger.Error("购买订单不存在",
zap.String("purchase_order_id", purchaseOrderID))
return fmt.Errorf("购买订单不存在")
}
// 使用购买订单ID查询微信订单RechargeID字段存储的是购买订单ID
wechatOrder, err := s.wechatOrderRepo.GetByRechargeID(ctx, purchaseOrder.ID)
if err != nil {
s.logger.Error("查找微信订单失败",
zap.String("purchase_order_id", purchaseOrderID),
zap.String("purchase_order_id", purchaseOrder.ID),
zap.Error(err))
return fmt.Errorf("查找微信订单失败: %w", err)
}
if wechatOrder == nil {
s.logger.Error("微信订单不存在",
zap.String("purchase_order_id", purchaseOrderID))
zap.String("purchase_order_id", purchaseOrder.ID))
return fmt.Errorf("微信订单不存在")
}
@@ -1193,6 +1317,17 @@ func (s *ComponentReportOrderService) queryWechatOrderStatusAndUpdate(ctx contex
zap.String("order_no", purchaseOrder.OrderNo),
zap.String("status", "paid"),
)
// 支付成功后,自动创建下载记录
err = s.createDownloadRecordForPaidOrder(ctx, purchaseOrder)
if err != nil {
s.logger.Error("创建下载记录失败",
zap.String("purchase_order_id", purchaseOrderID),
zap.Error(err))
} else {
s.logger.Info("自动创建下载记录成功",
zap.String("purchase_order_id", purchaseOrderID))
}
}
}
} else if tradeState == "CLOSED" || tradeState == "REVOKED" {

View File

@@ -250,6 +250,10 @@ type QCXG7A2BReq struct {
IDCard string `json:"id_card" validate:"required,validIDCard"`
}
type QCXG4896Req struct {
PlateNo string `json:"plate_no" validate:"required"`
AuthDate string `json:"auth_date" validate:"required,validAuthDate" encrypt:"false"`
}
type COMENT01Req struct {
EntName string `json:"ent_name" validate:"required,min=1,validEnterpriseName"`
EntCode string `json:"ent_code" validate:"required,validUSCI"`

View File

@@ -217,6 +217,7 @@ func registerAllProcessors(combService *comb.CombService) {
"QCXG9P1C": qcxg.ProcessQCXG9P1CRequest,
"QCXG8A3D": qcxg.ProcessQCXG8A3DRequest,
"QCXG6B4E": qcxg.ProcessQCXG6B4ERequest,
"QCXG4896": qcxg.ProcessQCXG4896Request,
// DWBG系列处理器 - 多维报告
"DWBG6A2C": dwbg.ProcessDWBG6A2CRequest,

View File

@@ -199,6 +199,7 @@ func (s *FormConfigServiceImpl) getDTOStruct(ctx context.Context, apiCode string
"IVYZ5A9O": &dto.IVYZ5A9OReq{}, //全国⾃然⼈⻛险评估评分模型
"IVYZ6M8P": &dto.IVYZ6M8PReq{}, //职业资格证书
"QYGL5CMP": &dto.QYGL5CMPReq{}, //企业五要素验证
"QCXG4896": &dto.QCXG4896Req{}, //网约车风险查询
}
// 优先返回已配置的DTO

View File

@@ -0,0 +1,47 @@
package qcxg
import (
"context"
"encoding/json"
"errors"
"strings"
"tyapi-server/internal/domains/api/dto"
"tyapi-server/internal/domains/api/services/processors"
"tyapi-server/internal/infrastructure/external/muzi"
)
// ProcessQCXG4896MRequest QCXG4896 API处理方法 - 网约车风险查询
func ProcessQCXG4896Request(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
var paramsDto dto.QCXG4896Req
if err := json.Unmarshal(params, &paramsDto); err != nil {
return nil, errors.Join(processors.ErrSystem, err)
}
if err := deps.Validator.ValidateStruct(paramsDto); err != nil {
return nil, errors.Join(processors.ErrInvalidParam, err)
}
reqData := map[string]interface{}{
"paramName": "licenseNo",
"paramValue": paramsDto.PlateNo,
"startTime": strings.Split(paramsDto.AuthDate, "-")[0],
"endTime": strings.Split(paramsDto.AuthDate, "-")[1],
}
respData, err := deps.MuziService.CallAPI(ctx, "PC0031", reqData)
if err != nil {
switch {
case errors.Is(err, muzi.ErrDatasource):
return nil, errors.Join(processors.ErrDatasource, err)
case errors.Is(err, muzi.ErrSystem):
return nil, errors.Join(processors.ErrSystem, err)
default:
return nil, errors.Join(processors.ErrSystem, err)
}
}
return respData, nil
}

View File

@@ -14,7 +14,6 @@ type ComponentReportDownload struct {
ProductID string `gorm:"type:varchar(36);not null;index" comment:"产品ID"`
ProductCode string `gorm:"type:varchar(50);not null;index" comment:"产品编号"`
ProductName string `gorm:"type:varchar(200);not null" comment:"产品名称"`
// 直接关联购买订单
OrderID *string `gorm:"type:varchar(36);index" comment:"关联的购买订单ID"`
OrderNumber *string `gorm:"type:varchar(64);index" comment:"关联的购买订单号"`

View File

@@ -289,8 +289,11 @@ func (h *ComponentReportOrderHandler) CheckPaymentStatus(c *gin.Context) {
// DownloadFile 下载文件
// GET /api/v1/component-report/download/:orderId
func (h *ComponentReportOrderHandler) DownloadFile(c *gin.Context) {
h.logger.Info("开始处理文件下载请求")
userID := c.GetString("user_id")
if userID == "" {
h.logger.Error("用户未登录")
c.JSON(http.StatusUnauthorized, gin.H{
"code": 401,
"message": "用户未登录",
@@ -298,8 +301,11 @@ func (h *ComponentReportOrderHandler) DownloadFile(c *gin.Context) {
return
}
h.logger.Info("获取用户ID", zap.String("user_id", userID))
orderID := c.Param("orderId")
if orderID == "" {
h.logger.Error("订单ID不能为空")
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "订单ID不能为空",
@@ -307,23 +313,46 @@ func (h *ComponentReportOrderHandler) DownloadFile(c *gin.Context) {
return
}
h.logger.Info("获取订单ID", zap.String("order_id", orderID))
filePath, err := h.service.DownloadFile(c.Request.Context(), orderID)
if err != nil {
h.logger.Error("下载文件失败", zap.Error(err), zap.String("order_id", orderID))
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
h.logger.Error("下载文件失败", zap.Error(err), zap.String("order_id", orderID), zap.String("user_id", userID))
// 根据错误类型返回不同的状态码和消息
errorMessage := err.Error()
statusCode := http.StatusInternalServerError
// 根据错误消息判断具体错误类型
if strings.Contains(errorMessage, "购买订单不存在") {
statusCode = http.StatusNotFound
} else if strings.Contains(errorMessage, "订单未支付") || strings.Contains(errorMessage, "已过期") {
statusCode = http.StatusForbidden
} else if strings.Contains(errorMessage, "生成报告文件失败") {
statusCode = http.StatusInternalServerError
}
c.JSON(statusCode, gin.H{
"code": statusCode,
"message": "下载文件失败",
"error": err.Error(),
"error": errorMessage,
})
return
}
h.logger.Info("成功获取文件路径",
zap.String("order_id", orderID),
zap.String("user_id", userID),
zap.String("file_path", filePath))
// 设置响应头
c.Header("Content-Type", "application/zip")
c.Header("Content-Disposition", "attachment; filename=component_report.zip")
// 发送文件
h.logger.Info("开始发送文件", zap.String("file_path", filePath))
c.File(filePath)
h.logger.Info("文件发送成功", zap.String("file_path", filePath))
}
// GetUserOrders 获取用户订单列表