fix: 统一所有 RPC 分页校验为 max=100 并补齐默认值

This commit is contained in:
zetaloop
2026-04-24 06:31:40 +08:00
parent 37faf1c920
commit 809dbf2cac
15 changed files with 85 additions and 16 deletions
@@ -2,6 +2,7 @@ package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
@@ -28,6 +29,9 @@ func (l *SearchCommentLikesLogic) SearchCommentLikes(in *pb.SearchCommentLikesRe
if limit <= 0 {
limit = 20
}
if limit > 100 {
return nil, errors.New("limit too large")
}
offset := in.GetOffset()
if offset < 0 {
offset = 0
@@ -2,6 +2,7 @@ package logic
import (
"context"
"errors"
"strings"
"juwan-backend/app/community/rpc/internal/svc"
@@ -29,6 +30,9 @@ func (l *SearchCommentsLogic) SearchComments(in *pb.SearchCommentsReq) (*pb.Sear
if limit <= 0 {
limit = 20
}
if limit > 100 {
return nil, errors.New("limit too large")
}
offset := in.GetOffset()
if offset < 0 {
offset = 0
@@ -2,6 +2,7 @@ package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
@@ -28,6 +29,9 @@ func (l *SearchPostLikesLogic) SearchPostLikes(in *pb.SearchPostLikesReq) (*pb.S
if limit <= 0 {
limit = 20
}
if limit > 100 {
return nil, errors.New("limit too large")
}
offset := in.GetOffset()
if offset < 0 {
offset = 0
@@ -2,6 +2,7 @@ package logic
import (
"context"
"errors"
"strings"
"juwan-backend/app/community/rpc/internal/svc"
@@ -29,6 +30,9 @@ func (l *SearchPostsLogic) SearchPosts(in *pb.SearchPostsReq) (*pb.SearchPostsRe
if limit <= 0 {
limit = 20
}
if limit > 100 {
return nil, errors.New("limit too large")
}
offset := in.GetOffset()
if offset < 0 {
offset = 0
@@ -27,8 +27,14 @@ func NewSearchGamesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Searc
func (l *SearchGamesLogic) SearchGames(in *pb.SearchGamesReq) (*pb.SearchGamesResp, error) {
notFoundErr := entcache.ErrNotFound
if in.Offset < 0 || in.Limit <= 0 || in.Limit > 100 {
return nil, errors.New("invalid pagination parameters")
if in.Limit <= 0 {
in.Limit = 20
}
if in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset < 0 {
in.Offset = 0
}
all, err := l.svcCtx.GameModelRO.Games.Query().Limit(int(in.Limit)).Offset(int(in.Offset)).All(l.ctx)
if err != nil && !errors.As(err, &notFoundErr) {
@@ -31,9 +31,12 @@ func (l *SearchOrderStateLogsLogic) SearchOrderStateLogs(in *pb.SearchOrderState
if in.Limit <= 0 {
in.Limit = 20
}
if in.Limit > 1000 {
if in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset < 0 {
in.Offset = 0
}
preds := make([]predicate.OrderStateLogs, 0, 8)
if in.Id != nil {
@@ -31,9 +31,12 @@ func (l *SearchOrdersLogic) SearchOrders(in *pb.SearchOrdersReq) (*pb.SearchOrde
if in.Limit <= 0 {
in.Limit = 20
}
if in.Limit > 1000 {
if in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset < 0 {
in.Offset = 0
}
preds := make([]predicate.Orders, 0, 20)
if in.Id != nil {
@@ -25,9 +25,15 @@ func NewSearchPlayerServicesLogic(ctx context.Context, svcCtx *svc.ServiceContex
}
func (l *SearchPlayerServicesLogic) SearchPlayerServices(in *pb.SearchPlayerServicesReq) (*pb.SearchPlayerServicesResp, error) {
if in.Limit > 1000 {
if in.Limit <= 0 {
in.Limit = 20
}
if in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset < 0 {
in.Offset = 0
}
update := l.svcCtx.PlayerModelRO.PlayerServices.Query()
if in.PlayerId != 0 {
@@ -26,6 +26,18 @@ func NewSearchPlayersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sea
}
func (l *SearchPlayersLogic) SearchPlayers(in *pb.SearchPlayersReq) (*pb.SearchPlayersResp, error) {
if in.Limit == nil || *in.Limit <= 0 {
def := int64(20)
in.Limit = &def
}
if *in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset == nil || *in.Offset < 0 {
zero := int64(0)
in.Offset = &zero
}
searcher := l.svcCtx.PlayerModelRO.Players.Query()
if in.Gender != nil && *in.Gender != 0 {
@@ -30,9 +30,12 @@ func (l *SearchShopInvitationsLogic) SearchShopInvitations(in *pb.SearchShopInvi
if in.Limit <= 0 {
in.Limit = 20
}
if in.Limit > 1000 {
if in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset < 0 {
in.Offset = 0
}
preds := make([]predicate.ShopInvitations, 0, 8)
if in.Id > 0 {
@@ -30,9 +30,12 @@ func (l *SearchShopPlayersLogic) SearchShopPlayers(in *pb.SearchShopPlayersReq)
if in.Limit <= 0 {
in.Limit = 20
}
if in.Limit > 1000 {
if in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset < 0 {
in.Offset = 0
}
preds := make([]predicate.ShopPlayers, 0, 8)
if in.ShopId > 0 {
@@ -34,9 +34,12 @@ func (l *SearchShopsLogic) SearchShops(in *pb.SearchShopsReq) (*pb.SearchShopsRe
if in.Limit <= 0 {
in.Limit = 20
}
if in.Limit > 1000 {
if in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset < 0 {
in.Offset = 0
}
preds := make([]predicate.Shops, 0, 12)
if in.Id > 0 {
@@ -28,9 +28,14 @@ func NewSearchUserVerificationsLogic(ctx context.Context, svcCtx *svc.ServiceCon
}
func (l *SearchUserVerificationsLogic) SearchUserVerifications(in *pb.SearchUserVerificationsReq) (*pb.SearchUserVerificationsResp, error) {
if in.Limit > 1000 {
logx.Errorf("Limit exceeds max limit: %d", in.Limit)
return nil, errors.New("limit exceeds max limit")
if in.Limit <= 0 {
in.Limit = 20
}
if in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset < 0 {
in.Offset = 0
}
predicates := make([]predicate.UserVerifications, 0, 3)
if in.UserId != 0 {
@@ -31,12 +31,15 @@ var SearUsersErr = errors.New("search users failed")
func (l *SearchUsersLogic) SearchUsers(in *pb.SearchUsersReq) (out *pb.SearchUsersResp, err error) {
if in.Offset == nil || *in.Offset < 0 {
logx.Errorf("invalid offset: %v", in.Offset)
return nil, errors.New("invalid offset")
zero := int64(0)
in.Offset = &zero
}
if *in.Limit > 1000 {
logx.Errorf("Limit exceeds max limit: %d", in.Limit)
return nil, errors.New("limit exceeds max limit")
if in.Limit == nil || *in.Limit <= 0 {
def := int64(20)
in.Limit = &def
}
if *in.Limit > 100 {
return nil, errors.New("limit too large")
}
user, err := l.svcCtx.UsersModelRO.Users.Query().
Where(users.Or(
@@ -34,6 +34,12 @@ func (l *SearchWalletTransactionsLogic) SearchWalletTransactions(in *pb.SearchWa
if in.Limit <= 0 {
in.Limit = 20
}
if in.Limit > 100 {
return nil, errors.New("limit too large")
}
if in.Offset < 0 {
in.Offset = 0
}
preds := make([]predicate.WalletTransactions, 0, 8)
if in.Id > 0 {