fix(api): 更新 YMT 用户列表处理逻辑以使用 gRPC 生成的代码
- 修改 YMTUsersAPI 的 list 方法,使用 resp.GetList() 替代 resp.List() 以获取用户列表 - 更新用户显示名称的获取方式,使用 user.GetRealname() 和 user.GetUsername() 方法 - 引入 userId 通过 user.GetId() 获取用户 ID,确保显示名称格式正确 - 更新 gRPC 用户客户端实现,替换临时结构体为生成的 proto 代码 - 移除不再需要的临时结构体定义,简化代码结构
This commit is contained in:
parent
dcfa1f2456
commit
bf5d927587
|
|
@ -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 (
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 关闭连接
|
||||
|
|
|
|||
Loading…
Reference in New Issue