From 3d21e2491f451589d90cbd7b96f518ea8c578c39 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 7 Jun 2023 12:21:07 +0800 Subject: [PATCH] fix --- constants/api_response_code.go | 8 -- product/internal/logic/getproductlistlogic.go | 40 +++---- .../logic/getsuccessrecommandlogic.go | 19 ++-- product/internal/types/types.go | 100 +++++------------- 4 files changed, 46 insertions(+), 121 deletions(-) delete mode 100644 constants/api_response_code.go diff --git a/constants/api_response_code.go b/constants/api_response_code.go deleted file mode 100644 index 28cfa20b..00000000 --- a/constants/api_response_code.go +++ /dev/null @@ -1,8 +0,0 @@ -package constants - -// api接口code响应码 -const ( - CODE_UNAUTH = 401 //未授权 - CODE_SERVICE_ERR = 510 //内部代码错误 - CODE_OK = 200 //ok -) diff --git a/product/internal/logic/getproductlistlogic.go b/product/internal/logic/getproductlistlogic.go index e691a203..f5e45356 100644 --- a/product/internal/logic/getproductlistlogic.go +++ b/product/internal/logic/getproductlistlogic.go @@ -10,6 +10,7 @@ import ( "fusenapi/product/internal/svc" "fusenapi/product/internal/types" "fusenapi/utils/auth" + "fusenapi/utils/basic" "fusenapi/utils/format" "fusenapi/utils/image" "github.com/zeromicro/go-zero/core/logx" @@ -37,17 +38,16 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp resp = &types.Response{} loginInfo := auth.GetUserInfoFormCtx(l.ctx) if loginInfo.UserId == 0 { - resp.Set(constants.CODE_SERVICE_ERR, "get login user info err") + return resp.SetStatus(basic.CodeServiceErr, "get login user info err") } //如果是demo if req.IsDemo == 1 { var demo types.GetProductListRsp if err := json.Unmarshal([]byte(constants.PRODUCT_LIST_DEMO), &demo); err != nil { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, "demo data format err") + return resp.SetStatus(basic.CodeServiceErr, "demo data format err") } - resp.SetWithData(constants.CODE_OK, "success", demo) - return + return resp.SetStatusWithMessage(basic.CodeOK, "success", demo) } if req.Page <= 0 { req.Page = 1 @@ -61,25 +61,21 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId) if err != nil && !errors.Is(err, sqlc.ErrNotFound) { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, "get user info err") - return + return resp.SetStatus(basic.CodeServiceErr, "get user info err") } if userInfo == nil { - resp.Set(constants.CODE_UNAUTH, "user not exists") - return + return resp.SetStatus(basic.CodeUnAuth, "user not exists") } //查询符合的产品列表 productModel := model.NewFsProductModel(l.svcCtx.MysqlConn) productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), "sort-desc") if err != nil { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list") - return + return resp.SetStatus(basic.CodeServiceErr, "failed to get product list") } productLen := len(productList) if productLen == 0 { - resp.Set(constants.CODE_OK, "success") - return + return resp.SetStatus(basic.CodeOK, "success") } //提取产品ids productIds := make([]string, 0, productLen) @@ -90,8 +86,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp productPriceList, err := productPriceModel.GetPriceList(l.ctx, productIds) if err != nil { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, "failed to get product min price list") - return + return resp.SetStatus(basic.CodeServiceErr, "failed to get product min price list") } //存储产品最小价格 mapProductMinPrice := make(map[int64]int64) @@ -100,8 +95,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp priceSlice, err := format.StrSlicToIntSlice(priceStrSlic) if err != nil { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, err.Error()) - return + return resp.SetStatus(basic.CodeServiceErr, err.Error()) } if len(priceSlice) == 0 { continue @@ -114,8 +108,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp productTemplatesV2, err := productTemplateModel.FindAllByCondition(l.ctx, productIds) if err != nil { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, "get product template_v2 err") - return + return resp.SetStatus(basic.CodeServiceErr, "get product template_v2 err") } mapProductTemplate := make(map[int64]struct{}) for _, v := range productTemplatesV2 { @@ -126,19 +119,17 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp tagInfo, err := tagsModel.FindOne(l.ctx, req.Cid) if err != nil && !errors.Is(err, sqlc.ErrNotFound) { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, "get tag err") - return + return resp.SetStatus(basic.CodeServiceErr, "get tag err") } if tagInfo == nil { - return &types.Response{Code: 510, Message: "classification not exists "} + return resp.SetStatus(basic.CodeServiceErr, "tag is not exists") } //获取产品尺寸数量 productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn) productSizeCount, err := productSizeModel.CountByStatus(l.ctx, 1) if err != nil { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, "get product size count err") - return + return resp.SetStatus(basic.CodeServiceErr, "get product size count err") } //拼接返回 itemList := make([]types.Items, 0, productLen) @@ -175,10 +166,9 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp item.CoverDefault = thousandFaceImageFormatReq.CoverDefault itemList = append(itemList, item) } - resp.SetWithData(constants.CODE_OK, "success", types.GetProductListRsp{ + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetProductListRsp{ Ob: types.Ob{ Items: itemList, }, TypeName: tagInfo.Title, Description: tagInfo.Description, }) - return } diff --git a/product/internal/logic/getsuccessrecommandlogic.go b/product/internal/logic/getsuccessrecommandlogic.go index 4ea68752..13fb85b2 100644 --- a/product/internal/logic/getsuccessrecommandlogic.go +++ b/product/internal/logic/getsuccessrecommandlogic.go @@ -3,11 +3,11 @@ package logic import ( "context" "errors" - "fusenapi/constants" "fusenapi/model" "fusenapi/product/internal/svc" "fusenapi/product/internal/types" "fusenapi/utils/auth" + "fusenapi/utils/basic" "fusenapi/utils/image" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/sqlx" @@ -32,19 +32,17 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco resp = &types.Response{} loginInfo := auth.GetUserInfoFormCtx(l.ctx) if loginInfo.UserId == 0 { - resp.Set(constants.CODE_SERVICE_ERR, "get login user info err") + return resp.SetStatus(basic.CodeUnAuth, "get login user info err") } //获取用户信息 userModel := model.NewFsUserModel(l.svcCtx.MysqlConn) userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId) if err != nil && errors.Is(err, sqlx.ErrNotFound) { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, "failed to get user info") - return + return resp.SetStatus(basic.CodeServiceErr, "failed to get user info") } if userInfo == nil { - resp.Set(constants.CODE_UNAUTH, "failed to get user info") - return + return resp.SetStatus(basic.CodeUnAuth, "failed to get user info") } if req.Num == 0 || req.Num > 500 { req.Num = 8 @@ -57,13 +55,11 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num)) if err != nil { logx.Error(err) - resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list") - return + return resp.SetStatus(basic.CodeServiceErr, "failed to get product list") } //没有推荐产品就返回 if len(productList) == 0 { - resp.Set(constants.CODE_OK, "success") - return + return resp.SetStatus(basic.CodeOK, "success") } list := make([]types.GetSuccessRecommandRsp, 0, len(productList)) for _, v := range productList { @@ -89,6 +85,5 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco data.CoverDefault = thousandFaceImageFormatReq.CoverDefault list = append(list, data) } - resp.SetWithData(constants.CODE_OK, "success", list) - return + return resp.SetStatusWithMessage(basic.CodeOK, "success", list) } diff --git a/product/internal/types/types.go b/product/internal/types/types.go index 1a4d06d1..2c27bdca 100644 --- a/product/internal/types/types.go +++ b/product/internal/types/types.go @@ -93,93 +93,41 @@ type Auth struct { } // Set 设置Response的Code和Message值 -func (resp *Response) Set(Code int, Message string) { - resp.Code = Code - resp.Message = Message +func (resp *Response) Set(Code int, Message string) *Response { + return &Response{ + Code: Code, + Message: Message, + } } // Set 设置整个Response -func (resp *Response) SetWithData(Code int, Message string, Data interface{}) { - resp.Code = Code - resp.Message = Message - resp.Data = Data -} - -// SetMessage 设置Response的Message -func (resp *Response) SetMessage(msg string) { - resp.Message = msg -} - -// SetWithData 设置Data -func (resp *Response) SetData(Data interface{}) { - resp.Data = Data -} - -// SetWithData 设置Response的Code和Message值 带Data入参数 -func (resp *Response) SetCode(Code int) { - resp.Code = Code +func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response { + return &Response{ + Code: Code, + Message: Message, + Data: Data, + } } // SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 -func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) { - resp.Code = sr.Code - resp.Message = sr.Message - if len(data) == 1 { - resp.Data = data[0] +func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp } // SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数 -func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) { - resp.Code = sr.Code - resp.Message = msg - if len(data) == 1 { - resp.Data = data[0] +func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + Message: sr.Message, } -} - -// Set 设置Response的Code和Message值 -func (resp *ResponseJwt) Set(Code int, Message string) { - resp.Code = Code - resp.Message = Message -} - -// Set 设置整个Response -func (resp *ResponseJwt) SetWithData(Code int, Message string, Data interface{}) { - resp.Code = Code - resp.Message = Message - resp.Data = Data -} - -// SetMessage 设置Response的Message -func (resp *ResponseJwt) SetMessage(msg string) { - resp.Message = msg -} - -// SetWithData 设置Data -func (resp *ResponseJwt) SetData(Data interface{}) { - resp.Data = Data -} - -// SetWithData 设置Response的Code和Message值 带Data入参数 -func (resp *ResponseJwt) SetCode(Code int) { - resp.Code = Code -} - -// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 -func (resp *ResponseJwt) SetStatus(sr *basic.StatusResponse, data ...interface{}) { - resp.Code = sr.Code - resp.Message = sr.Message if len(data) == 1 { - resp.Data = data[0] - } -} - -// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数 -func (resp *ResponseJwt) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) { - resp.Code = sr.Code - resp.Message = msg - if len(data) == 1 { - resp.Data = data[0] + newResp.Data = data[0] } + return newResp }