From 5490a76650161fa02688d038f6ac27048c9cb188 Mon Sep 17 00:00:00 2001 From: xiabin Date: Wed, 12 Feb 2025 18:10:25 +0800 Subject: [PATCH] 1 --- app/auth_service/auth_service.proto | 2 + .../auth_service/auth_service.pb.go | 79 ++++++++++------- .../internal/logic/code2_session_logic.go | 8 ++ .../internal/gen/dao/model/user.gen.go | 1 + .../internal/gen/dao/query/user.gen.go | 6 +- .../internal/logic/find_by_id_logic.go | 38 +++++++++ .../internal/server/user_management_server.go | 6 ++ app/user_management/user_management.proto | 4 + .../user_management/user_management.pb.go | 85 +++++++++++-------- .../user_management_grpc.pb.go | 40 +++++++++ .../user_management_client/user_management.go | 8 ++ 11 files changed, 211 insertions(+), 66 deletions(-) create mode 100644 app/user_management/internal/logic/find_by_id_logic.go diff --git a/app/auth_service/auth_service.proto b/app/auth_service/auth_service.proto index 5625d72..095b2b3 100644 --- a/app/auth_service/auth_service.proto +++ b/app/auth_service/auth_service.proto @@ -21,6 +21,8 @@ message Code2SessionResponse{ string OpenId = 1; string UnionId = 2; uint64 UserId = 3; + uint32 AppId = 4; + bool IsNew = 5; } message GetAccessTokenRequest{ diff --git a/app/auth_service/auth_service/auth_service.pb.go b/app/auth_service/auth_service/auth_service.pb.go index 0aed50a..4432bea 100644 --- a/app/auth_service/auth_service/auth_service.pb.go +++ b/app/auth_service/auth_service/auth_service.pb.go @@ -174,6 +174,8 @@ type Code2SessionResponse struct { OpenId string `protobuf:"bytes,1,opt,name=OpenId,proto3" json:"OpenId,omitempty"` UnionId string `protobuf:"bytes,2,opt,name=UnionId,proto3" json:"UnionId,omitempty"` UserId uint64 `protobuf:"varint,3,opt,name=UserId,proto3" json:"UserId,omitempty"` + AppId uint32 `protobuf:"varint,4,opt,name=AppId,proto3" json:"AppId,omitempty"` + IsNew bool `protobuf:"varint,5,opt,name=IsNew,proto3" json:"IsNew,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -229,6 +231,20 @@ func (x *Code2SessionResponse) GetUserId() uint64 { return 0 } +func (x *Code2SessionResponse) GetAppId() uint32 { + if x != nil { + return x.AppId + } + return 0 +} + +func (x *Code2SessionResponse) GetIsNew() bool { + if x != nil { + return x.IsNew + } + return false +} + type GetAccessTokenRequest struct { state protoimpl.MessageState `protogen:"open.v1"` AppId string `protobuf:"bytes,1,opt,name=AppId,proto3" json:"AppId,omitempty"` @@ -332,37 +348,40 @@ var file_auth_service_proto_rawDesc = string([]byte{ 0x0a, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x41, 0x6e, 0x6f, 0x6e, 0x79, 0x6d, 0x6f, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x41, 0x6e, 0x6f, 0x6e, 0x79, - 0x6d, 0x6f, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x60, 0x0a, 0x14, 0x43, 0x6f, 0x64, 0x65, - 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x12, 0x16, 0x0a, 0x06, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x06, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x55, 0x6e, 0x69, 0x6f, - 0x6e, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x55, 0x6e, 0x69, 0x6f, 0x6e, - 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, - 0x28, 0x04, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x22, 0x2d, 0x0a, 0x15, 0x47, 0x65, - 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x41, 0x70, 0x70, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x05, 0x41, 0x70, 0x70, 0x49, 0x64, 0x22, 0x3a, 0x0a, 0x16, 0x47, 0x65, 0x74, - 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, - 0x6e, 0x73, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, - 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, - 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x32, 0xf9, 0x01, 0x0a, 0x0c, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, - 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x35, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x15, - 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, - 0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x55, 0x0a, - 0x0c, 0x43, 0x6f, 0x64, 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x21, 0x2e, + 0x6d, 0x6f, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x8c, 0x01, 0x0a, 0x14, 0x43, 0x6f, 0x64, + 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x16, 0x0a, 0x06, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x06, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x55, 0x6e, 0x69, + 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x55, 0x6e, 0x69, 0x6f, + 0x6e, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x04, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x41, + 0x70, 0x70, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x41, 0x70, 0x70, 0x49, + 0x64, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, + 0x52, 0x05, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x22, 0x2d, 0x0a, 0x15, 0x47, 0x65, 0x74, 0x41, 0x63, + 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x12, 0x14, 0x0a, 0x05, 0x41, 0x70, 0x70, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x05, 0x41, 0x70, 0x70, 0x49, 0x64, 0x22, 0x3a, 0x0a, 0x16, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, + 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x12, 0x20, 0x0a, 0x0b, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, + 0x65, 0x6e, 0x32, 0xf9, 0x01, 0x0a, 0x0c, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, + 0x69, 0x63, 0x65, 0x12, 0x35, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x15, 0x2e, 0x61, 0x75, + 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x55, 0x0a, 0x0c, 0x43, 0x6f, + 0x64, 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x21, 0x2e, 0x61, 0x75, 0x74, + 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x43, 0x6f, 0x64, 0x65, 0x32, 0x53, + 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x22, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x43, 0x6f, 0x64, - 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x22, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, - 0x43, 0x6f, 0x64, 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x5b, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, - 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x23, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, - 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, - 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x24, 0x2e, 0x61, 0x75, - 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x63, - 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x42, 0x10, 0x5a, 0x0e, 0x2e, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x5b, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, + 0x6b, 0x65, 0x6e, 0x12, 0x23, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, + 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x24, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, + 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, + 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x10, + 0x5a, 0x0e, 0x2e, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, }) var ( diff --git a/app/auth_service/internal/logic/code2_session_logic.go b/app/auth_service/internal/logic/code2_session_logic.go index ab0037d..ddbdf4a 100644 --- a/app/auth_service/internal/logic/code2_session_logic.go +++ b/app/auth_service/internal/logic/code2_session_logic.go @@ -48,6 +48,7 @@ func (l *Code2SessionLogic) Code2Session(in *auth_service.Code2SessionRequest) ( return nil, err } + var isNew bool //如果没有用户id,则创建一个空用户 if auModel.UserID == nil { userId, err := l.svcCtx.UserManagerClient.CreateEmptyUser(l.ctx, &user_management.Empty{}) @@ -55,11 +56,18 @@ func (l *Code2SessionLogic) Code2Session(in *auth_service.Code2SessionRequest) ( return nil, err } auModel.UserID = &userId.UserId + } else { + user, err := l.svcCtx.UserManagerClient.FindById(l.ctx, &user_management.UserId{UserId: *auModel.UserID}) + if err != nil { + return nil, err + } + isNew = user.IsNew } return &auth_service.Code2SessionResponse{ OpenId: res.OpenID, UnionId: res.UnionID, UserId: *auModel.UserID, + IsNew: isNew, }, nil } diff --git a/app/user_management/internal/gen/dao/model/user.gen.go b/app/user_management/internal/gen/dao/model/user.gen.go index da4ba6f..166c685 100644 --- a/app/user_management/internal/gen/dao/model/user.gen.go +++ b/app/user_management/internal/gen/dao/model/user.gen.go @@ -13,6 +13,7 @@ const TableNameUser = "user" // User mapped from table type User struct { ID uint64 `gorm:"column:id;type:bigint unsigned;primaryKey;autoIncrement:true;index:idx_app_user_id,priority:1" json:"id"` + IsNew uint32 `gorm:"column:is_new;type:tinyint unsigned;not null" json:"is_new"` Nickname string `gorm:"column:nickname;type:varchar(255);not null;comment:昵称" json:"nickname"` // 昵称 Avatar string `gorm:"column:avatar;type:varchar(255);not null;comment:头像" json:"avatar"` // 头像 CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间 diff --git a/app/user_management/internal/gen/dao/query/user.gen.go b/app/user_management/internal/gen/dao/query/user.gen.go index fb316ef..4de48e2 100644 --- a/app/user_management/internal/gen/dao/query/user.gen.go +++ b/app/user_management/internal/gen/dao/query/user.gen.go @@ -28,6 +28,7 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user { tableName := _user.userDo.TableName() _user.ALL = field.NewAsterisk(tableName) _user.ID = field.NewUint64(tableName, "id") + _user.IsNew = field.NewUint32(tableName, "is_new") _user.Nickname = field.NewString(tableName, "nickname") _user.Avatar = field.NewString(tableName, "avatar") _user.CreatedAt = field.NewTime(tableName, "created_at") @@ -43,6 +44,7 @@ type user struct { ALL field.Asterisk ID field.Uint64 + IsNew field.Uint32 Nickname field.String // 昵称 Avatar field.String // 头像 CreatedAt field.Time // 创建时间 @@ -64,6 +66,7 @@ func (u user) As(alias string) *user { func (u *user) updateTableName(table string) *user { u.ALL = field.NewAsterisk(table) u.ID = field.NewUint64(table, "id") + u.IsNew = field.NewUint32(table, "is_new") u.Nickname = field.NewString(table, "nickname") u.Avatar = field.NewString(table, "avatar") u.CreatedAt = field.NewTime(table, "created_at") @@ -92,8 +95,9 @@ func (u *user) GetFieldByName(fieldName string) (field.OrderExpr, bool) { } func (u *user) fillFieldMap() { - u.fieldMap = make(map[string]field.Expr, 5) + u.fieldMap = make(map[string]field.Expr, 6) u.fieldMap["id"] = u.ID + u.fieldMap["is_new"] = u.IsNew u.fieldMap["nickname"] = u.Nickname u.fieldMap["avatar"] = u.Avatar u.fieldMap["created_at"] = u.CreatedAt diff --git a/app/user_management/internal/logic/find_by_id_logic.go b/app/user_management/internal/logic/find_by_id_logic.go new file mode 100644 index 0000000..239d110 --- /dev/null +++ b/app/user_management/internal/logic/find_by_id_logic.go @@ -0,0 +1,38 @@ +package logic + +import ( + "context" + + "gitea.youtukeji.com.cn/youtu/youtu_grpc/app/user_management/internal/svc" + "gitea.youtukeji.com.cn/youtu/youtu_grpc/app/user_management/user_management" + + "github.com/zeromicro/go-zero/core/logx" +) + +type FindByIdLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewFindByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindByIdLogic { + return &FindByIdLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// FindById 创建用户或者获取用户 +func (l *FindByIdLogic) FindById(in *user_management.UserId) (*user_management.User, error) { + userModel, err := l.svcCtx.Query.User.WithContext(l.ctx).Where(l.svcCtx.Query.User.ID.Eq(in.GetUserId())).FirstOrCreate() + if err != nil { + return nil, err + } + return &user_management.User{ + ID: userModel.ID, + Nickname: userModel.Nickname, + Avatar: userModel.Avatar, + IsNew: userModel.IsNew == 1, + }, nil +} diff --git a/app/user_management/internal/server/user_management_server.go b/app/user_management/internal/server/user_management_server.go index c47f67a..a913ba1 100644 --- a/app/user_management/internal/server/user_management_server.go +++ b/app/user_management/internal/server/user_management_server.go @@ -46,6 +46,12 @@ func (s *UserManagementServer) CreateEmptyUser(ctx context.Context, in *user_man return l.CreateEmptyUser(in) } +// FirstOrCreateUser 创建用户或者获取用户 +func (s *UserManagementServer) FindById(ctx context.Context, in *user_management.UserId) (*user_management.User, error) { + l := logic.NewFindByIdLogic(ctx, s.svcCtx) + return l.FindById(in) +} + // GetUserById 通过 ID 获取用户 func (s *UserManagementServer) GetUserById(ctx context.Context, in *user_management.UserId) (*user_management.User, error) { l := logic.NewGetUserByIdLogic(ctx, s.svcCtx) diff --git a/app/user_management/user_management.proto b/app/user_management/user_management.proto index 5cf4309..e9b9165 100644 --- a/app/user_management/user_management.proto +++ b/app/user_management/user_management.proto @@ -36,6 +36,7 @@ message User { uint64 ID = 1; string Nickname = 2; string Avatar = 3; + bool IsNew = 4; } service user_management { @@ -50,6 +51,9 @@ service user_management { //CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 rpc CreateEmptyUser(Empty) returns(UserId); + //FirstOrCreateUser 创建用户或者获取用户 + rpc FindById(UserId) returns(User); + //GetUserById 通过 ID 获取用户 rpc GetUserById(UserId) returns(User); } diff --git a/app/user_management/user_management/user_management.pb.go b/app/user_management/user_management/user_management.pb.go index ba7c405..6ecdd7e 100644 --- a/app/user_management/user_management/user_management.pb.go +++ b/app/user_management/user_management/user_management.pb.go @@ -350,6 +350,7 @@ type User struct { ID uint64 `protobuf:"varint,1,opt,name=ID,proto3" json:"ID,omitempty"` Nickname string `protobuf:"bytes,2,opt,name=Nickname,proto3" json:"Nickname,omitempty"` Avatar string `protobuf:"bytes,3,opt,name=Avatar,proto3" json:"Avatar,omitempty"` + IsNew bool `protobuf:"varint,4,opt,name=IsNew,proto3" json:"IsNew,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -405,6 +406,13 @@ func (x *User) GetAvatar() string { return "" } +func (x *User) GetIsNew() bool { + if x != nil { + return x.IsNew + } + return false +} + var File_user_management_proto protoreflect.FileDescriptor var file_user_management_proto_rawDesc = string([]byte{ @@ -431,36 +439,41 @@ var file_user_management_proto_rawDesc = string([]byte{ 0x76, 0x61, 0x74, 0x61, 0x72, 0x22, 0x20, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x22, 0x07, 0x0a, 0x05, 0x45, 0x6d, 0x70, 0x74, 0x79, - 0x22, 0x4a, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x44, 0x18, 0x01, + 0x22, 0x60, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x02, 0x49, 0x44, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x76, 0x61, 0x74, 0x61, 0x72, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x41, 0x76, 0x61, 0x74, 0x61, 0x72, 0x32, 0xea, 0x02, 0x0a, - 0x0f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, - 0x12, 0x3b, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x18, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, - 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, - 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, - 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x4c, 0x0a, - 0x07, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1f, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, - 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, - 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x20, 0x2e, 0x75, 0x73, 0x65, 0x72, - 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x53, 0x65, 0x74, 0x55, - 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x49, 0x0a, 0x0a, 0x43, - 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x12, 0x22, 0x2e, 0x75, 0x73, 0x65, 0x72, - 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x43, 0x72, 0x65, 0x61, - 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, - 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, - 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x42, 0x0a, 0x0f, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, - 0x45, 0x6d, 0x70, 0x74, 0x79, 0x55, 0x73, 0x65, 0x72, 0x12, 0x16, 0x2e, 0x75, 0x73, 0x65, 0x72, - 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x45, 0x6d, 0x70, 0x74, - 0x79, 0x1a, 0x17, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, - 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x3d, 0x0a, 0x0b, 0x47, 0x65, - 0x74, 0x55, 0x73, 0x65, 0x72, 0x42, 0x79, 0x49, 0x64, 0x12, 0x17, 0x2e, 0x75, 0x73, 0x65, 0x72, - 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, - 0x49, 0x64, 0x1a, 0x15, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, - 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x42, 0x13, 0x5a, 0x11, 0x2e, 0x2f, 0x75, - 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x62, 0x06, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x41, 0x76, 0x61, 0x74, 0x61, 0x72, 0x12, 0x14, 0x0a, 0x05, + 0x49, 0x73, 0x4e, 0x65, 0x77, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x49, 0x73, 0x4e, + 0x65, 0x77, 0x32, 0xa6, 0x03, 0x0a, 0x0f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, + 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x3b, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x18, + 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, + 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, + 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x4c, 0x0a, 0x07, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1f, + 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, + 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x20, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, + 0x74, 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x49, 0x0a, 0x0a, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x12, + 0x22, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, + 0x74, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, + 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x42, 0x0a, 0x0f, + 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x55, 0x73, 0x65, 0x72, 0x12, + 0x16, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, + 0x74, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x17, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, + 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, + 0x12, 0x3a, 0x0a, 0x08, 0x46, 0x69, 0x6e, 0x64, 0x42, 0x79, 0x49, 0x64, 0x12, 0x17, 0x2e, 0x75, + 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, + 0x73, 0x65, 0x72, 0x49, 0x64, 0x1a, 0x15, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, + 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x12, 0x3d, 0x0a, 0x0b, + 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x42, 0x79, 0x49, 0x64, 0x12, 0x17, 0x2e, 0x75, 0x73, + 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, + 0x65, 0x72, 0x49, 0x64, 0x1a, 0x15, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, + 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x42, 0x13, 0x5a, 0x11, 0x2e, + 0x2f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, }) var ( @@ -491,14 +504,16 @@ var file_user_management_proto_depIdxs = []int32{ 2, // 1: user_management.user_management.SetUser:input_type -> user_management.SetUserRequest 4, // 2: user_management.user_management.CreateUser:input_type -> user_management.CreateUserRequest 6, // 3: user_management.user_management.CreateEmptyUser:input_type -> user_management.Empty - 5, // 4: user_management.user_management.GetUserById:input_type -> user_management.UserId - 1, // 5: user_management.user_management.Ping:output_type -> user_management.Response - 3, // 6: user_management.user_management.SetUser:output_type -> user_management.SetUserResponse - 5, // 7: user_management.user_management.CreateUser:output_type -> user_management.UserId - 5, // 8: user_management.user_management.CreateEmptyUser:output_type -> user_management.UserId - 7, // 9: user_management.user_management.GetUserById:output_type -> user_management.User - 5, // [5:10] is the sub-list for method output_type - 0, // [0:5] is the sub-list for method input_type + 5, // 4: user_management.user_management.FindById:input_type -> user_management.UserId + 5, // 5: user_management.user_management.GetUserById:input_type -> user_management.UserId + 1, // 6: user_management.user_management.Ping:output_type -> user_management.Response + 3, // 7: user_management.user_management.SetUser:output_type -> user_management.SetUserResponse + 5, // 8: user_management.user_management.CreateUser:output_type -> user_management.UserId + 5, // 9: user_management.user_management.CreateEmptyUser:output_type -> user_management.UserId + 7, // 10: user_management.user_management.FindById:output_type -> user_management.User + 7, // 11: user_management.user_management.GetUserById:output_type -> user_management.User + 6, // [6:12] is the sub-list for method output_type + 0, // [0:6] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name diff --git a/app/user_management/user_management/user_management_grpc.pb.go b/app/user_management/user_management/user_management_grpc.pb.go index 72626c4..1fc2ed4 100644 --- a/app/user_management/user_management/user_management_grpc.pb.go +++ b/app/user_management/user_management/user_management_grpc.pb.go @@ -23,6 +23,7 @@ const ( UserManagement_SetUser_FullMethodName = "/user_management.user_management/SetUser" UserManagement_CreateUser_FullMethodName = "/user_management.user_management/CreateUser" UserManagement_CreateEmptyUser_FullMethodName = "/user_management.user_management/CreateEmptyUser" + UserManagement_FindById_FullMethodName = "/user_management.user_management/FindById" UserManagement_GetUserById_FullMethodName = "/user_management.user_management/GetUserById" ) @@ -37,6 +38,8 @@ type UserManagementClient interface { CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*UserId, error) // CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 CreateEmptyUser(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*UserId, error) + // FirstOrCreateUser 创建用户或者获取用户 + FindById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) // GetUserById 通过 ID 获取用户 GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) } @@ -89,6 +92,16 @@ func (c *userManagementClient) CreateEmptyUser(ctx context.Context, in *Empty, o return out, nil } +func (c *userManagementClient) FindById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(User) + err := c.cc.Invoke(ctx, UserManagement_FindById_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *userManagementClient) GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) { cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) out := new(User) @@ -110,6 +123,8 @@ type UserManagementServer interface { CreateUser(context.Context, *CreateUserRequest) (*UserId, error) // CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 CreateEmptyUser(context.Context, *Empty) (*UserId, error) + // FirstOrCreateUser 创建用户或者获取用户 + FindById(context.Context, *UserId) (*User, error) // GetUserById 通过 ID 获取用户 GetUserById(context.Context, *UserId) (*User, error) mustEmbedUnimplementedUserManagementServer() @@ -134,6 +149,9 @@ func (UnimplementedUserManagementServer) CreateUser(context.Context, *CreateUser func (UnimplementedUserManagementServer) CreateEmptyUser(context.Context, *Empty) (*UserId, error) { return nil, status.Errorf(codes.Unimplemented, "method CreateEmptyUser not implemented") } +func (UnimplementedUserManagementServer) FindById(context.Context, *UserId) (*User, error) { + return nil, status.Errorf(codes.Unimplemented, "method FindById not implemented") +} func (UnimplementedUserManagementServer) GetUserById(context.Context, *UserId) (*User, error) { return nil, status.Errorf(codes.Unimplemented, "method GetUserById not implemented") } @@ -230,6 +248,24 @@ func _UserManagement_CreateEmptyUser_Handler(srv interface{}, ctx context.Contex return interceptor(ctx, in, info, handler) } +func _UserManagement_FindById_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(UserId) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(UserManagementServer).FindById(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: UserManagement_FindById_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(UserManagementServer).FindById(ctx, req.(*UserId)) + } + return interceptor(ctx, in, info, handler) +} + func _UserManagement_GetUserById_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(UserId) if err := dec(in); err != nil { @@ -271,6 +307,10 @@ var UserManagement_ServiceDesc = grpc.ServiceDesc{ MethodName: "CreateEmptyUser", Handler: _UserManagement_CreateEmptyUser_Handler, }, + { + MethodName: "FindById", + Handler: _UserManagement_FindById_Handler, + }, { MethodName: "GetUserById", Handler: _UserManagement_GetUserById_Handler, diff --git a/app/user_management/user_management_client/user_management.go b/app/user_management/user_management_client/user_management.go index 77d2869..aed8542 100644 --- a/app/user_management/user_management_client/user_management.go +++ b/app/user_management/user_management_client/user_management.go @@ -31,6 +31,8 @@ type ( CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*UserId, error) // CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 CreateEmptyUser(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*UserId, error) + // FirstOrCreateUser 创建用户或者获取用户 + FindById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) // GetUserById 通过 ID 获取用户 GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) } @@ -69,6 +71,12 @@ func (m *defaultUserManagement) CreateEmptyUser(ctx context.Context, in *Empty, return client.CreateEmptyUser(ctx, in, opts...) } +// FirstOrCreateUser 创建用户或者获取用户 +func (m *defaultUserManagement) FindById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) { + client := user_management.NewUserManagementClient(m.cli.Conn()) + return client.FindById(ctx, in, opts...) +} + // GetUserById 通过 ID 获取用户 func (m *defaultUserManagement) GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) { client := user_management.NewUserManagementClient(m.cli.Conn())