初始化项目

This commit is contained in:
2025-12-10 09:02:41 +08:00
parent 3a40846865
commit 3c55577df8
19 changed files with 2653 additions and 10 deletions

381
controller/order.go Normal file
View File

@@ -0,0 +1,381 @@
package controller
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"order/model/dto"
"order/service"
)
// Order 订单控制器
type Order struct{}
var OrderController = &Order{}
// Create 创建订单
func (c *Order) Create(r *ghttp.Request) {
ctx := r.Context()
var req dto.CreateOrderReq
if err := r.Parse(&req); err != nil {
g.Log().Error(ctx, "解析请求参数失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": "参数错误",
"data": nil,
})
return
}
// 获取订单服务实例
orderService := service.GetOrderService()
if orderService == nil {
g.Log().Error(ctx, "订单服务未初始化")
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": "服务内部错误",
"data": nil,
})
return
}
// 创建订单
resp, err := orderService.CreateOrder(ctx, &req)
if err != nil {
g.Log().Error(ctx, "创建订单失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJson(g.Map{
"code": 200,
"message": "success",
"data": resp,
})
}
// Pay 支付订单
func (c *Order) Pay(r *ghttp.Request) {
ctx := r.Context()
var req dto.PayOrderReq
if err := r.Parse(&req); err != nil {
g.Log().Error(ctx, "解析请求参数失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": "参数错误",
"data": nil,
})
return
}
// 获取支付服务实例
paymentService := service.GetPaymentService()
if paymentService == nil {
g.Log().Error(ctx, "支付服务未初始化")
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": "服务内部错误",
"data": nil,
})
return
}
// 支付订单
resp, err := paymentService.PayOrder(ctx, &req)
if err != nil {
g.Log().Error(ctx, "支付订单失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJson(g.Map{
"code": 200,
"message": "success",
"data": resp,
})
}
// Query 查询订单详情
func (c *Order) Query(r *ghttp.Request) {
ctx := r.Context()
var req dto.QueryOrderReq
if err := r.Parse(&req); err != nil {
g.Log().Error(ctx, "解析请求参数失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": "参数错误",
"data": nil,
})
return
}
// 获取订单服务实例
orderService := service.GetOrderService()
if orderService == nil {
g.Log().Error(ctx, "订单服务未初始化")
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": "服务内部错误",
"data": nil,
})
return
}
// 查询订单
resp, err := orderService.QueryOrder(ctx, &req)
if err != nil {
g.Log().Error(ctx, "查询订单失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJson(g.Map{
"code": 200,
"message": "success",
"data": resp,
})
}
// Cancel 取消订单
func (c *Order) Cancel(r *ghttp.Request) {
ctx := r.Context()
var req dto.CancelOrderReq
if err := r.Parse(&req); err != nil {
g.Log().Error(ctx, "解析请求参数失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": "参数错误",
"data": nil,
})
return
}
// 获取订单服务实例
orderService := service.GetOrderService()
if orderService == nil {
g.Log().Error(ctx, "订单服务未初始化")
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": "服务内部错误",
"data": nil,
})
return
}
// 取消订单
resp, err := orderService.CancelOrder(ctx, &req)
if err != nil {
g.Log().Error(ctx, "取消订单失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJson(g.Map{
"code": 200,
"message": "success",
"data": resp,
})
}
// Refund 退款
func (c *Order) Refund(r *ghttp.Request) {
ctx := r.Context()
var req dto.RefundOrderReq
if err := r.Parse(&req); err != nil {
g.Log().Error(ctx, "解析请求参数失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": "参数错误",
"data": nil,
})
return
}
// 获取支付服务实例
paymentService := service.GetPaymentService()
if paymentService == nil {
g.Log().Error(ctx, "支付服务未初始化")
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": "服务内部错误",
"data": nil,
})
return
}
// 退款
resp, err := paymentService.RefundOrder(ctx, &req)
if err != nil {
g.Log().Error(ctx, "退款失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJson(g.Map{
"code": 200,
"message": "success",
"data": resp,
})
}
// List 查询订单列表
func (c *Order) List(r *ghttp.Request) {
ctx := r.Context()
var req dto.ListOrdersReq
if err := r.Parse(&req); err != nil {
g.Log().Error(ctx, "解析请求参数失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": "参数错误",
"data": nil,
})
return
}
// 获取订单服务实例
orderService := service.GetOrderService()
if orderService == nil {
g.Log().Error(ctx, "订单服务未初始化")
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": "服务内部错误",
"data": nil,
})
return
}
// 查询订单列表
resp, err := orderService.ListOrders(ctx, &req)
if err != nil {
g.Log().Error(ctx, "查询订单列表失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJson(g.Map{
"code": 200,
"message": "success",
"data": resp,
})
}
// PaymentNotify 支付回调
func (c *Order) PaymentNotify(r *ghttp.Request) {
ctx := r.Context()
var req service.PaymentNotifyReq
if err := r.Parse(&req); err != nil {
g.Log().Error(ctx, "解析支付回调参数失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": "参数错误",
"data": nil,
})
return
}
// 获取支付服务实例
paymentService := service.GetPaymentService()
if paymentService == nil {
g.Log().Error(ctx, "支付服务未初始化")
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": "服务内部错误",
"data": nil,
})
return
}
// 处理支付回调
if err := paymentService.HandlePaymentNotify(ctx, &req); err != nil {
g.Log().Error(ctx, "处理支付回调失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJson(g.Map{
"code": 200,
"message": "success",
"data": nil,
})
}
// RefundNotify 退款回调
func (c *Order) RefundNotify(r *ghttp.Request) {
ctx := r.Context()
var req service.RefundNotifyReq
if err := r.Parse(&req); err != nil {
g.Log().Error(ctx, "解析退款回调参数失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": "参数错误",
"data": nil,
})
return
}
// 获取支付服务实例
paymentService := service.GetPaymentService()
if paymentService == nil {
g.Log().Error(ctx, "支付服务未初始化")
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": "服务内部错误",
"data": nil,
})
return
}
// 处理退款回调
if err := paymentService.HandleRefundNotify(ctx, &req); err != nil {
g.Log().Error(ctx, "处理退款回调失败:", err)
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJson(g.Map{
"code": 200,
"message": "success",
"data": nil,
})
}

