Merge branch 'dev' into f66

# Conflicts:
#	modules/user/login_comp.go
This commit is contained in:
zhaocy 2022-06-07 10:05:51 +08:00
commit 6e9bad8887
19 changed files with 84 additions and 151 deletions

View File

@ -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() {
}

View File

@ -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 {

View File

@ -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,

View File

@ -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) {

View File

@ -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() //主动关闭接口

View File

@ -41,7 +41,7 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe
defer func() {
session.SendMsg("pack", "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
}

View File

@ -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,

View File

@ -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() }

View File

@ -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 {

View File

@ -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; //
}

View File

@ -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;

View File

@ -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,

6
sys/cache/pack.go vendored
View File

@ -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,
}

2
sys/cache/user.go vendored
View File

@ -6,7 +6,7 @@ import (
)
const ( //Redis
Redis_UserCache string = "user:%d" //会话列表
Redis_UserCache string = "user:%s" //会话列表
)
type IUser interface {

View File

@ -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",
},
}

View File

@ -22,6 +22,5 @@ func (this *DB) init() (err error) {
); err != nil {
return
}
err = this.checkUserIdInit()
return
}

View File

@ -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

View File

@ -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
}

View File

@ -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)