102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
|
|
package grpc
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"google.golang.org/grpc"
|
||
|
|
|
||
|
|
pb "github.com/muyuqingfeng/iloom/shared/pkg/grpc/pb/system"
|
||
|
|
)
|
||
|
|
|
||
|
|
// SystemClient wraps the generated SystemServiceClient with convenience methods.
|
||
|
|
type SystemClient struct {
|
||
|
|
conn *grpc.ClientConn
|
||
|
|
Raw pb.SystemServiceClient
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewSystemClient creates a SystemClient connected to the given address.
|
||
|
|
func NewSystemClient(addr string) (*SystemClient, error) {
|
||
|
|
conn, err := Dial(addr)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &SystemClient{
|
||
|
|
conn: conn,
|
||
|
|
Raw: pb.NewSystemServiceClient(conn),
|
||
|
|
}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Close closes the underlying gRPC connection.
|
||
|
|
func (c *SystemClient) Close() error {
|
||
|
|
return c.conn.Close()
|
||
|
|
}
|
||
|
|
|
||
|
|
// LoginResult holds the response from a Login call.
|
||
|
|
type LoginResult struct {
|
||
|
|
UserID string
|
||
|
|
Username string
|
||
|
|
RealName string
|
||
|
|
Avatar string
|
||
|
|
RoleKey string
|
||
|
|
RoleName string
|
||
|
|
}
|
||
|
|
|
||
|
|
// Login authenticates a user and returns identity info.
|
||
|
|
func (c *SystemClient) Login(ctx context.Context, username, password, ip string) (*LoginResult, error) {
|
||
|
|
resp, err := c.Raw.Login(ctx, &pb.LoginReq{
|
||
|
|
Username: username,
|
||
|
|
Password: password,
|
||
|
|
Ip: ip,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("system.Login: %w", err)
|
||
|
|
}
|
||
|
|
return &LoginResult{
|
||
|
|
UserID: resp.UserId,
|
||
|
|
Username: resp.Username,
|
||
|
|
RealName: resp.RealName,
|
||
|
|
Avatar: resp.Avatar,
|
||
|
|
RoleKey: resp.RoleKey,
|
||
|
|
RoleName: resp.RoleName,
|
||
|
|
}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetUser returns full user info by user ID.
|
||
|
|
func (c *SystemClient) GetUser(ctx context.Context, userID string) (*pb.UserInfo, error) {
|
||
|
|
resp, err := c.Raw.GetUser(ctx, &pb.GetUserReq{UserId: userID})
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("system.GetUser: %w", err)
|
||
|
|
}
|
||
|
|
return resp, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// CreateUser creates a new user and returns the user ID.
|
||
|
|
func (c *SystemClient) CreateUser(ctx context.Context, username, password, realName, phone, roleID string) (string, error) {
|
||
|
|
resp, err := c.Raw.CreateUser(ctx, &pb.CreateUserReq{
|
||
|
|
Username: username,
|
||
|
|
Password: password,
|
||
|
|
RealName: realName,
|
||
|
|
Phone: phone,
|
||
|
|
RoleId: roleID,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return "", fmt.Errorf("system.CreateUser: %w", err)
|
||
|
|
}
|
||
|
|
return resp.Id, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ListUser returns a paginated list of users with optional filters.
|
||
|
|
func (c *SystemClient) ListUser(ctx context.Context, page, pageSize int64, username string, status int64) ([]*pb.UserInfo, int64, error) {
|
||
|
|
resp, err := c.Raw.ListUser(ctx, &pb.ListUserReq{
|
||
|
|
Page: page,
|
||
|
|
PageSize: pageSize,
|
||
|
|
Username: username,
|
||
|
|
Status: status,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, fmt.Errorf("system.ListUser: %w", err)
|
||
|
|
}
|
||
|
|
return resp.List, resp.Total, nil
|
||
|
|
}
|