From 58d02d0d50d7b6b56e1432f2c6b2b17ac5cd54a0 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 29 Jun 2023 11:15:42 +0800 Subject: [PATCH] fix --- .../internal/handler/getpickuplisthandler.go | 78 +++++++++++++++++++ server/inventory/internal/handler/routes.go | 5 ++ .../internal/logic/getpickuplistlogic.go | 34 ++++++++ server/inventory/internal/types/types.go | 15 +++- server_api/basic.api | 4 +- server_api/inventory.api | 15 +++- 6 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 server/inventory/internal/handler/getpickuplisthandler.go create mode 100644 server/inventory/internal/logic/getpickuplistlogic.go diff --git a/server/inventory/internal/handler/getpickuplisthandler.go b/server/inventory/internal/handler/getpickuplisthandler.go new file mode 100644 index 00000000..93caf33a --- /dev/null +++ b/server/inventory/internal/handler/getpickuplisthandler.go @@ -0,0 +1,78 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "fusenapi/server/inventory/internal/logic" + "fusenapi/server/inventory/internal/svc" + "fusenapi/server/inventory/internal/types" +) + +func GetPickupListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + if claims != nil { + // 从token中获取对应的用户信息 + userinfo, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + } else { + // 如果claims为nil,则认为用户身份为白板用户 + userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} + } + + var req types.GetPickupListReq + // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 + if err := httpx.Parse(r, &req); err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 510, + Message: "parameter error", + }) + logx.Info(err) + return + } + // 创建一个业务逻辑层实例 + l := logic.NewGetPickupListLogic(r.Context(), svcCtx) + resp := l.GetPickupList(&req, userinfo) + // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } + } +} diff --git a/server/inventory/internal/handler/routes.go b/server/inventory/internal/handler/routes.go index 1eca32bb..ac11f0b2 100644 --- a/server/inventory/internal/handler/routes.go +++ b/server/inventory/internal/handler/routes.go @@ -27,6 +27,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/inventory/supplement", Handler: SupplementHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/inventory/pick-up-list", + Handler: GetPickupListHandler(serverCtx), + }, }, ) } diff --git a/server/inventory/internal/logic/getpickuplistlogic.go b/server/inventory/internal/logic/getpickuplistlogic.go new file mode 100644 index 00000000..fff544ee --- /dev/null +++ b/server/inventory/internal/logic/getpickuplistlogic.go @@ -0,0 +1,34 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/inventory/internal/svc" + "fusenapi/server/inventory/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetPickupListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetPickupListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPickupListLogic { + return &GetPickupListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetPickupListLogic) GetPickupList(req *types.GetPickupListReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + return resp.SetStatus(basic.CodeOK) +} diff --git a/server/inventory/internal/types/types.go b/server/inventory/internal/types/types.go index fccafa7c..7b9a7b94 100644 --- a/server/inventory/internal/types/types.go +++ b/server/inventory/internal/types/types.go @@ -16,8 +16,8 @@ type TakeForm struct { } type GetCloudListReq struct { - Page int64 `form:"page"` - PageSize int64 `form:"page_size"` + Page int `form:"page"` + PageSize int `form:"page_size"` Size int64 `form:"size"` } @@ -63,6 +63,13 @@ type SupplementRsp struct { Sn string `json:"sn"` } +type GetPickupListReq struct { + Status int64 `form:"status,options=0|1|2|3|4"` + Page int `form:"page"` + PageSize int `form:"page_size"` + Size int `form:"size"` +} + type Request struct { } @@ -81,8 +88,8 @@ type Auth struct { type Pagnation struct { TotalCount int64 `json:"total_count"` TotalPage int64 `json:"total_page"` - CurPage int64 `json:"cur_page"` - PageSize int64 `json:"page_size"` + CurPage int `json:"cur_page"` + PageSize int `json:"page_size"` } // Set 设置Response的Code和Message值 diff --git a/server_api/basic.api b/server_api/basic.api index a827855d..13658fad 100644 --- a/server_api/basic.api +++ b/server_api/basic.api @@ -29,7 +29,7 @@ type Auth { type Pagnation{ TotalCount int64 `json:"total_count"` TotalPage int64 `json:"total_page"` - CurPage int64 `json:"cur_page"` - PageSize int64 `json:"page_size"` + CurPage int `json:"cur_page"` + PageSize int `json:"page_size"` } diff --git a/server_api/inventory.api b/server_api/inventory.api index eca56923..efd43195 100644 --- a/server_api/inventory.api +++ b/server_api/inventory.api @@ -18,6 +18,9 @@ service inventory { //云仓补货 @handler SupplementHandler post /inventory/supplement(SupplementReq) returns (response); + //提货列表 + @handler GetPickupListHandler + get /inventory/pick-up-list(GetPickupListReq) returns (response); } //提取云仓货物 @@ -31,8 +34,8 @@ type TakeForm { } //获取云仓库存列表 type GetCloudListReq { - Page int64 `form:"page"` - PageSize int64 `form:"page_size"` + Page int `form:"page"` + PageSize int `form:"page_size"` Size int64 `form:"size"` } type GetCloudListRsp { @@ -73,4 +76,12 @@ type SupplementReq { } type SupplementRsp { Sn string `json:"sn"` +} + +//提货列表 +type GetPickupListReq { + Status int64 `form:"status,options=0|1|2|3|4"` + Page int `form:"page"` + PageSize int `form:"page_size"` + Size int `form:"size"` } \ No newline at end of file