81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"chatappgateway/internal/apperr"
|
|
commonpb "gitea.haiyihy.com/hy/chatappcommon/proto"
|
|
)
|
|
|
|
// Client 定义用户服务 gRPC 客户端能力。
|
|
type Client interface {
|
|
Register(ctx context.Context, request *commonpb.RegisterRequest) (*commonpb.RegisterResponse, error)
|
|
}
|
|
|
|
// Service 负责注册参数校验、字段归一化和下游调用。
|
|
type Service struct {
|
|
client Client
|
|
}
|
|
|
|
// RegisterRequest 描述 HTTP 层的注册入参。
|
|
type RegisterRequest struct {
|
|
Account string `json:"account"`
|
|
Password string `json:"password"`
|
|
CountryCode string `json:"country_code"`
|
|
VerifyCode string `json:"verify_code"`
|
|
Nickname string `json:"nickname"`
|
|
DeviceID string `json:"device_id"`
|
|
Platform string `json:"platform"`
|
|
AppVersion string `json:"app_version"`
|
|
}
|
|
|
|
// New 创建注册服务。
|
|
func New(client Client) *Service {
|
|
return &Service{client: client}
|
|
}
|
|
|
|
// Register 校验客户端请求并转成 gRPC 请求。
|
|
func (s *Service) Register(ctx context.Context, request RegisterRequest) (*commonpb.RegisterResponse, error) {
|
|
normalized, err := normalize(request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return s.client.Register(ctx, &commonpb.RegisterRequest{
|
|
Account: normalized.Account,
|
|
Password: normalized.Password,
|
|
CountryCode: normalized.CountryCode,
|
|
VerifyCode: normalized.VerifyCode,
|
|
Nickname: normalized.Nickname,
|
|
DeviceId: normalized.DeviceID,
|
|
Platform: normalized.Platform,
|
|
AppVersion: normalized.AppVersion,
|
|
})
|
|
}
|
|
|
|
func normalize(request RegisterRequest) (RegisterRequest, error) {
|
|
normalized := RegisterRequest{
|
|
Account: strings.TrimSpace(request.Account),
|
|
Password: strings.TrimSpace(request.Password),
|
|
CountryCode: strings.TrimSpace(request.CountryCode),
|
|
VerifyCode: strings.TrimSpace(request.VerifyCode),
|
|
Nickname: strings.TrimSpace(request.Nickname),
|
|
DeviceID: strings.TrimSpace(request.DeviceID),
|
|
Platform: strings.TrimSpace(request.Platform),
|
|
AppVersion: strings.TrimSpace(request.AppVersion),
|
|
}
|
|
|
|
if normalized.Account == "" {
|
|
return RegisterRequest{}, apperr.New(400, "bad_request", "account is required")
|
|
}
|
|
if normalized.Password == "" {
|
|
return RegisterRequest{}, apperr.New(400, "bad_request", "password is required")
|
|
}
|
|
if normalized.Nickname == "" {
|
|
normalized.Nickname = normalized.Account
|
|
}
|
|
|
|
return normalized, nil
|
|
}
|