package handler import ( "net/http" "strconv" "github.com/gin-gonic/gin" "github.com/gouki/lunar-server/internal/service" ) // GetOrderList 获取订单列表 func GetOrderList(c *gin.Context) { userID, exists := c.Get("userID") if !exists { c.JSON(http.StatusUnauthorized, gin.H{ "code": 401, "msg": "未授权", }) return } page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10")) orderService := service.NewOrderService() orders, total, err := orderService.GetOrderList(userID.(uint), page, pageSize) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "code": 500, "msg": "获取订单列表失败", }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "msg": "success", "data": gin.H{ "list": orders, "total": total, "page": page, }, }) } // GetOrderDetail 获取订单详情(仅限本人订单) func GetOrderDetail(c *gin.Context) { userID, _ := c.Get("userID") id := c.Param("id") orderID, _ := strconv.Atoi(id) orderService := service.NewOrderService() order, err := orderService.GetOrderByID(uint(orderID)) if err != nil || order.UserID != userID.(uint) { // 不存在与无权访问统一返回 404,避免枚举他人订单 c.JSON(http.StatusNotFound, gin.H{ "code": 404, "msg": "订单不存在", }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "msg": "success", "data": order, }) } // CancelOrder 取消订单(仅限本人待支付订单) func CancelOrder(c *gin.Context) { userID, _ := c.Get("userID") id := c.Param("id") orderID, _ := strconv.Atoi(id) orderService := service.NewOrderService() if err := orderService.CancelOrder(userID.(uint), uint(orderID)); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 400, "msg": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "msg": "success", }) }