Merge pull request #131 from swift-fs/v2.0

refactor: 前台api/api目录按照gf gen ctrl规范编码,可以使用gf工具链快速生成控制器相关代码,也可手动编写
This commit is contained in:
孟帅 2024-08-27 10:17:46 +08:00 committed by GitHub
commit 6cf80ed0fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 203 additions and 108 deletions

View File

@ -1,16 +1,15 @@
// Package member
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package member
import "github.com/gogf/gf/v2/frame/g"
import (
"context"
// GetIdByCodeReq 通过邀请码获取用户ID
type GetIdByCodeReq struct {
g.Meta `path:"/member/getIdByCode" method:"post" tags:"用户" summary:"通过邀请码获取用户ID"`
Code string `json:"code" dc:"邀请码"`
"hotgo/api/api/member/v1"
)
type IMemberV1 interface {
GetIdByCode(ctx context.Context, req *v1.GetIdByCodeReq) (res *v1.GetIdByCodeRes, err error)
}
type GetIdByCodeRes struct{}

View File

@ -0,0 +1,16 @@
// Package member
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package v1
import "github.com/gogf/gf/v2/frame/g"
// GetIdByCodeReq 通过邀请码获取用户ID
type GetIdByCodeReq struct {
g.Meta `path:"/member/getIdByCode" method:"post" tags:"用户" summary:"通过邀请码获取用户ID"`
Code string `json:"code" dc:"邀请码"`
}
type GetIdByCodeRes struct{}

17
server/api/api/pay/pay.go Normal file
View File

@ -0,0 +1,17 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package pay
import (
"context"
"hotgo/api/api/pay/v1"
)
type IPayV1 interface {
NotifyAliPay(ctx context.Context, req *v1.NotifyAliPayReq) (res *v1.NotifyAliPayRes, err error)
NotifyWxPay(ctx context.Context, req *v1.NotifyWxPayReq) (res *v1.NotifyWxPayRes, err error)
NotifyQQPay(ctx context.Context, req *v1.NotifyQQPayReq) (res *v1.NotifyQQPayRes, err error)
}

View File

@ -3,11 +3,12 @@
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package pay
package v1
import (
"github.com/gogf/gf/v2/frame/g"
"hotgo/internal/model/input/payin"
"github.com/gogf/gf/v2/frame/g"
)
// NotifyAliPayReq 支付宝回调

View File

@ -38,4 +38,10 @@ gfcli:
# srcFolder: "internal/logic"
# dstFolder: "internal/service"
# dstFileNameCase: "CamelLower"
# clear: true
# clear: true
ctrl:
# api/api下的接口可以使用gf gen ctrl自动生成控制器相关代码
srcFolder: "api/api"
dstFolder: "internal/controller/api"
clear: true
merge: false

View File

@ -45,7 +45,12 @@ func (c *cSite) Config(ctx context.Context, _ *common.SiteConfigReq) (res *commo
func (c *cSite) getWsAddr(ctx context.Context, request *ghttp.Request) string {
// 如果是本地IP访问则认为是调试模式走实际请求地址否则走配置中的地址
// 尝试读取hostname兼容本地运行模式
ip := ghttp.RequestFromCtx(ctx).GetHeader("hostname")
if len(ip) == 0 {
ip = ghttp.RequestFromCtx(ctx).GetHost()
}
if validate.IsLocalIPAddr(ip) {
return "ws://" + ip + ":" + gstr.StrEx(request.Host, ":") + g.Cfg().MustGet(ctx, "router.websocket.prefix").String()
}
@ -59,7 +64,12 @@ func (c *cSite) getWsAddr(ctx context.Context, request *ghttp.Request) string {
func (c *cSite) getDomain(ctx context.Context, request *ghttp.Request) string {
// 如果是本地IP访问则认为是调试模式走实际请求地址否则走配置中的地址
ip := request.GetHeader("hostname")
// 尝试读取hostname兼容本地运行模式
ip := ghttp.RequestFromCtx(ctx).GetHeader("hostname")
if len(ip) == 0 {
ip = ghttp.RequestFromCtx(ctx).GetHost()
}
if validate.IsLocalIPAddr(ip) {
return "http://" + ip + ":" + gstr.StrEx(request.Host, ":")
}

View File

@ -1,23 +1,5 @@
// Package member
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package member
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"hotgo/api/api/member"
)
var (
Member = cMember{}
)
type cMember struct{}
func (c *cMember) GetIdByCode(ctx context.Context, _ *member.GetIdByCodeReq) (res *member.GetIdByCodeRes, err error) {
g.RequestFromCtx(ctx).Response.Writeln("Hello World api member!")
return
}

View File

@ -0,0 +1,15 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package member
import (
"hotgo/api/api/member"
)
type ControllerV1 struct{}
func NewV1() member.IMemberV1 {
return &ControllerV1{}
}

View File

@ -0,0 +1,14 @@
package member
import (
"context"
v1 "hotgo/api/api/member/v1"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) GetIdByCode(ctx context.Context, req *v1.GetIdByCodeReq) (res *v1.GetIdByCodeRes, err error) {
g.RequestFromCtx(ctx).Response.Writeln("Hello World api member!")
return
}

View File

@ -1,57 +0,0 @@
// Package pay
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package pay
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"hotgo/api/api/pay"
"hotgo/internal/consts"
"hotgo/internal/library/response"
"hotgo/internal/model/input/payin"
"hotgo/internal/service"
)
var (
Notify = cNotify{}
)
type cNotify struct{}
// AliPay 支付宝回调
func (c *cNotify) AliPay(ctx context.Context, _ *pay.NotifyAliPayReq) (res *pay.NotifyAliPayRes, err error) {
if _, err = service.Pay().Notify(ctx, &payin.PayNotifyInp{PayType: consts.PayTypeAliPay}); err != nil {
return nil, err
}
response.RText(g.RequestFromCtx(ctx), "success")
return
}
// WxPay 微信支付回调
func (c *cNotify) WxPay(ctx context.Context, _ *pay.NotifyWxPayReq) (res *pay.NotifyWxPayRes, err error) {
if _, err = service.Pay().Notify(ctx, &payin.PayNotifyInp{PayType: consts.PayTypeWxPay}); err != nil {
return
}
response.CustomJson(g.RequestFromCtx(ctx), `{"code": "SUCCESS","message": "收单成功"}`)
return
}
// QQPay QQ支付回调
func (c *cNotify) QQPay(ctx context.Context, _ *pay.NotifyQQPayReq) (res *pay.NotifyQQPayRes, err error) {
if _, err = service.Pay().Notify(ctx, &payin.PayNotifyInp{PayType: consts.PayTypeQQPay}); err != nil {
return
}
r := g.RequestFromCtx(ctx)
r.Response.ClearBuffer()
r.Response.Write(`<?xml version="1.0" encoding="UTF-8"?>`)
r.Response.WriteXml(g.Map{
"return_code": "SUCCESS",
})
return
}

View File

@ -0,0 +1,5 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package pay

View File

@ -0,0 +1,15 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package pay
import (
"hotgo/api/api/pay"
)
type ControllerV1 struct{}
func NewV1() pay.IPayV1 {
return &ControllerV1{}
}

View File

@ -0,0 +1,22 @@
package pay
import (
"context"
v1 "hotgo/api/api/pay/v1"
"hotgo/internal/consts"
"hotgo/internal/library/response"
"hotgo/internal/model/input/payin"
"hotgo/internal/service"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) NotifyAliPay(ctx context.Context, req *v1.NotifyAliPayReq) (res *v1.NotifyAliPayRes, err error) {
if _, err = service.Pay().Notify(ctx, &payin.PayNotifyInp{PayType: consts.PayTypeAliPay}); err != nil {
return nil, err
}
response.RText(g.RequestFromCtx(ctx), "success")
return
}

View File

@ -0,0 +1,26 @@
package pay
import (
"context"
v1 "hotgo/api/api/pay/v1"
"hotgo/internal/consts"
"hotgo/internal/model/input/payin"
"hotgo/internal/service"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) NotifyQQPay(ctx context.Context, req *v1.NotifyQQPayReq) (res *v1.NotifyQQPayRes, err error) {
if _, err = service.Pay().Notify(ctx, &payin.PayNotifyInp{PayType: consts.PayTypeQQPay}); err != nil {
return
}
r := g.RequestFromCtx(ctx)
r.Response.ClearBuffer()
r.Response.Write(`<?xml version="1.0" encoding="UTF-8"?>`)
r.Response.WriteXml(g.Map{
"return_code": "SUCCESS",
})
return
}

View File

@ -0,0 +1,22 @@
package pay
import (
"context"
v1 "hotgo/api/api/pay/v1"
"hotgo/internal/consts"
"hotgo/internal/library/response"
"hotgo/internal/model/input/payin"
"hotgo/internal/service"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) NotifyWxPay(ctx context.Context, req *v1.NotifyWxPayReq) (res *v1.NotifyWxPayRes, err error) {
if _, err = service.Pay().Notify(ctx, &payin.PayNotifyInp{PayType: consts.PayTypeWxPay}); err != nil {
return
}
response.CustomJson(g.RequestFromCtx(ctx), `{"code": "SUCCESS","message": "收单成功"}`)
return
}

View File

@ -10,13 +10,7 @@ package pay
import (
"context"
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/util/gmeta"
"hotgo/api/api/pay"
v1 "hotgo/api/api/pay/v1"
"hotgo/internal/consts"
"hotgo/internal/library/contexts"
"hotgo/internal/library/location"
@ -25,6 +19,13 @@ import (
"hotgo/internal/model/input/payin"
"hotgo/internal/service"
"hotgo/utility/validate"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/util/gmeta"
)
// Create 创建支付订单和日志
@ -127,11 +128,11 @@ func (s *sPay) GenNotifyURL(ctx context.Context, in payin.PayCreateInp) (notifyU
var object interface{}
switch in.PayType {
case consts.PayTypeAliPay:
object = pay.NotifyAliPayReq{}
object = v1.NotifyAliPayReq{}
case consts.PayTypeWxPay:
object = pay.NotifyWxPayReq{}
object = v1.NotifyWxPayReq{}
case consts.PayTypeQQPay:
object = pay.NotifyQQPayReq{}
object = v1.NotifyQQPayReq{}
default:
err = gerror.Newf("未被支持的支付方式:%v", in.PayType)
return

View File

@ -7,23 +7,24 @@ package router
import (
"context"
"github.com/gogf/gf/v2/net/ghttp"
"hotgo/internal/consts"
"hotgo/internal/controller/api/member"
"hotgo/internal/controller/api/pay"
"hotgo/internal/service"
"hotgo/utility/simple"
"github.com/gogf/gf/v2/net/ghttp"
)
// Api 前台路由
func Api(ctx context.Context, group *ghttp.RouterGroup) {
group.Group(simple.RouterPrefix(ctx, consts.AppApi), func(group *ghttp.RouterGroup) {
group.Bind(
pay.Notify, // 支付异步通知
pay.NewV1(), // 支付异步通知
)
group.Middleware(service.Middleware().ApiAuth)
group.Bind(
member.Member, // 管理员
member.NewV1(), // 管理员
)
})
}