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")
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"product/internal/svc"
|
||||
"product/product"
|
||||
|
||||
"pkg.local/utils"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -23,8 +25,8 @@ func NewEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditLogic {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *EditLogic) Edit(in *product.EditReq) (*product.EditResp, error) {
|
||||
func (l *EditLogic) Edit(in *product.EditReq) (*product.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &product.EditResp{}, nil
|
||||
_ = in
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@ import (
|
||||
validateService "pkg.local/validate"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type ItemsLogic struct {
|
||||
@@ -34,10 +32,10 @@ func NewItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ItemsLogic
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ItemsLogic) Items(in *product.ItemsReq) (*product.ItemsResp, error) {
|
||||
func (l *ItemsLogic) Items(in *product.ItemsReq) (*product.Response, error) {
|
||||
var req validator.ProductItemsValidator
|
||||
if msg := validateService.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
||||
return nil, status.Error(codes.InvalidArgument, msg)
|
||||
return msgResponse(utils.Ok.Code, msg), nil
|
||||
}
|
||||
|
||||
page := int(req.Page)
|
||||
@@ -64,7 +62,7 @@ func (l *ItemsLogic) Items(in *product.ItemsReq) (*product.ItemsResp, error) {
|
||||
result, err := model.ProductModel{}.Init().Page(w, &info)
|
||||
if err != nil {
|
||||
log.Errorf("product items: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询失败")
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
items := make([]*product.Item, 0, len(info))
|
||||
@@ -72,10 +70,10 @@ func (l *ItemsLogic) Items(in *product.ItemsReq) (*product.ItemsResp, error) {
|
||||
items = append(items, toProductItem(row))
|
||||
}
|
||||
|
||||
return &product.ItemsResp{
|
||||
return okResponse(utils.StructToJson(&product.ItemsData{
|
||||
Count: result.Count,
|
||||
Items: items,
|
||||
}, nil
|
||||
})), nil
|
||||
}
|
||||
|
||||
func toProductItem(row dao.ProductInfo) *product.Item {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"product/product"
|
||||
|
||||
"pkg.local/utils"
|
||||
)
|
||||
|
||||
func okResponse(data string) *product.Response {
|
||||
return &product.Response{
|
||||
Code: utils.Ok.Code,
|
||||
Msg: utils.Ok.Msg,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func failResponse(err utils.Error) *product.Response {
|
||||
return &product.Response{
|
||||
Code: int32(err.GetCode()),
|
||||
Msg: err.GetMsg(),
|
||||
}
|
||||
}
|
||||
|
||||
func msgResponse(code int32, msg string) *product.Response {
|
||||
return &product.Response{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
// Source: product.proto
|
||||
|
||||
package server
|
||||
@@ -23,17 +22,17 @@ func NewProductServer(svcCtx *svc.ServiceContext) *ProductServer {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ProductServer) Add(ctx context.Context, in *product.AddReq) (*product.AddResp, error) {
|
||||
func (s *ProductServer) Add(ctx context.Context, in *product.AddReq) (*product.Response, error) {
|
||||
l := logic.NewAddLogic(ctx, s.svcCtx)
|
||||
return l.Add(in)
|
||||
}
|
||||
|
||||
func (s *ProductServer) Edit(ctx context.Context, in *product.EditReq) (*product.EditResp, error) {
|
||||
func (s *ProductServer) Edit(ctx context.Context, in *product.EditReq) (*product.Response, error) {
|
||||
l := logic.NewEditLogic(ctx, s.svcCtx)
|
||||
return l.Edit(in)
|
||||
}
|
||||
|
||||
func (s *ProductServer) Items(ctx context.Context, in *product.ItemsReq) (*product.ItemsResp, error) {
|
||||
func (s *ProductServer) Items(ctx context.Context, in *product.ItemsReq) (*product.Response, error) {
|
||||
l := logic.NewItemsLogic(ctx, s.svcCtx)
|
||||
return l.Items(in)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user