From 7ada2ce4a4351c1043dc195072c1b10966a3370c Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Tue, 7 Jun 2022 09:59:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0uid=20=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/robot/user.go | 4 +-- comm/core.go | 15 ++-------- comm/usersession.go | 8 ++--- modules/gateway/agent.go | 10 +++---- modules/gateway/core.go | 4 +-- modules/pack/api_comp.go | 16 +++++----- modules/user/login_comp.go | 2 +- pb/comm.pb.go | 16 +++++----- pb/pack_db.pb.go | 61 ++++++++++++++++---------------------- pb/proto/comm.proto | 4 +-- pb/proto/pack_db.proto | 5 ++-- pb/proto/user_db.proto | 2 +- pb/user_db.pb.go | 8 ++--- sys/cache/pack.go | 6 ++-- sys/cache/user.go | 2 +- sys/cache/user_test.go | 3 +- sys/db/db.go | 1 - sys/db/pack.go | 4 +-- sys/db/user.go | 61 +++++--------------------------------- sys/db/user_test.go | 5 ++-- 20 files changed, 85 insertions(+), 152 deletions(-) diff --git a/cmd/robot/user.go b/cmd/robot/user.go index ced98e66f..cc4dd1b75 100644 --- a/cmd/robot/user.go +++ b/cmd/robot/user.go @@ -23,7 +23,7 @@ func (r *Robot) handleUserMsg(msg *pb.UserMessage) { log.Fatal("load user err") } - if rsp.Data.UserData.UserId != 0 { + if rsp.Data.UserData.UserId != "" { r.onUserLoaded() } else { log.Debugf("%s不存在", r.Opts.Account) @@ -35,5 +35,5 @@ func (r *Robot) handleUserMsg(msg *pb.UserMessage) { } func (r *Robot) CreateUser() { - + } diff --git a/comm/core.go b/comm/core.go index cf2eab281..88a10e89e 100644 --- a/comm/core.go +++ b/comm/core.go @@ -38,27 +38,16 @@ type ISC_GateRouteComp interface { //用户会话 type IUserSession interface { GetSessionId() string - GetUserId() uint32 + GetUserId() string GetIP() string GetGatewayServiceId() string - Build(uid uint32) (err error) + Build(uid string) (err error) UnBuild(ServiceMethod string, msg proto.Message) (err error) SendMsg(mainType, subType string, code pb.ErrorCode, msg proto.Message) (err error) Close() (err error) ToString() string } -//消息体 -type MessageHead struct { - ServiceMethod string //服务名 -} - -//处理JSON消息 -type Message struct { - Head *MessageHead - Data []byte -} - func ProtoDecode(msg *pb.UserMessage, req proto.Message) (ok bool) { err := proto.Unmarshal(msg.Data, req) if err != nil { diff --git a/comm/usersession.go b/comm/usersession.go index 7f27e04c7..44a961e51 100644 --- a/comm/usersession.go +++ b/comm/usersession.go @@ -10,7 +10,7 @@ import ( "google.golang.org/protobuf/proto" ) -func NewUserSession(service base.IRPCXService, ip, sessionId, gatewayServiceId string, uid uint32) IUserSession { +func NewUserSession(service base.IRPCXService, ip, sessionId, gatewayServiceId string, uid string) IUserSession { return &UserSession{ IP: ip, SessionId: sessionId, @@ -24,7 +24,7 @@ type UserSession struct { IP string SessionId string GatewayServiceId string //用户所在网关服务 - UserId uint32 + UserId string service base.IRPCXService } @@ -32,7 +32,7 @@ func (this *UserSession) GetSessionId() string { return this.SessionId } -func (this *UserSession) GetUserId() uint32 { +func (this *UserSession) GetUserId() string { return this.UserId } func (this *UserSession) GetIP() string { @@ -42,7 +42,7 @@ func (this *UserSession) GetGatewayServiceId() string { return this.GatewayServiceId } -func (this *UserSession) Build(uid uint32) (err error) { +func (this *UserSession) Build(uid string) (err error) { reply := &pb.RPCMessageReply{} if err := this.service.RpcCallById(this.GatewayServiceId, string(Rpc_GatewayAgentBuild), context.Background(), &pb.AgentBuildReq{ UserSessionId: this.SessionId, diff --git a/modules/gateway/agent.go b/modules/gateway/agent.go index bc79f4c86..ac7ca93f2 100644 --- a/modules/gateway/agent.go +++ b/modules/gateway/agent.go @@ -19,7 +19,7 @@ func newAgent(gateway IGateway, conn *websocket.Conn) *Agent { gateway: gateway, wsConn: conn, sessionId: id.NewUUId(), - uId: 0, + uId: "", writeChan: make(chan *pb.UserMessage, 2), closeSignal: make(chan bool), state: 1, @@ -35,7 +35,7 @@ type Agent struct { gateway IGateway wsConn *websocket.Conn sessionId string - uId uint32 + uId string writeChan chan *pb.UserMessage closeSignal chan bool state int32 //状态 0 关闭 1 运行 2 关闭中 @@ -100,16 +100,16 @@ func (this *Agent) SessionId() string { func (this *Agent) IP() string { return this.wsConn.RemoteAddr().String() } -func (this *Agent) UserId() uint32 { +func (this *Agent) UserId() string { return this.uId } -func (this *Agent) Build(uId uint32) { +func (this *Agent) Build(uId string) { this.uId = uId } func (this *Agent) UnBuild() { - this.uId = 0 + this.uId = "" } func (this *Agent) WriteMsg(msg *pb.UserMessage) (err error) { diff --git a/modules/gateway/core.go b/modules/gateway/core.go index 566c19d29..c0313200c 100644 --- a/modules/gateway/core.go +++ b/modules/gateway/core.go @@ -11,8 +11,8 @@ type ( IAgent interface { SessionId() string IP() string - UserId() uint32 - Build(uId uint32) + UserId() string + Build(uId string) UnBuild() WriteMsg(msg *pb.UserMessage) (err error) Close() //主动关闭接口 diff --git a/modules/pack/api_comp.go b/modules/pack/api_comp.go index d1de91c90..434bf8518 100644 --- a/modules/pack/api_comp.go +++ b/modules/pack/api_comp.go @@ -41,7 +41,7 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe defer func() { session.SendMsg(string(this.module.GetType()), QueryUserPackResp, code, &pb.QueryUserPackResp{Items: items}) }() - if session.GetUserId() == 0 { + if session.GetUserId() == "" { code = pb.ErrorCode_NoLogin return } @@ -51,11 +51,11 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe return } else { items = make([]*pb.ItemAmount, 0, len(pack.Pack)) - for _, v := range pack.Pack { - if v.Itype == req.IType { - items = append(items, &pb.ItemAmount{IsNew: v.IsNew, ItemId: v.ItemId, Amount: v.Amount}) - } - } + // for _, v := range pack.Pack { + // if v.Itype == req.IType { + // items = append(items, &pb.ItemAmount{IsNew: v.IsNew, ItemId: v.ItemId, Amount: v.Amount}) + // } + // } } return } @@ -68,7 +68,7 @@ func (this *Api_Comp) UseItemReq(ctx context.Context, session comm.IUserSession, defer func() { session.SendMsg(string(this.module.GetType()), UseItemResp, code, &pb.UseItemResp{}) }() - if session.GetUserId() == 0 { + if session.GetUserId() == "" { code = pb.ErrorCode_NoLogin return } @@ -84,7 +84,7 @@ func (this *Api_Comp) SellItemReq(ctx context.Context, session comm.IUserSession defer func() { session.SendMsg(string(this.module.GetType()), SellItemResp, code, &pb.SellItemResp{}) }() - if session.GetUserId() == 0 { + if session.GetUserId() == "" { code = pb.ErrorCode_NoLogin return } diff --git a/modules/user/login_comp.go b/modules/user/login_comp.go index 7cad5c595..6257bf40d 100644 --- a/modules/user/login_comp.go +++ b/modules/user/login_comp.go @@ -27,7 +27,7 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req } } - if db_user.UserId == 0 { + if db_user.UserId == "" { db_user.Account = req.Name err = db.Defsys.User_CreateUser(db_user) if err != nil { diff --git a/pb/comm.pb.go b/pb/comm.pb.go index d6ace1697..d1ce2baff 100644 --- a/pb/comm.pb.go +++ b/pb/comm.pb.go @@ -100,7 +100,7 @@ type AgentMessage struct { Ip string `protobuf:"bytes,1,opt,name=Ip,proto3" json:"Ip,omitempty"` UserSessionId string `protobuf:"bytes,2,opt,name=UserSessionId,proto3" json:"UserSessionId,omitempty"` - UserId uint32 `protobuf:"varint,3,opt,name=UserId,proto3" json:"UserId,omitempty"` + UserId string `protobuf:"bytes,3,opt,name=UserId,proto3" json:"UserId,omitempty"` GatewayServiceId string `protobuf:"bytes,4,opt,name=GatewayServiceId,proto3" json:"GatewayServiceId,omitempty"` Method string `protobuf:"bytes,5,opt,name=Method,proto3" json:"Method,omitempty"` Message []byte `protobuf:"bytes,6,opt,name=Message,proto3" json:"Message,omitempty"` @@ -152,11 +152,11 @@ func (x *AgentMessage) GetUserSessionId() string { return "" } -func (x *AgentMessage) GetUserId() uint32 { +func (x *AgentMessage) GetUserId() string { if x != nil { return x.UserId } - return 0 + return "" } func (x *AgentMessage) GetGatewayServiceId() string { @@ -243,7 +243,7 @@ type AgentBuildReq struct { unknownFields protoimpl.UnknownFields UserSessionId string `protobuf:"bytes,1,opt,name=UserSessionId,proto3" json:"UserSessionId,omitempty"` - UserId uint32 `protobuf:"varint,2,opt,name=UserId,proto3" json:"UserId,omitempty"` + UserId string `protobuf:"bytes,2,opt,name=UserId,proto3" json:"UserId,omitempty"` } func (x *AgentBuildReq) Reset() { @@ -285,11 +285,11 @@ func (x *AgentBuildReq) GetUserSessionId() string { return "" } -func (x *AgentBuildReq) GetUserId() uint32 { +func (x *AgentBuildReq) GetUserId() string { if x != nil { return x.UserId } - return 0 + return "" } //用户代理解绑请求 @@ -621,7 +621,7 @@ var file_comm_proto_rawDesc = []byte{ 0x01, 0x28, 0x09, 0x52, 0x02, 0x49, 0x70, 0x12, 0x24, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x16, 0x0a, - 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x55, + 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x2a, 0x0a, 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, @@ -636,7 +636,7 @@ var file_comm_proto_rawDesc = []byte{ 0x74, 0x42, 0x75, 0x69, 0x6c, 0x64, 0x52, 0x65, 0x71, 0x12, 0x24, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, - 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, + 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x22, 0x37, 0x0a, 0x0f, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x55, 0x6e, 0x42, 0x75, 0x69, 0x6c, 0x64, 0x52, 0x65, 0x71, 0x12, 0x24, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, diff --git a/pb/pack_db.pb.go b/pb/pack_db.pb.go index b9aa2f991..6dd9577bf 100644 --- a/pb/pack_db.pb.go +++ b/pb/pack_db.pb.go @@ -75,10 +75,9 @@ type GridData struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - IsNew bool `protobuf:"varint,1,opt,name=IsNew,proto3" json:"IsNew,omitempty"` //是否是新的 - ItemId uint32 `protobuf:"varint,2,opt,name=ItemId,proto3" json:"ItemId,omitempty"` //存放物品的Id - Itype ItemType `protobuf:"varint,3,opt,name=Itype,proto3,enum=ItemType" json:"Itype,omitempty"` //物品类型 - Amount uint32 `protobuf:"varint,4,opt,name=Amount,proto3" json:"Amount,omitempty"` //存放物品的数量 + IsNew bool `protobuf:"varint,1,opt,name=IsNew,proto3" json:"IsNew,omitempty"` //是否是新的 + ItemId uint32 `protobuf:"varint,2,opt,name=ItemId,proto3" json:"ItemId,omitempty"` //存放物品的Id + Amount uint32 `protobuf:"varint,3,opt,name=Amount,proto3" json:"Amount,omitempty"` //存放物品的数量 } func (x *GridData) Reset() { @@ -127,13 +126,6 @@ func (x *GridData) GetItemId() uint32 { return 0 } -func (x *GridData) GetItype() ItemType { - if x != nil { - return x.Itype - } - return ItemType_Props -} - func (x *GridData) GetAmount() uint32 { if x != nil { return x.Amount @@ -147,8 +139,8 @@ type DB_UserPackData struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - UserId uint32 `protobuf:"varint,1,opt,name=UserId,proto3" json:"UserId,omitempty" bson:"_id"` //tags:{bson:"_id"}用户Id - Pack []*GridData `protobuf:"bytes,2,rep,name=Pack,proto3" json:"Pack,omitempty"` //背包列表 + UserId string `protobuf:"bytes,1,opt,name=UserId,proto3" json:"UserId,omitempty" bson:"_id"` //tags:{bson:"_id"}用户Id + Pack []*GridData `protobuf:"bytes,2,rep,name=Pack,proto3" json:"Pack,omitempty"` //背包列表 } func (x *DB_UserPackData) Reset() { @@ -183,11 +175,11 @@ func (*DB_UserPackData) Descriptor() ([]byte, []int) { return file_pack_db_proto_rawDescGZIP(), []int{1} } -func (x *DB_UserPackData) GetUserId() uint32 { +func (x *DB_UserPackData) GetUserId() string { if x != nil { return x.UserId } - return 0 + return "" } func (x *DB_UserPackData) GetPack() []*GridData { @@ -201,22 +193,20 @@ var File_pack_db_proto protoreflect.FileDescriptor var file_pack_db_proto_rawDesc = []byte{ 0x0a, 0x0d, 0x70, 0x61, 0x63, 0x6b, 0x5f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, - 0x71, 0x0a, 0x08, 0x47, 0x72, 0x69, 0x64, 0x44, 0x61, 0x74, 0x61, 0x12, 0x14, 0x0a, 0x05, 0x49, + 0x50, 0x0a, 0x08, 0x47, 0x72, 0x69, 0x64, 0x44, 0x61, 0x74, 0x61, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x0d, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x1f, 0x0a, 0x05, 0x49, 0x74, 0x79, - 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x09, 0x2e, 0x49, 0x74, 0x65, 0x6d, 0x54, - 0x79, 0x70, 0x65, 0x52, 0x05, 0x49, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, - 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, - 0x6e, 0x74, 0x22, 0x48, 0x0a, 0x0f, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x50, 0x61, 0x63, - 0x6b, 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1d, 0x0a, - 0x04, 0x50, 0x61, 0x63, 0x6b, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x47, 0x72, - 0x69, 0x64, 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x50, 0x61, 0x63, 0x6b, 0x2a, 0x2e, 0x0a, 0x08, - 0x49, 0x74, 0x65, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x12, 0x09, 0x0a, 0x05, 0x50, 0x72, 0x6f, 0x70, - 0x73, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x45, 0x71, 0x75, 0x69, 0x70, 0x10, 0x02, 0x12, 0x0c, - 0x0a, 0x08, 0x46, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x10, 0x0c, 0x42, 0x06, 0x5a, 0x04, - 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x0d, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, + 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, + 0x74, 0x22, 0x48, 0x0a, 0x0f, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x50, 0x61, 0x63, 0x6b, + 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1d, 0x0a, 0x04, + 0x50, 0x61, 0x63, 0x6b, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x47, 0x72, 0x69, + 0x64, 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x50, 0x61, 0x63, 0x6b, 0x2a, 0x2e, 0x0a, 0x08, 0x49, + 0x74, 0x65, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x12, 0x09, 0x0a, 0x05, 0x50, 0x72, 0x6f, 0x70, 0x73, + 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x45, 0x71, 0x75, 0x69, 0x70, 0x10, 0x02, 0x12, 0x0c, 0x0a, + 0x08, 0x46, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x10, 0x0c, 0x42, 0x06, 0x5a, 0x04, 0x2e, + 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -239,13 +229,12 @@ var file_pack_db_proto_goTypes = []interface{}{ (*DB_UserPackData)(nil), // 2: DB_UserPackData } var file_pack_db_proto_depIdxs = []int32{ - 0, // 0: GridData.Itype:type_name -> ItemType - 1, // 1: DB_UserPackData.Pack:type_name -> GridData - 2, // [2:2] is the sub-list for method output_type - 2, // [2:2] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name - 2, // [2:2] is the sub-list for extension extendee - 0, // [0:2] is the sub-list for field type_name + 1, // 0: DB_UserPackData.Pack:type_name -> GridData + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name } func init() { file_pack_db_proto_init() } diff --git a/pb/proto/comm.proto b/pb/proto/comm.proto index 07aa9a060..c32a58ad2 100644 --- a/pb/proto/comm.proto +++ b/pb/proto/comm.proto @@ -14,7 +14,7 @@ message UserMessage { message AgentMessage { string Ip = 1; string UserSessionId = 2; - uint32 UserId = 3; + string UserId = 3; string GatewayServiceId = 4; string Method = 5; bytes Message = 6; @@ -29,7 +29,7 @@ message RPCMessageReply { //用户代理绑定Uid请求 message AgentBuildReq { string UserSessionId = 1; - uint32 UserId = 2; + string UserId = 2; } //用户代理解绑请求 message AgentUnBuildReq { diff --git a/pb/proto/pack_db.proto b/pb/proto/pack_db.proto index 92d570ce7..1f144c70e 100644 --- a/pb/proto/pack_db.proto +++ b/pb/proto/pack_db.proto @@ -11,12 +11,11 @@ enum ItemType { message GridData { bool IsNew = 1; //是否是新的 uint32 ItemId = 2; //存放物品的Id - ItemType Itype = 3; //物品类型 - uint32 Amount = 4; //存放物品的数量 + uint32 Amount = 3; //存放物品的数量 } //用户背包 message DB_UserPackData { - uint32 UserId = 1; //tags:{bson:"_id"}用户Id + string UserId = 1; //tags:{bson:"_id"}用户Id repeated GridData Pack = 2; //背包列表 } \ No newline at end of file diff --git a/pb/proto/user_db.proto b/pb/proto/user_db.proto index 2276b00f7..7a239c802 100644 --- a/pb/proto/user_db.proto +++ b/pb/proto/user_db.proto @@ -8,7 +8,7 @@ message Cache_UserData { } message DB_UserData { - uint32 UserId = 1; //tags:{bson:"_id"}动态Id + string UserId = 1; //tags:{bson:"_id"}动态Id string account = 2; string NiceName = 3; string Email = 4; diff --git a/pb/user_db.pb.go b/pb/user_db.pb.go index 136214325..2ce469232 100644 --- a/pb/user_db.pb.go +++ b/pb/user_db.pb.go @@ -88,7 +88,7 @@ type DB_UserData struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - UserId uint32 `protobuf:"varint,1,opt,name=UserId,proto3" json:"UserId,omitempty" bson:"_id"` //tags:{bson:"_id"}动态Id + UserId string `protobuf:"bytes,1,opt,name=UserId,proto3" json:"UserId,omitempty" bson:"_id"` //tags:{bson:"_id"}动态Id Account string `protobuf:"bytes,2,opt,name=account,proto3" json:"account,omitempty"` NiceName string `protobuf:"bytes,3,opt,name=NiceName,proto3" json:"NiceName,omitempty"` Email string `protobuf:"bytes,4,opt,name=Email,proto3" json:"Email,omitempty"` @@ -127,11 +127,11 @@ func (*DB_UserData) Descriptor() ([]byte, []int) { return file_user_db_proto_rawDescGZIP(), []int{1} } -func (x *DB_UserData) GetUserId() uint32 { +func (x *DB_UserData) GetUserId() string { if x != nil { return x.UserId } - return 0 + return "" } func (x *DB_UserData) GetAccount() string { @@ -176,7 +176,7 @@ var file_user_db_proto_rawDesc = []byte{ 0x2e, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x52, 0x08, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x22, 0x8d, 0x01, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x18, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x69, 0x63, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, 0x65, diff --git a/sys/cache/pack.go b/sys/cache/pack.go index f370d7492..8465fa099 100644 --- a/sys/cache/pack.go +++ b/sys/cache/pack.go @@ -10,17 +10,17 @@ import ( ) const ( //Redis - Redis_PackCache string = "pack:%d" + Redis_PackCache string = "pack:%s" ) const () type IPack interface { - QueryUserPack(uId uint32) (pack *pb.DB_UserPackData, err error) + QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) } ///查询用户背包数据 -func (this *Cache) QueryUserPack(uId uint32) (pack *pb.DB_UserPackData, err error) { +func (this *Cache) QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) { pack = &pb.DB_UserPackData{ UserId: uId, } diff --git a/sys/cache/user.go b/sys/cache/user.go index 709115789..f2bae78cd 100644 --- a/sys/cache/user.go +++ b/sys/cache/user.go @@ -6,7 +6,7 @@ import ( ) const ( //Redis - Redis_UserCache string = "user:%d" //会话列表 + Redis_UserCache string = "user:%s" //会话列表 ) type IUser interface { diff --git a/sys/cache/user_test.go b/sys/cache/user_test.go index 6f12ddac3..0f2a97533 100644 --- a/sys/cache/user_test.go +++ b/sys/cache/user_test.go @@ -7,6 +7,7 @@ import ( "github.com/liwei1dao/lego/sys/redis" "github.com/stretchr/testify/require" + "go.mongodb.org/mongo-driver/bson/primitive" ) var cache *Cache @@ -28,7 +29,7 @@ func TestUpdateUser(t *testing.T) { SessionId: "1", GatewayServiceId: "work", UserData: &pb.DB_UserData{ - UserId: 1, + UserId: primitive.NewObjectID().Hex(), Account: "aaa", }, } diff --git a/sys/db/db.go b/sys/db/db.go index ff084ef3f..bac81395f 100644 --- a/sys/db/db.go +++ b/sys/db/db.go @@ -22,6 +22,5 @@ func (this *DB) init() (err error) { ); err != nil { return } - err = this.checkUserIdInit() return } diff --git a/sys/db/pack.go b/sys/db/pack.go index 333dd4f34..b50b51f62 100644 --- a/sys/db/pack.go +++ b/sys/db/pack.go @@ -12,11 +12,11 @@ const ( //Redis ) type IPack interface { - QueryUserPack(uId uint32) (pack *pb.DB_UserPackData, err error) + QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) } ///查询用户背包数据 -func (this *DB) QueryUserPack(uId uint32) (pack *pb.DB_UserPackData, err error) { +func (this *DB) QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) { pack = &pb.DB_UserPackData{} err = this.mgo.FindOne(DB_PackTable, bson.M{"_id": uId}).Decode(pack) return diff --git a/sys/db/user.go b/sys/db/user.go index 97fbc850b..12b4688d5 100644 --- a/sys/db/user.go +++ b/sys/db/user.go @@ -1,34 +1,25 @@ package db import ( - "context" - "fmt" "go_dreamfactory/pb" - "math/rand" - "time" "github.com/liwei1dao/lego/core" - "github.com/liwei1dao/lego/sys/log" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" ) const ( //Redis - DB_UserTable core.SqlTable = "user" //用户表 - DB_UserIdTable core.SqlTable = "userid" //用户id表 + DB_UserTable core.SqlTable = "user" //用户表 ) type IUser interface { User_FindUserByAccount(account string) (*pb.DB_UserData, error) - User_FindUserById(id uint32) (*pb.DB_UserData, error) + User_FindUserById(id string) (*pb.DB_UserData, error) User_CreateUser(user *pb.DB_UserData) error User_UpdateUser(data *pb.DB_UserData) (err error) } -type UserId struct { - UserId uint32 `bson:"_id"` -} - func (this *DB) User_FindUserByAccount(account string) (*pb.DB_UserData, error) { filter := bson.D{ {"account", account}, @@ -39,7 +30,7 @@ func (this *DB) User_FindUserByAccount(account string) (*pb.DB_UserData, error) return user, err } -func (this *DB) User_FindUserById(id uint32) (*pb.DB_UserData, error) { +func (this *DB) User_FindUserById(id string) (*pb.DB_UserData, error) { filter := bson.D{ {"_id", id}, } @@ -49,21 +40,15 @@ func (this *DB) User_FindUserById(id uint32) (*pb.DB_UserData, error) { return user, err } -func (this *DB) User_CreateUser(user *pb.DB_UserData) error { - userId := &UserId{} - err := this.mgo.FindOneAndDelete(DB_UserIdTable, bson.M{}).Decode(userId) - if err != nil { - log.Errorf("find userId err :%v", err) - return err - } - user.UserId = userId.UserId +func (this *DB) User_CreateUser(user *pb.DB_UserData) (err error) { + user.UserId = primitive.NewObjectID().Hex() _, err = this.mgo.InsertOne(DB_UserTable, user) return err } //更新用户数据到DB -func (this *DB) User_UpdateUser(data *pb.DB_UserData) error { - err := this.mgo.FindOneAndUpdate( +func (this *DB) User_UpdateUser(data *pb.DB_UserData) (err error) { + err = this.mgo.FindOneAndUpdate( DB_UserTable, bson.M{"_id": data.UserId}, bson.M{"$set": bson.M{ @@ -74,33 +59,3 @@ func (this *DB) User_UpdateUser(data *pb.DB_UserData) error { ).Decode(data) return err } - -//校验数据库初始化工作是否完成 -func (this *DB) checkUserIdInit() (err error) { - ctx, _ := context.WithTimeout(context.Background(), time.Second*60) - count, err := this.mgo.CountDocuments(DB_UserIdTable, bson.M{}) - if err != nil || count == 0 { - //批量插入数据 - leng := 1000000 - cIds := make([]interface{}, leng) - for i, _ := range cIds { - cIds[i] = 1000000 + i - } - data := make([]interface{}, leng) - r := rand.New(rand.NewSource(time.Now().Unix())) - n := 0 - for _, i := range r.Perm(leng) { - data[n] = bson.M{"_id": i} - n++ - } - var ( - err error - ) - begin := time.Now() - if _, err = this.mgo.InsertManyByCtx(DB_UserIdTable, ctx, data); err != nil { - return fmt.Errorf("checkUserIdInit err=%s", err.Error()) - } - log.Debugf("checkUserIdInit succ time consuming:%v", time.Now().Sub(begin)) - } - return -} diff --git a/sys/db/user_test.go b/sys/db/user_test.go index 6b5134c9f..b0128ad23 100644 --- a/sys/db/user_test.go +++ b/sys/db/user_test.go @@ -9,6 +9,7 @@ import ( "github.com/liwei1dao/lego/sys/mgo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.mongodb.org/mongo-driver/bson/primitive" ) var db *DB @@ -37,7 +38,7 @@ func TestCreate(t *testing.T) { } func TestFindOne(t *testing.T) { - user, err := db.User_FindUserById(1) + user, err := db.User_FindUserById("") require.Nil(t, err) assert.Equal(t, "legu1", user.Account) @@ -49,7 +50,7 @@ func TestFindOne(t *testing.T) { func TestUpdate(t *testing.T) { user := &pb.DB_UserData{ - UserId: 10001, + UserId: primitive.NewObjectID().String(), Email: "new@qq.com", } err := db.User_UpdateUser(user)