- 实现数据库连接和自动迁移 - 实现用户服务(登录、资料、JWT认证) - 实现许愿服务(创建、列表、机器人许愿) - 实现订单服务(创建、支付、状态管理) - 实现JWT认证中间件 - 实现管理后台API(仪表盘、用户、订单、许愿、设置) - 创建Inertia.js前端页面(Vue3 + Tailwind CSS) - 修复Go模块依赖问题
87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
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",
|
|
})
|
|
}
|