feat: bff resp string to json
CI / changes (push) Successful in 1s
CI / docker-bff (push) Successful in 1s
CI / docker-product (push) Failing after 1s

This commit is contained in:
zzw
2026-08-07 11:57:43 +08:00
parent cf49ec56c7
commit 39fb09e7df
9 changed files with 192 additions and 188 deletions
+10 -11
View File
@@ -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")
}