# usermodule **Repository Path**: sheeplight/usermodule ## Basic Information - **Project Name**: usermodule - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-04 - **Last Updated**: 2025-11-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # UserModule 一个基于 gRPC 的用户管理 Go SDK,提供完整的用户、组和开发者管理功能。 ## 功能特性 - 🔐 **用户管理** - 用户注册、登录、认证和密钥管理 - 👥 **组管理** - 创建和管理用户组,支持角色权限 - 👨‍💻 **开发者管理** - 开发者密钥管理和权限控制 - 🔑 **密钥管理** - 支持用户密钥和开发者密钥的完整生命周期管理 - 📊 **统计信息** - 提供详细的用户、组和系统统计 - 🌐 **gRPC 支持** - 基于 gRPC 的高性能通信 ## 安装 ```bash go get gitee.com/sheeplight/usermodule ``` ## 快速开始 ### 1. 创建客户端连接 ```go package main import ( "log" "time" "gitee.com/sheeplight/usermodule/lass" ) func main() { // 连接到用户管理服务 client, err := lass.NewLass("localhost:8080", 30*time.Second) if err != nil { log.Fatal("连接失败:", err) } defer client.Close() // 使用客户端... } ``` ### 2. 用户操作 ```go // 用户注册 userInfo, err := client.UserRegister("张三", "zhangsan@example.com", "password123") if err != nil { log.Fatal("注册失败:", err) } // 用户登录 user, err := client.UserLogin("zhangsan@example.com", "password123") if err != nil { log.Fatal("登录失败:", err) } // 创建用户密钥 keyInfo, keyValue, err := user.CreateKey("我的密钥", "用于API访问", time.Now().Add(365*24*time.Hour).Unix()) if err != nil { log.Fatal("创建密钥失败:", err) } fmt.Printf("密钥值: %s\n", keyValue) // 获取用户统计 stats, err := user.GetStats() if err != nil { log.Fatal("获取统计失败:", err) } fmt.Printf("总密钥数: %d\n", stats.TotalKeys) ``` ### 3. 开发者操作 ```go // 通过开发者密钥获取开发者实例 developer := client.Developer("your-developer-key") // 认证开发者 devInfo, err := developer.Authenticate() if err != nil { log.Fatal("开发者认证失败:", err) } fmt.Printf("开发者: %s (%s)\n", devInfo.Name, devInfo.Email) // 创建组 group, err := developer.CreateGroup("我的项目组", "项目开发组", true) if err != nil { log.Fatal("创建组失败:", err) } // 添加用户到组 err = group.AddUser(userInfo.ID, lass.UserRoleMember) if err != nil { log.Fatal("添加用户失败:", err) } // 获取组用户列表 users, pagination, err := group.GetUsers(1, 10) if err != nil { log.Fatal("获取组用户失败:", err) } fmt.Printf("组用户数: %d\n", len(users)) ``` ### 4. 组操作 ```go // 通过组密钥获取组实例 group, err := client.Group("your-group-key") if err != nil { log.Fatal("获取组失败:", err) } // 获取组信息 groupInfo, err := group.GetInfo() if err != nil { log.Fatal("获取组信息失败:", err) } fmt.Printf("组名: %s\n", groupInfo.Name) // 获取组统计 stats, err := group.GetStats() if err != nil { log.Fatal("获取组统计失败:", err) } fmt.Printf("组用户数: %d\n", stats.TotalUsers) ``` ## API 参考 ### Lass 主接口 ```go type Lass interface { // 开发者相关 Developer(key string) Developer // 用户相关 UserLogin(username, password string) (User, error) UserRegister(name, email, password string) (*UserInfo, error) User(token string) User // 组相关 Group(key string) (Group, error) // 连接管理 Close() error } ``` ### User 用户接口 ```go type User interface { // 认证 Authenticate() (*UserInfo, error) // 密钥管理 CreateKey(name, description string, expiresAt int64) (*KeyInfo, string, error) GetKeys(page, pageSize int32, includeInactive bool) ([]KeyInfo, *PaginationInfo, error) GetKey(keyID uint32) (*KeyInfo, error) UpdateKey(keyID uint32, name, description string, expiresAt int64) error DeleteKey(keyID uint32) error ToggleKey(keyID uint32, isActive bool) error // 统计 GetStats() (*Stats, error) } ``` ### Developer 开发者接口 ```go type Developer interface { // 认证 Authenticate() (*DeveloperInfo, error) // 组管理 ListGroups(page, pageSize int32) ([]GroupInfo, *PaginationInfo, error) GetGroup(groupID uint32) (Group, error) CreateGroup(name, description string, isPublic bool) (Group, error) UpdateGroup(groupID uint32, name, description string, isPublic bool) error DeleteGroup(groupID uint32) error // 用户管理 CreateUser(name, email, password string) (*UserInfo, error) GetUsers(page, pageSize int32) ([]UserInfo, *PaginationInfo, error) GetUser(userID uint32) (*UserInfo, error) UpdateUser(userID uint32, name, email string) error DeleteUser(userID uint32) error // 密钥管理 CreateKey(name, description string, expiresAt int64) (*KeyInfo, string, error) GetKeys(page, pageSize int32, includeInactive bool) ([]KeyInfo, *PaginationInfo, error) GetKey(keyID uint32) (*KeyInfo, error) UpdateKey(keyID uint32, name, description string, expiresAt int64) error DeleteKey(keyID uint32) error ToggleKey(keyID uint32, isActive bool) error // 统计 GetStats() (*Stats, error) } ``` ### Group 组接口 ```go type Group interface { // 基本信息 GetInfo() (*GroupInfo, error) UpdateInfo(name, description string, isPublic bool) error // 用户管理 GetUsers(page, pageSize int32) ([]GroupUserInfo, *PaginationInfo, error) AddUser(userID uint32, role UserRole) error RemoveUser(userID uint32) error UpdateUserRole(userID uint32, role string) error // 认证 AuthenticateUser(userToken string) (uint32, error) // 统计 GetStats() (*Stats, error) } ``` ## 数据结构 ### 用户信息 ```go type UserInfo struct { ID uint32 `json:"id"` Name string `json:"name"` Email string `json:"email"` Status string `json:"status"` GroupCount uint32 `json:"group_count"` CreatedAt int64 `json:"created_at"` LastLoginAt int64 `json:"last_login_at"` } ``` ### 组信息 ```go type GroupInfo struct { ID uint32 `json:"id"` Name string `json:"name"` Description string `json:"description"` IsPublic bool `json:"is_public"` Status string `json:"status"` UserCount uint32 `json:"user_count"` KeyCount uint32 `json:"key_count"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } ``` ### 密钥信息 ```go type KeyInfo struct { ID uint32 `json:"id"` Name string `json:"name"` Type string `json:"type"` IsActive bool `json:"is_active"` CreatedAt int64 `json:"created_at"` ExpiresAt int64 `json:"expires_at"` LastUsedAt int64 `json:"last_used_at"` UsageCount int64 `json:"usage_count"` Description string `json:"description"` } ``` ## 错误处理 SDK 提供了预定义的错误类型: ```go var ( ErrInvalidCredentials = errors.New("invalid credentials") ErrUnauthorized = errors.New("unauthorized access") ErrNotFound = errors.New("resource not found") ErrInvalidParameter = errors.New("invalid parameter") ErrInternalError = errors.New("internal server error") ) ``` ## 用户角色 支持的用户角色: ```go const ( UserRoleMember UserRole = "member" // 普通成员 UserRoleViewer UserRole = "viewer" // 只读成员 ) ``` ## 分页支持 所有列表接口都支持分页: ```go type PaginationInfo struct { Page int32 `json:"page"` PageSize int32 `json:"page_size"` Total int64 `json:"total"` TotalPages int32 `json:"total_pages"` HasNext bool `json:"has_next"` HasPrev bool `json:"has_prev"` } ``` ## 许可证 本项目采用 MIT 许可证。详见 [LICENSE](LICENSE) 文件。 ## 贡献 欢迎提交 Issue 和 Pull Request 来改进这个项目。 ## 联系方式 如有问题或建议,请通过以下方式联系: - 项目地址: [gitee.com/sheeplight/usermodule](https://gitee.com/sheeplight/usermodule) - 问题反馈: [Issues](https://gitee.com/sheeplight/usermodule/issues)