From 5b3e3f047e2a0042eba5771a119fd8c1db376462 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 10:45:53 +0800 Subject: [PATCH] info address --- .../internal/handler/addresslisthandler.go | 35 ++++++++++++ server/info/internal/handler/routes.go | 7 +++ .../info/internal/logic/addresslistlogic.go | 56 +++++++++++++++++++ server_api/info.api | 11 ++-- 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 server/info/internal/handler/addresslisthandler.go create mode 100644 server/info/internal/logic/addresslistlogic.go diff --git a/server/info/internal/handler/addresslisthandler.go b/server/info/internal/handler/addresslisthandler.go new file mode 100644 index 00000000..c496bee7 --- /dev/null +++ b/server/info/internal/handler/addresslisthandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/info/internal/logic" + "fusenapi/server/info/internal/svc" + "fusenapi/server/info/internal/types" +) + +func AddressListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.Request + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewAddressListLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.AddressList(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/info/internal/handler/routes.go b/server/info/internal/handler/routes.go index c259faea..82d01e1c 100644 --- a/server/info/internal/handler/routes.go +++ b/server/info/internal/handler/routes.go @@ -2,6 +2,7 @@ package handler import ( + "net/http" "fusenapi/server/info/internal/svc" @@ -10,6 +11,7 @@ import ( ) func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { + server.AddRoutes( []rest.Route{ { @@ -37,6 +39,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/info/address/delete", Handler: AddressDeleteHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/info/address/list", + Handler: AddressListHandler(serverCtx), + }, }, ) } diff --git a/server/info/internal/logic/addresslistlogic.go b/server/info/internal/logic/addresslistlogic.go new file mode 100644 index 00000000..31891b26 --- /dev/null +++ b/server/info/internal/logic/addresslistlogic.go @@ -0,0 +1,56 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/info/internal/svc" + "fusenapi/server/info/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type AddressListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAddressListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddressListLogic { + return &AddressListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *AddressListLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *AddressListLogic) AddressList(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + if !userinfo.IsUser() { + return resp.SetStatus(basic.CodeUnAuth) + } + + m := l.svcCtx.AllModels.FsAddress // 创建地址模型 + result, err := m.GetUserAllAddress(l.ctx, userinfo.UserId) + if err != nil { + logx.Error(err) // 日志记录错误 + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, err.Error()) // 返回数据库创建错误 + } + + return resp.SetStatus(basic.CodeOK, map[string]any{ + "address_list": result, + }) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *AddressListLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server_api/info.api b/server_api/info.api index 4a4cf6b5..dfadc0b2 100644 --- a/server_api/info.api +++ b/server_api/info.api @@ -12,18 +12,21 @@ import "basic.api" service info { @handler InfoHandler post /api/info/user(UserInfoRequest) returns (response); - + @handler AddressDefaultHandler post /api/info/address/default(AddressIdRequest) returns (response); - + @handler AddressAddHandler post /api/info/address/add(AddressNameRequest) returns (response); - + @handler AddressUpdateHandler post /api/info/address/update(AddressObjectRequest) returns (response); - + @handler AddressDeleteHandler post /api/info/address/delete(AddressIdRequest) returns (response); + + @handler AddressListHandler + get /api/info/address/list(request) returns (response); } type (