feat: bff resp string to json
This commit is contained in:
@@ -3,6 +3,7 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -14,10 +15,9 @@ import (
|
||||
"pkg.local/log"
|
||||
"pkg.local/modelbase"
|
||||
"pkg.local/redis"
|
||||
"pkg.local/utils"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -35,7 +35,7 @@ func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
func (l *AddLogic) Add(in *product.AddReq) (*product.Response, error) {
|
||||
l.Info("zero logx 方式 ... ")
|
||||
log.Info("原始 zap 方式 ... ")
|
||||
|
||||
@@ -45,7 +45,7 @@ func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
if in.GetAgentPrice() == 0 || in.GetStorePrice() == 0 ||
|
||||
in.GetSalePrice() == 0 || in.GetSharePrice() == 0 ||
|
||||
in.GetSaleReward() == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "缺少相关价格")
|
||||
return failResponse(utils.ErrorMissingParams), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
if len(in.GetPublishTime()) > 1 {
|
||||
t, err := parsePublishTime(in.GetPublishTime())
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "时间错误")
|
||||
return msgResponse(utils.Ok.Code, "时间错误"), nil
|
||||
}
|
||||
publishTime = t
|
||||
}
|
||||
@@ -64,10 +64,10 @@ func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
}, &check)
|
||||
if err != nil {
|
||||
log.Errorf("check product name: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询失败")
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
if check.Id > 0 {
|
||||
return nil, status.Error(codes.AlreadyExists, "数据已存在")
|
||||
return failResponse(utils.ErrorDataIsExist), nil
|
||||
}
|
||||
|
||||
// 管理员信息后续从 ctx / metadata 取
|
||||
@@ -158,10 +158,9 @@ func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("create product: %v", err)
|
||||
return nil, status.Error(codes.Internal, "创建失败")
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
// redis 用法示例
|
||||
key := redis.GetRedisKey("product:last_add")
|
||||
if err := redis.Client.Set(l.ctx, key, in.GetName(), time.Hour).Err(); err != nil {
|
||||
log.Errorf("redis set %s: %v", key, err)
|
||||
@@ -169,7 +168,7 @@ func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
log.Infof("add product success id=%d", data.Id)
|
||||
}
|
||||
|
||||
return &product.AddResp{}, nil
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
}
|
||||
|
||||
func parsePublishTime(s string) (time.Time, error) {
|
||||
@@ -178,5 +177,5 @@ func parsePublishTime(s string) (time.Time, error) {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
return time.Time{}, status.Error(codes.InvalidArgument, "invalid time format")
|
||||
return time.Time{}, errors.New("invalid time format")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user