From 1fbea6ad9f8fdd86dc7687f9d466c6ff82600374 Mon Sep 17 00:00:00 2001 From: hxt Date: Sun, 22 Jun 2025 23:50:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(internal):=20=E6=B7=BB=E5=8A=A0=20CORS=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=B9=B6=E6=9B=B4=E6=96=B0=20API=20=E8=B7=AF?= =?UTF-8?q?=E5=BE=84-=20=E5=9C=A8=20cmd.go=20=E4=B8=AD=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=20CORS=20=E4=B8=AD=E9=97=B4=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=94=AF=E6=8C=81=E8=B7=A8=E5=9F=9F=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=20-=20=E6=9B=B4=E6=96=B0=E4=BA=86=20hero.go=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=20API=20=E8=B7=AF=E5=BE=84=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E5=85=B6=E7=AC=A6=E5=90=88=E6=96=B0=E7=9A=84=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E8=A7=84=E8=8C=83=20-=20=E4=BC=98=E5=8C=96=E4=BA=86=20hero=5Fv?= =?UTF-8?q?1.go=20=E4=B8=AD=E7=9A=84=E5=93=8D=E5=BA=94=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=8C=E6=8F=90=E9=AB=98=E4=BA=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=95=88=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/hero/v1/hero.go | 8 +++----- internal/cmd/cmd.go | 6 ++++++ internal/controller/hero/hero_v1.go | 4 +--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/api/hero/v1/hero.go b/api/hero/v1/hero.go index bcffb94..b504fb4 100644 --- a/api/hero/v1/hero.go +++ b/api/hero/v1/hero.go @@ -15,14 +15,12 @@ type GetOneRes struct { // GetListReq GetListRes 列表返回所有角色 type GetListReq struct { - g.Meta `path:"/getList" method:"get" tags:"Hero" summary:"Get all hero"` -} -type GetListRes struct { - Records []*EpicHeroVO `json:"list"` // ✅ 返回一个数组 + g.Meta `path:"/app-api/epic/hero/list-all" method:"get" tags:"Hero" summary:"Get all hero"` } +type GetListRes []*EpicHeroVO 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"` } type GetDetailRes struct { diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index e861223..ca571e8 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -8,6 +8,11 @@ import ( "github.com/gogf/gf/v2/os/gcmd" ) +func CORS(r *ghttp.Request) { + r.Response.CORSDefault() + r.Middleware.Next() +} + var ( Main = gcmd.Command{ Name: "main", @@ -15,6 +20,7 @@ var ( Brief: "start http server", Func: func(ctx context.Context, parser *gcmd.Parser) (err error) { s := g.Server() + s.Use(CORS) s.Group("/", func(group *ghttp.RouterGroup) { group.Middleware(ghttp.MiddlewareHandlerResponse) group.Bind( diff --git a/internal/controller/hero/hero_v1.go b/internal/controller/hero/hero_v1.go index 2a4d7cc..cd62bd0 100644 --- a/internal/controller/hero/hero_v1.go +++ b/internal/controller/hero/hero_v1.go @@ -26,9 +26,7 @@ func (c *ControllerV1) GetList(ctx context.Context, req *v1.GetListReq) (res *v1 } // 构造响应 - res = &v1.GetListRes{ - Records: list, // ✅ 正确赋值字段 - } + res = (*v1.GetListRes)(&list) return res, nil }