From bf5d927587fcc50b40b7be50ea87ec2cd318ac72 Mon Sep 17 00:00:00 2001 From: zhouyonggao <1971162852@qq.com> Date: Sat, 20 Dec 2025 14:18:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20=E6=9B=B4=E6=96=B0=20YMT=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=88=97=E8=A1=A8=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BB=A5=E4=BD=BF=E7=94=A8=20gRPC=20=E7=94=9F?= =?UTF-8?q?=E6=88=90=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 YMTUsersAPI 的 list 方法,使用 resp.GetList() 替代 resp.List() 以获取用户列表 - 更新用户显示名称的获取方式,使用 user.GetRealname() 和 user.GetUsername() 方法 - 引入 userId 通过 user.GetId() 获取用户 ID,确保显示名称格式正确 - 更新 gRPC 用户客户端实现,替换临时结构体为生成的 proto 代码 - 移除不再需要的临时结构体定义,简化代码结构 --- server/go.mod | 1 + server/internal/api/ymt_users.go | 13 ++++---- server/internal/grpc/user_client.go | 48 ++++++++++------------------- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/server/go.mod b/server/go.mod index c1f8139..5961e57 100644 --- a/server/go.mod +++ b/server/go.mod @@ -8,6 +8,7 @@ require ( github.com/xuri/excelize/v2 v2.8.1 google.golang.org/grpc v1.60.1 gopkg.in/yaml.v3 v3.0.1 + grpc/user/userv1 v0.0.0 ) require ( diff --git a/server/internal/api/ymt_users.go b/server/internal/api/ymt_users.go index f23bc3b..f7475ff 100644 --- a/server/internal/api/ymt_users.go +++ b/server/internal/api/ymt_users.go @@ -70,7 +70,7 @@ func (a *YMTUsersAPI) list(w http.ResponseWriter, r *http.Request) { // 转换响应格式 out := []map[string]interface{}{} - for i, user := range resp.List { + for i, user := range resp.GetList() { if i >= limit { break } @@ -78,17 +78,18 @@ func (a *YMTUsersAPI) list(w http.ResponseWriter, r *http.Request) { continue } // 构建显示名称:realname(id)或 username(id) - displayName := user.Realname + displayName := user.GetRealname() if displayName == "" { - displayName = user.Username + displayName = user.GetUsername() } + userId := user.GetId() if displayName == "" { - displayName = strconv.FormatInt(int64(user.Id), 10) + displayName = strconv.FormatInt(int64(userId), 10) } - display := fmt.Sprintf("%s(%d)", displayName, user.Id) + display := fmt.Sprintf("%s(%d)", displayName, userId) out = append(out, map[string]interface{}{ - "id": user.Id, + "id": userId, "name": display, }) } diff --git a/server/internal/grpc/user_client.go b/server/internal/grpc/user_client.go index 9a40848..a5a97cd 100644 --- a/server/internal/grpc/user_client.go +++ b/server/internal/grpc/user_client.go @@ -10,6 +10,9 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/keepalive" + + // 使用生成的 proto 代码 + userv1 "grpc/user/userv1" ) var ( @@ -17,22 +20,10 @@ var ( userClient *UserClient ) -// AdminSimpleInfo 临时结构,等 proto 生成后会被替换 -type AdminSimpleInfo struct { - Id int32 - Username string - Realname string -} - -// SimpleListAllUserResp 临时结构,等 proto 生成后会被替换 -type SimpleListAllUserResp struct { - List []*AdminSimpleInfo -} - type UserClient struct { conn *grpc.ClientConn addr string - client interface{} // 临时使用 interface{},等 proto 生成后改为 userv1.UserClient + client userv1.UserClient } // NewUserClient 创建用户服务 gRPC 客户端 @@ -58,14 +49,13 @@ func NewUserClient(addr string) (*UserClient, error) { return } - // TODO: 等 proto 文件生成后,取消注释下面的代码 - // client := userv1.NewUserClient(conn) + client := userv1.NewUserClient(conn) userClient = &UserClient{ conn: conn, addr: addr, - client: nil, // 临时设为 nil,等 proto 生成后使用 userv1.NewUserClient(conn) + client: client, } - log.Printf("gRPC user client connected to %s (proto code not generated yet)", addr) + log.Printf("gRPC user client connected to %s", addr) }) if err != nil { @@ -76,21 +66,15 @@ func NewUserClient(addr string) (*UserClient, error) { } // SimpleListAllUser 调用 SimpleListAllUser gRPC 方法 -// 注意:此方法需要 proto 生成的代码才能正常工作 -// 请先运行: cd grpc && make generate -func (c *UserClient) SimpleListAllUser(ctx context.Context, keyword string) (*SimpleListAllUserResp, error) { - // TODO: 等 proto 文件生成后,取消注释下面的代码并删除临时实现 - // req := &userv1.SimpleListAllUserReq{ - // Keyword: keyword, - // } - // resp, err := c.client.SimpleListAllUser(ctx, req) - // if err != nil { - // return nil, fmt.Errorf("gRPC SimpleListAllUser failed: %w", err) - // } - // return resp, nil - - // 临时实现:返回错误提示需要生成 proto - return nil, fmt.Errorf("proto code not generated, please run 'cd grpc && make generate' first, then rebuild with -tags proto_generated") +func (c *UserClient) SimpleListAllUser(ctx context.Context, keyword string) (*userv1.SimpleListAllUserResp, error) { + req := &userv1.SimpleListAllUserReq{ + Keyword: keyword, + } + resp, err := c.client.SimpleListAllUser(ctx, req) + if err != nil { + return nil, fmt.Errorf("gRPC SimpleListAllUser failed: %w", err) + } + return resp, nil } // Close 关闭连接