iloom/shared/pkg/grpc/system_client.go
Chever John 9c39c8cbd7
init: iloom WMS monorepo with K8s deployment manifests
Go workspace (go.work) with 5 microservices + shared library:
- gateway (8080), auth-service (8081), purchaser-service (8082)
- textile-service (8083), washing-service (8084)
- shared: proto definitions, common packages

Infrastructure: docker-compose for local dev, K8s manifests for
K3s cluster deployment (mysql/redis/etcd + traefik ingress).

Frontend (iloom-flatten) added as git submodule.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-14 11:33:31 +08:00

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
}