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) { id := c.Param("id") orderID, _ := strconv.Atoi(id) orderService := service.NewOrderService() order, err := orderService.GetOrderByID(uint(orderID)) if err != nil { 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) { id := c.Param("id") orderID, _ := strconv.Atoi(id) orderService := service.NewOrderService() if err := orderService.CancelOrder(uint(orderID)); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "code": 500, "msg": "取消订单失败", }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "msg": "success", }) }