优化阅读页跳转逻辑,优先传递章节中间ID(mid),以提升分享功能的一致性。更新相关页面以支持新逻辑,确保用户体验流畅。增加退款功能的相关处理,支持订单退款及退款原因的记录,增强订单管理的灵活性。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
|
||||
"soul-api/internal/database"
|
||||
"soul-api/internal/model"
|
||||
"soul-api/internal/wechat"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -32,7 +34,7 @@ func OrdersList(c *gin.Context) {
|
||||
if statusFilter == "completed" {
|
||||
q = q.Where("status IN ?", []string{"paid", "completed"})
|
||||
} else {
|
||||
q = q.Where("status = ?", statusFilter)
|
||||
q = q.Where("status = ?", statusFilter) // 含 refunded、pending、created、failed
|
||||
}
|
||||
}
|
||||
if search != "" {
|
||||
@@ -195,3 +197,55 @@ func MiniprogramOrders(c *gin.Context) {
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": out})
|
||||
}
|
||||
|
||||
// AdminOrderRefund PUT /api/admin/orders/refund 管理端-订单退款(仅支持已支付订单,调用微信支付退款)
|
||||
func AdminOrderRefund(c *gin.Context) {
|
||||
var req struct {
|
||||
OrderSn string `json:"orderSn" binding:"required"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "error": "缺少订单号"})
|
||||
return
|
||||
}
|
||||
db := database.DB()
|
||||
var order model.Order
|
||||
if err := db.Where("order_sn = ?", req.OrderSn).First(&order).Error; err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "error": "订单不存在"})
|
||||
return
|
||||
}
|
||||
status := ""
|
||||
if order.Status != nil {
|
||||
status = *order.Status
|
||||
}
|
||||
if status != "paid" && status != "completed" {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "error": "仅支持已支付订单退款"})
|
||||
return
|
||||
}
|
||||
transactionID := ""
|
||||
if order.TransactionID != nil {
|
||||
transactionID = *order.TransactionID
|
||||
}
|
||||
if transactionID == "" {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "error": "订单缺少微信支付单号,无法退款"})
|
||||
return
|
||||
}
|
||||
totalCents := int(order.Amount * 100)
|
||||
if totalCents < 1 {
|
||||
totalCents = 1
|
||||
}
|
||||
if err := wechat.RefundOrder(context.Background(), order.OrderSN, transactionID, totalCents, req.Reason); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "error": "微信退款失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
refunded := "refunded"
|
||||
updates := map[string]interface{}{"status": refunded}
|
||||
if req.Reason != "" {
|
||||
updates["refund_reason"] = req.Reason
|
||||
}
|
||||
if err := db.Model(&order).Updates(updates).Error; err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "error": "退款成功但更新订单状态失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "message": "退款成功"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user