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
|
github.com/xuri/excelize/v2 v2.8.1
|
||||||
google.golang.org/grpc v1.60.1
|
google.golang.org/grpc v1.60.1
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
grpc/user/userv1 v0.0.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ func (a *YMTUsersAPI) list(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// 转换响应格式
|
// 转换响应格式
|
||||||
out := []map[string]interface{}{}
|
out := []map[string]interface{}{}
|
||||||
for i, user := range resp.List {
|
for i, user := range resp.GetList() {
|
||||||
if i >= limit {
|
if i >= limit {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -78,17 +78,18 @@ func (a *YMTUsersAPI) list(w http.ResponseWriter, r *http.Request) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// 构建显示名称:realname(id)或 username(id)
|
// 构建显示名称:realname(id)或 username(id)
|
||||||
displayName := user.Realname
|
displayName := user.GetRealname()
|
||||||
if displayName == "" {
|
if displayName == "" {
|
||||||
displayName = user.Username
|
displayName = user.GetUsername()
|
||||||
}
|
}
|
||||||
|
userId := user.GetId()
|
||||||
if displayName == "" {
|
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{}{
|
out = append(out, map[string]interface{}{
|
||||||
"id": user.Id,
|
"id": userId,
|
||||||
"name": display,
|
"name": display,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,9 @@ import (
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
"google.golang.org/grpc/keepalive"
|
"google.golang.org/grpc/keepalive"
|
||||||
|
|
||||||
|
// 使用生成的 proto 代码
|
||||||
|
userv1 "grpc/user/userv1"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -17,22 +20,10 @@ var (
|
||||||
userClient *UserClient
|
userClient *UserClient
|
||||||
)
|
)
|
||||||
|
|
||||||
// AdminSimpleInfo 临时结构,等 proto 生成后会被替换
|
|
||||||
type AdminSimpleInfo struct {
|
|
||||||
Id int32
|
|
||||||
Username string
|
|
||||||
Realname string
|
|
||||||
}
|
|
||||||
|
|
||||||
// SimpleListAllUserResp 临时结构,等 proto 生成后会被替换
|
|
||||||
type SimpleListAllUserResp struct {
|
|
||||||
List []*AdminSimpleInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserClient struct {
|
type UserClient struct {
|
||||||
conn *grpc.ClientConn
|
conn *grpc.ClientConn
|
||||||
addr string
|
addr string
|
||||||
client interface{} // 临时使用 interface{},等 proto 生成后改为 userv1.UserClient
|
client userv1.UserClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUserClient 创建用户服务 gRPC 客户端
|
// NewUserClient 创建用户服务 gRPC 客户端
|
||||||
|
|
@ -58,14 +49,13 @@ func NewUserClient(addr string) (*UserClient, error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: 等 proto 文件生成后,取消注释下面的代码
|
client := userv1.NewUserClient(conn)
|
||||||
// client := userv1.NewUserClient(conn)
|
|
||||||
userClient = &UserClient{
|
userClient = &UserClient{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
addr: addr,
|
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 {
|
if err != nil {
|
||||||
|
|
@ -76,21 +66,15 @@ func NewUserClient(addr string) (*UserClient, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SimpleListAllUser 调用 SimpleListAllUser gRPC 方法
|
// SimpleListAllUser 调用 SimpleListAllUser gRPC 方法
|
||||||
// 注意:此方法需要 proto 生成的代码才能正常工作
|
func (c *UserClient) SimpleListAllUser(ctx context.Context, keyword string) (*userv1.SimpleListAllUserResp, error) {
|
||||||
// 请先运行: cd grpc && make generate
|
req := &userv1.SimpleListAllUserReq{
|
||||||
func (c *UserClient) SimpleListAllUser(ctx context.Context, keyword string) (*SimpleListAllUserResp, error) {
|
Keyword: keyword,
|
||||||
// TODO: 等 proto 文件生成后,取消注释下面的代码并删除临时实现
|
}
|
||||||
// req := &userv1.SimpleListAllUserReq{
|
resp, err := c.client.SimpleListAllUser(ctx, req)
|
||||||
// Keyword: keyword,
|
if err != nil {
|
||||||
// }
|
return nil, fmt.Errorf("gRPC SimpleListAllUser failed: %w", err)
|
||||||
// resp, err := c.client.SimpleListAllUser(ctx, req)
|
}
|
||||||
// if err != nil {
|
return resp, 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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close 关闭连接
|
// Close 关闭连接
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue