This commit is contained in:
laodaming 2023-06-07 12:21:07 +08:00
parent 51c7478b94
commit 3d21e2491f
4 changed files with 46 additions and 121 deletions

View File

@ -1,8 +0,0 @@
package constants
// api接口code响应码
const (
CODE_UNAUTH = 401 //未授权
CODE_SERVICE_ERR = 510 //内部代码错误
CODE_OK = 200 //ok
)

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}