2026-07-05 01:55:03 +08:00

55 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package user
import (
"context"
"strings"
"unicode/utf8"
"hyapp/pkg/xerr"
userdomain "hyapp/services/user-service/internal/domain/user"
)
const (
BusinessSceneManagerResourceGrant = "manager_resource_grant"
BusinessSceneManagerVIPGrant = "manager_vip_grant"
defaultBusinessLookupPageSize = 20
maxBusinessLookupPageSize = 20
)
// BusinessUserLookup 执行业务场景用户查询。调用方必须先完成该 scene 的能力校验;
// service 层仍校验 scene 和 keyword避免未来入口把它误用成全局用户搜索。
func (s *Service) BusinessUserLookup(ctx context.Context, scene string, keyword string, pageSize int32) ([]userdomain.BusinessUserLookupItem, error) {
scene = strings.ToLower(strings.TrimSpace(scene))
if scene != BusinessSceneManagerResourceGrant && scene != BusinessSceneManagerVIPGrant {
return nil, xerr.New(xerr.InvalidArgument, "business lookup scene is invalid")
}
if s.userRepository == nil {
return nil, xerr.New(xerr.Unavailable, "user repository is not configured")
}
keyword = strings.TrimSpace(keyword)
numericKeyword := isASCIIInteger(keyword)
if keyword == "" || (!numericKeyword && utf8.RuneCountInString(keyword) < 2) {
return nil, xerr.New(xerr.InvalidArgument, "keyword is invalid")
}
if pageSize <= 0 {
pageSize = defaultBusinessLookupPageSize
}
if pageSize > maxBusinessLookupPageSize {
pageSize = maxBusinessLookupPageSize
}
return s.userRepository.BusinessUserLookup(ctx, keyword, numericKeyword, pageSize)
}
func isASCIIInteger(value string) bool {
if value == "" {
return false
}
for _, ch := range value {
if ch < '0' || ch > '9' {
return false
}
}
return true
}