This commit is contained in:
laodaming 2023-06-06 16:14:02 +08:00
parent 245155d10e
commit 29f6e2ddbf
4 changed files with 49 additions and 43 deletions

View File

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

View File

@ -8,55 +8,48 @@ import (
{{.types}}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response{
func (resp *Response) Set(Code int, Message string) {
resp.Code = Code
resp.Message = Message
return resp
}
// Set 设置整个Response
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response{
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) {
resp.Code = Code
resp.Message = Message
resp.Data = Data
return resp
}
// SetMessage 设置Response的Message
func (resp *Response) SetMessage(msg string) *Response{
func (resp *Response) SetMessage(msg string) {
resp.Message = msg
return resp
}
// SetWithData 设置Data
func (resp *Response) SetData(Data interface{}) *Response{
func (resp *Response) SetData(Data interface{}) {
resp.Data = Data
return resp
}
// SetWithData 设置Response的Code和Message值 带Data入参数
func (resp *Response) SetCode(Code int) *Response{
func (resp *Response) SetCode(Code int) {
resp.Code = Code
return resp
}
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response{
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]
}
return resp
}
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response{
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]
}
return resp
}

View File

@ -36,18 +36,22 @@ func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
// 获取产品列表
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo auth.UserInfo) (resp *types.Response) {
resp = &types.Response{}
loginInfo.UserId = 83
//校验前台登录情况
if loginInfo.UserId == 0 {
return &types.Response{Code: 401, Message: "please sign in"}
resp.Set(constants.CODE_UNAUTH, "please sign in")
return
}
//如果是demo
if req.IsDemo == 1 {
var demo types.GetProductListRsp
if err := json.Unmarshal([]byte(constants.PRODUCT_LIST_DEMO), &demo); err != nil {
logx.Error(err)
return &types.Response{Code: 510, Message: "demo data format err"}
resp.Set(constants.CODE_SERVICE_ERR, "demo data format err")
}
return &types.Response{Code: 200, Message: "success", Data: demo}
resp.SetWithData(constants.CODE_OK, "success", demo)
return
}
if req.Page <= 0 {
req.Page = 1
@ -61,22 +65,25 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
logx.Error(err)
return &types.Response{Code: 510, Message: "get user info err"}
resp.Set(constants.CODE_SERVICE_ERR, "get user info err")
return
}
if userInfo == nil {
return &types.Response{Code: 401, Message: "user not exists"}
resp.Set(constants.CODE_UNAUTH, "user not exists")
return
}
//查询符合的产品列表
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), 0, 1, "sort-desc")
if err != nil {
logx.Error(err)
return &types.Response{Code: 510, Message: "failed to get product list"}
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list")
return
}
fmt.Println(len(productList))
productLen := len(productList)
if productLen == 0 {
return &types.Response{Code: 200, Message: "success"}
resp.Set(constants.CODE_OK, "success")
return
}
//提取产品ids
productIds := make([]string, 0, productLen)
@ -87,7 +94,8 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
productPriceList, err := productPriceModel.GetPriceList(l.ctx, productIds)
if err != nil {
logx.Error(err)
return &types.Response{Code: 510, Message: "failed to get product min price list"}
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product min price list")
return
}
//存储产品最小价格
mapProductMinPrice := make(map[int64]int64)
@ -96,7 +104,8 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
priceSlice, err := format.StrSlicToIntSlice(priceStrSlic)
if err != nil {
logx.Error(err)
return &types.Response{Code: 510, Message: err.Error()}
resp.Set(constants.CODE_SERVICE_ERR, err.Error())
return
}
if len(priceSlice) == 0 {
continue
@ -109,7 +118,8 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
productTemplatesV2, err := productTemplateModel.FindAllByCondition(l.ctx, productIds)
if err != nil {
logx.Error(err)
return &types.Response{Code: 510, Message: "get product template_v2 err"}
resp.Set(constants.CODE_SERVICE_ERR, "get product template_v2 err")
return
}
mapProductTemplate := make(map[int64]struct{})
for _, v := range productTemplatesV2 {
@ -120,7 +130,8 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
tagInfo, err := tagsModel.FindOne(l.ctx, req.Cid)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
logx.Error(err)
return &types.Response{Code: 510, Message: "get classification err "}
resp.Set(constants.CODE_SERVICE_ERR, "get tag err")
return
}
if tagInfo == nil {
return &types.Response{Code: 510, Message: "classification not exists "}
@ -130,7 +141,8 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
productSizeCount, err := productSizeModel.CountByStatus(l.ctx, 1)
if err != nil {
logx.Error(err)
return &types.Response{Code: 510, Message: "get product size count err "}
resp.Set(constants.CODE_SERVICE_ERR, "get product size count err")
return
}
//拼接返回
itemList := make([]types.Items, 0, productLen)
@ -176,10 +188,10 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
}
itemList = append(itemList, item)
}
return &types.Response{Code: 200, Message: "success", Data: types.GetProductListRsp{
resp.SetWithData(constants.CODE_OK, "success", types.GetProductListRsp{
Ob: types.Ob{
Items: itemList,
}, TypeName: tagInfo.Title, Description: tagInfo.Description,
}}
})
return
}

View File

@ -79,54 +79,47 @@ type Response struct {
}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response {
func (resp *Response) Set(Code int, Message string) {
resp.Code = Code
resp.Message = Message
return resp
}
// Set 设置整个Response
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) {
resp.Code = Code
resp.Message = Message
resp.Data = Data
return resp
}
// SetMessage 设置Response的Message
func (resp *Response) SetMessage(msg string) *Response {
func (resp *Response) SetMessage(msg string) {
resp.Message = msg
return resp
}
// SetWithData 设置Data
func (resp *Response) SetData(Data interface{}) *Response {
func (resp *Response) SetData(Data interface{}) {
resp.Data = Data
return resp
}
// SetWithData 设置Response的Code和Message值 带Data入参数
func (resp *Response) SetCode(Code int) *Response {
func (resp *Response) SetCode(Code int) {
resp.Code = Code
return resp
}
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
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]
}
return resp
}
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
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]
}
return resp
}