View File

@@ -0,0 +1,316 @@
package controller
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"go.mongodb.org/mongo-driver/bson"
"order/model/dto"
"order/service"
)
type PaymentConfigController struct{}
var PaymentConfig = &PaymentConfigController{}
// CreatePaymentConfig 创建支付配置
func (c *PaymentConfigController) CreatePaymentConfig(r *ghttp.Request) {
var req dto.CreatePaymentConfigReq
if err := r.Parse(&req); err != nil {
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": err.Error(),
"data": nil,
})
return
}
ctx := context.Background()
// 构建支付配置实体
config := &service.PaymentConfig{
TenantID: req.TenantID,
PayMethod: req.PayMethod,
ConfigType: req.ConfigType,
Environment: req.Environment,
IsEnabled: req.IsEnabled,
}
if req.WechatConfig != nil {
config.WechatConfig = &service.WechatConfig{
AppID: req.WechatConfig.AppID,
MchID: req.WechatConfig.MchID,
APIKey: req.WechatConfig.APIKey,
APIClientCert: req.WechatConfig.APIClientCert,
APIClientKey: req.WechatConfig.APIClientKey,
NotifyURL: req.WechatConfig.NotifyURL,
RefundNotifyURL: req.WechatConfig.RefundNotifyURL,
SubAppID: req.WechatConfig.SubAppID,
SubMchID: req.WechatConfig.SubMchID,
}
}
if req.AlipayConfig != nil {
config.AlipayConfig = &service.AlipayConfig{
AppID: req.AlipayConfig.AppID,
PrivateKey: req.AlipayConfig.PrivateKey,
PublicKey: req.AlipayConfig.PublicKey,
AlipayPublicKey: req.AlipayConfig.AlipayPublicKey,
NotifyURL: req.AlipayConfig.NotifyURL,
RefundNotifyURL: req.AlipayConfig.RefundNotifyURL,
Format: req.AlipayConfig.Format,
Charset: req.AlipayConfig.Charset,
SignType: req.AlipayConfig.SignType,
IsSandbox: req.AlipayConfig.IsSandbox,
}
}
if err := service.PaymentService.CreatePaymentConfig(ctx, config); err != nil {
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJsonExit(g.Map{
"code": 200,
"message": "success",
"data": nil,
})
}
// UpdatePaymentConfig 更新支付配置
func (c *PaymentConfigController) UpdatePaymentConfig(r *ghttp.Request) {
var req dto.UpdatePaymentConfigReq
if err := r.Parse(&req); err != nil {
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": err.Error(),
"data": nil,
})
return
}
ctx := context.Background()
update := bson.M{}
if req.ConfigType != "" {
update["config_type"] = req.ConfigType
}
if req.Environment != "" {
update["environment"] = req.Environment
}
if req.IsEnabled != nil {
update["is_enabled"] = *req.IsEnabled
}
if req.WechatConfig != nil {
wechatConfig := bson.M{}
if req.WechatConfig.AppID != "" {
wechatConfig["app_id"] = req.WechatConfig.AppID
}
if req.WechatConfig.MchID != "" {
wechatConfig["mch_id"] = req.WechatConfig.MchID
}
if req.WechatConfig.APIKey != "" {
wechatConfig["api_key"] = req.WechatConfig.APIKey
}
if req.WechatConfig.APIClientCert != "" {
wechatConfig["api_client_cert"] = req.WechatConfig.APIClientCert
}
if req.WechatConfig.APIClientKey != "" {
wechatConfig["api_client_key"] = req.WechatConfig.APIClientKey
}
if req.WechatConfig.NotifyURL != "" {
wechatConfig["notify_url"] = req.WechatConfig.NotifyURL
}
if req.WechatConfig.RefundNotifyURL != "" {
wechatConfig["refund_notify_url"] = req.WechatConfig.RefundNotifyURL
}
if req.WechatConfig.SubAppID != "" {
wechatConfig["sub_app_id"] = req.WechatConfig.SubAppID
}
if req.WechatConfig.SubMchID != "" {
wechatConfig["sub_mch_id"] = req.WechatConfig.SubMchID
}
update["wechat_config"] = wechatConfig
}
if req.AlipayConfig != nil {
alipayConfig := bson.M{}
if req.AlipayConfig.AppID != "" {
alipayConfig["app_id"] = req.AlipayConfig.AppID
}
if req.AlipayConfig.PrivateKey != "" {
alipayConfig["private_key"] = req.AlipayConfig.PrivateKey
}
if req.AlipayConfig.PublicKey != "" {
alipayConfig["public_key"] = req.AlipayConfig.PublicKey
}
if req.AlipayConfig.AlipayPublicKey != "" {
alipayConfig["alipay_public_key"] = req.AlipayConfig.AlipayPublicKey
}
if req.AlipayConfig.NotifyURL != "" {
alipayConfig["notify_url"] = req.AlipayConfig.NotifyURL
}
if req.AlipayConfig.RefundNotifyURL != "" {
alipayConfig["refund_notify_url"] = req.AlipayConfig.RefundNotifyURL
}
if req.AlipayConfig.Format != "" {
alipayConfig["format"] = req.AlipayConfig.Format
}
if req.AlipayConfig.Charset != "" {
alipayConfig["charset"] = req.AlipayConfig.Charset
}
if req.AlipayConfig.SignType != "" {
alipayConfig["sign_type"] = req.AlipayConfig.SignType
}
if req.AlipayConfig.IsSandbox != false {
alipayConfig["is_sandbox"] = req.AlipayConfig.IsSandbox
}
update["alipay_config"] = alipayConfig
}
if err := service.PaymentService.UpdatePaymentConfig(ctx, req.ID, update); err != nil {
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJsonExit(g.Map{
"code": 200,
"message": "success",
"data": nil,
})
}
// QueryPaymentConfig 查询支付配置
func (c *PaymentConfigController) QueryPaymentConfig(r *ghttp.Request) {
tentID := r.Get("tenant_id").String()
payMethod := r.Get("pay_method").String()
configType := r.Get("config_type").String()
environment := r.Get("environment").String()
ctx := context.Background()
config, err := service.PaymentService.QueryPaymentConfig(ctx, tentID, payMethod, configType, environment)
if err != nil {
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
r.Response.WriteJsonExit(g.Map{
"code": 200,
"message": "success",
"data": config,
})
}
// ListPaymentConfigs 查询支付配置列表
func (c *PaymentConfigController) ListPaymentConfigs(r *ghttp.Request) {
var req dto.QueryPaymentConfigReq
if err := r.Parse(&req); err != nil {
r.Response.WriteJsonExit(g.Map{
"code": 400,
"message": err.Error(),
"data": nil,
})
return
}
ctx := context.Background()
filter := bson.M{"tenant_id": req.TenantID}
if req.PayMethod != "" {
filter["pay_method"] = req.PayMethod
}
if req.ConfigType != "" {
filter["config_type"] = req.ConfigType
}
if req.Environment != "" {
filter["environment"] = req.Environment
}
if req.IsEnabled != nil {
filter["is_enabled"] = *req.IsEnabled
}
if req.Page <= 0 {
req.Page = 1
}
if req.PageSize <= 0 {
req.PageSize = 10
}
configs, total, err := service.PaymentService.ListPaymentConfigs(ctx, filter, req.Page, req.PageSize)
if err != nil {
r.Response.WriteJsonExit(g.Map{
"code": 500,
"message": err.Error(),
"data": nil,
})
return
}
resp := dto.PaymentConfigListResp{
List: make([]dto.PaymentConfigResp, 0, len(configs)),
Total: total,
Page: req.Page,
PageSize: req.PageSize,
TotalPage: int((total + int64(req.PageSize) - 1) / int64(req.PageSize)),
}
for _, config := range configs {
configResp := dto.PaymentConfigResp{
ID: config.ID.Hex(),
TenantID: config.TenantID,
PayMethod: config.PayMethod,
ConfigType: config.ConfigType,
Environment: config.Environment,
IsEnabled: config.IsEnabled,
CreatedAt: config.CreatedAt,
UpdatedAt: config.UpdatedAt,
CreatedBy: config.CreatedBy,
UpdatedBy: config.UpdatedBy,
}
if config.WechatConfig != nil {
configResp.WechatConfig = &dto.WechatConfigResp{
AppID: config.WechatConfig.AppID,
MchID: config.WechatConfig.MchID,
NotifyURL: config.WechatConfig.NotifyURL,
RefundNotifyURL: config.WechatConfig.RefundNotifyURL,
SubAppID: config.WechatConfig.SubAppID,
SubMchID: config.WechatConfig.SubMchID,
}
}
if config.AlipayConfig != nil {
configResp.AlipayConfig = &dto.AlipayConfigResp{
AppID: config.AlipayConfig.AppID,
NotifyURL: config.AlipayConfig.NotifyURL,
RefundNotifyURL: config.AlipayConfig.RefundNotifyURL,
Format: config.AlipayConfig.Format,
Charset: config.AlipayConfig.Charset,
SignType: config.AlipayConfig.SignType,
IsSandbox: config.AlipayConfig.IsSandbox,
}
}
resp.List = append(resp.List, configResp)
}
r.Response.WriteJsonExit(g.Map{
"code": 200,
"message": "success",
"data": resp,
})
}