feat(internal): 添加 CORS 支持并更新 API 路径- 在 cmd.go 中添加了 CORS 中间件,用于支持跨域请求

- 更新了 hero.go 中的 API 路径,使其符合新的命名规范
- 优化了 hero_v1.go 中的响应构造方式,提高了代码效率
This commit is contained in:
hxt
2025-06-22 23:50:57 +08:00
parent eac06dfed3
commit 1fbea6ad9f
3 changed files with 10 additions and 8 deletions

View File

@@ -15,14 +15,12 @@ type GetOneRes struct {
// GetListReq GetListRes 列表返回所有角色 // GetListReq GetListRes 列表返回所有角色
type GetListReq struct { type GetListReq struct {
g.Meta `path:"/getList" method:"get" tags:"Hero" summary:"Get all hero"` g.Meta `path:"/app-api/epic/hero/list-all" method:"get" tags:"Hero" summary:"Get all hero"`
}
type GetListRes struct {
Records []*EpicHeroVO `json:"list"` // ✅ 返回一个数组
} }
type GetListRes []*EpicHeroVO
type GetDetailReq struct { type GetDetailReq struct {
g.Meta `path:"/getDetail" method:"get" tags:"Hero" summary:"Get hero detail by code"` g.Meta `path:"/app-api/epic/hero/hero-detail" method:"get" tags:"Hero" summary:"Get hero detail by code"`
Code string `v:"required" dc:"角色code"` Code string `v:"required" dc:"角色code"`
} }
type GetDetailRes struct { type GetDetailRes struct {

View File

@@ -8,6 +8,11 @@ import (
"github.com/gogf/gf/v2/os/gcmd" "github.com/gogf/gf/v2/os/gcmd"
) )
func CORS(r *ghttp.Request) {
r.Response.CORSDefault()
r.Middleware.Next()
}
var ( var (
Main = gcmd.Command{ Main = gcmd.Command{
Name: "main", Name: "main",
@@ -15,6 +20,7 @@ var (
Brief: "start http server", Brief: "start http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) { Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
s := g.Server() s := g.Server()
s.Use(CORS)
s.Group("/", func(group *ghttp.RouterGroup) { s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse) group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind( group.Bind(

View File

@@ -26,9 +26,7 @@ func (c *ControllerV1) GetList(ctx context.Context, req *v1.GetListReq) (res *v1
} }
// 构造响应 // 构造响应
res = &v1.GetListRes{ res = (*v1.GetListRes)(&list)
Records: list, // ✅ 正确赋值字段
}
return res, nil return res, nil
} }