bindbox-game/internal/api/user/orders_app.go
邹方成 45815bfb7d chore: 清理无用文件与优化代码结构
refactor(utils): 修复密码哈希比较逻辑错误
feat(user): 新增按状态筛选优惠券接口
docs: 添加虚拟发货与任务中心相关文档
fix(wechat): 修正Code2Session上下文传递问题
test: 补充订单折扣与积分转换测试用例
build: 更新配置文件与构建脚本
style: 清理多余的空行与注释
2025-12-18 17:35:55 +08:00

144 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
"net/http"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/validation"
"bindbox-game/internal/service/user"
"strconv"
"time"
)
type listOrdersRequest struct {
Page int `form:"page"`
PageSize int `form:"page_size"`
Status int32 `form:"status"`
}
type listOrdersResponse struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
Total int64 `json:"total"`
List []*user.OrderWithItems `json:"list"`
}
// ListUserOrders 查看用户订单记录
// @Summary 查看用户订单记录
// @Description 查看用户抽奖来源订单记录
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Param user_id path integer true "用户ID"
// @Security LoginVerifyToken
// @Param page query int true "页码" default(1)
// @Param page_size query int true "每页数量最多100" default(20)
// @Success 200 {object} listOrdersResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/users/{user_id}/orders [get]
func (h *handler) ListUserOrders() core.HandlerFunc {
return func(ctx core.Context) {
req := new(listOrdersRequest)
rsp := new(listOrdersResponse)
if err := ctx.ShouldBindForm(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
userID := int64(ctx.SessionUserInfo().Id)
items, total, err := h.user.ListOrdersWithItems(ctx.RequestContext(), userID, req.Status, req.Page, req.PageSize)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10002, err.Error()))
return
}
rsp.Page = req.Page
rsp.PageSize = req.PageSize
rsp.Total = total
rsp.List = items
ctx.Payload(rsp)
}
}
// GetOrderDetail 获取订单详情
// @Summary 获取订单详情
// @Description 获取指定订单的详细信息
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Security LoginVerifyToken
// @Param order_id path int true "订单ID"
// @Success 200 {object} user.OrderWithItems
// @Failure 400 {object} code.Failure
// @Router /api/app/orders/{order_id} [get]
func (h *handler) GetOrderDetail() core.HandlerFunc {
return func(ctx core.Context) {
orderIDStr := ctx.Param("order_id")
orderID, err := strconv.ParseInt(orderIDStr, 10, 64)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "订单ID格式错误"))
return
}
userID := int64(ctx.SessionUserInfo().Id)
order, err := h.user.GetOrderWithItems(ctx.RequestContext(), userID, orderID)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10003, err.Error()))
return
}
if order == nil {
ctx.AbortWithError(core.Error(http.StatusNotFound, 10004, "订单不存在"))
return
}
ctx.Payload(order)
}
}
type cancelOrderRequest struct {
Reason string `json:"reason"`
}
type cancelOrderResponse struct {
OrderID int64 `json:"order_id"`
OrderNo string `json:"order_no"`
Status int32 `json:"status"`
CancelledAt time.Time `json:"cancelled_at"`
}
// CancelOrder 取消订单
// @Summary 取消订单
// @Description 取消指定订单(仅限待付款状态)
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Security LoginVerifyToken
// @Param order_id path int true "订单ID"
// @Param body body cancelOrderRequest false "取消原因"
// @Success 200 {object} cancelOrderResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/orders/{order_id}/cancel [post]
func (h *handler) CancelOrder() core.HandlerFunc {
return func(ctx core.Context) {
orderIDStr := ctx.Param("order_id")
orderID, err := strconv.ParseInt(orderIDStr, 10, 64)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "订单ID格式错误"))
return
}
req := new(cancelOrderRequest)
_ = ctx.ShouldBindJSON(req) // Body is optional
userID := int64(ctx.SessionUserInfo().Id)
order, err := h.user.CancelOrder(ctx.RequestContext(), userID, orderID, req.Reason)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10005, err.Error()))
return
}
ctx.Payload(&cancelOrderResponse{
OrderID: order.ID,
OrderNo: order.OrderNo,
Status: order.Status,
CancelledAt: order.CancelledAt,
})
}
}