feat: 实现订单支付功能模块
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m36s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m36s
- 新增订单管理、支付回调、购买验证等核心服务 - 实现订单、订单项目的数据存储层和 API 接口 - 添加订单数据库迁移和 DTO 定义 - 更新 API 文档和路由配置 - 同步 3 个新规范到主规范库(订单管理、订单支付、套餐购买验证) - 完成 OpenSpec 变更归档 Ultraworked with Sisyphus
This commit is contained in:
131
internal/handler/h5/order.go
Normal file
131
internal/handler/h5/order.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package h5
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
orderService "github.com/break/junhong_cmp_fiber/internal/service/order"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
)
|
||||
|
||||
type OrderHandler struct {
|
||||
service *orderService.Service
|
||||
}
|
||||
|
||||
func NewOrderHandler(service *orderService.Service) *OrderHandler {
|
||||
return &OrderHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *OrderHandler) Create(c *fiber.Ctx) error {
|
||||
var req dto.CreateOrderRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
ctx := c.UserContext()
|
||||
userType := middleware.GetUserTypeFromContext(ctx)
|
||||
|
||||
var buyerType string
|
||||
var buyerID uint
|
||||
|
||||
switch userType {
|
||||
case constants.UserTypeAgent:
|
||||
buyerType = model.BuyerTypeAgent
|
||||
buyerID = middleware.GetShopIDFromContext(ctx)
|
||||
case constants.UserTypeEnterprise:
|
||||
return errors.New(errors.CodeForbidden, "企业账号不支持在线购买")
|
||||
case constants.UserTypePersonalCustomer:
|
||||
buyerType = model.BuyerTypePersonal
|
||||
buyerID = middleware.GetCustomerIDFromContext(ctx)
|
||||
default:
|
||||
return errors.New(errors.CodeForbidden, "不支持的用户类型")
|
||||
}
|
||||
|
||||
order, err := h.service.Create(ctx, &req, buyerType, buyerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, order)
|
||||
}
|
||||
|
||||
func (h *OrderHandler) Get(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的订单ID")
|
||||
}
|
||||
|
||||
order, err := h.service.Get(c.UserContext(), uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, order)
|
||||
}
|
||||
|
||||
func (h *OrderHandler) List(c *fiber.Ctx) error {
|
||||
var req dto.OrderListRequest
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
ctx := c.UserContext()
|
||||
userType := middleware.GetUserTypeFromContext(ctx)
|
||||
|
||||
var buyerType string
|
||||
var buyerID uint
|
||||
|
||||
switch userType {
|
||||
case constants.UserTypeAgent:
|
||||
buyerType = model.BuyerTypeAgent
|
||||
buyerID = middleware.GetShopIDFromContext(ctx)
|
||||
case constants.UserTypePersonalCustomer:
|
||||
buyerType = model.BuyerTypePersonal
|
||||
buyerID = middleware.GetCustomerIDFromContext(ctx)
|
||||
default:
|
||||
return errors.New(errors.CodeForbidden, "不支持的用户类型")
|
||||
}
|
||||
|
||||
orders, err := h.service.List(ctx, &req, buyerType, buyerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, orders)
|
||||
}
|
||||
|
||||
func (h *OrderHandler) WalletPay(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的订单ID")
|
||||
}
|
||||
|
||||
ctx := c.UserContext()
|
||||
userType := middleware.GetUserTypeFromContext(ctx)
|
||||
|
||||
var buyerType string
|
||||
var buyerID uint
|
||||
|
||||
switch userType {
|
||||
case constants.UserTypeAgent:
|
||||
buyerType = model.BuyerTypeAgent
|
||||
buyerID = middleware.GetShopIDFromContext(ctx)
|
||||
case constants.UserTypePersonalCustomer:
|
||||
buyerType = model.BuyerTypePersonal
|
||||
buyerID = middleware.GetCustomerIDFromContext(ctx)
|
||||
default:
|
||||
return errors.New(errors.CodeForbidden, "不支持的用户类型")
|
||||
}
|
||||
|
||||
if err := h.service.WalletPay(ctx, uint(id), buyerType, buyerID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user