From 4931d310e20cc67f4bd4a05cdc1157c1c689c8df Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Tue, 14 Jun 2022 15:21:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E5=BA=95=E5=B1=82=E4=BA=92?= =?UTF-8?q?=E7=9B=B8=E5=BC=95=E7=94=A8=E6=96=B9=E5=BC=8F=E8=AF=A5=E7=94=B1?= =?UTF-8?q?=E4=B8=8A=E5=B1=82=E6=8C=89=E9=9C=80=E6=89=A7=E8=A1=8C=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=BC=95=E7=94=A8=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/cache_comp.go | 5 ----- modules/db_comp.go | 6 ------ modules/gate_comp.go | 16 ++++++++-------- modules/mail/api_delmail.go | 4 ++-- modules/mail/api_getAttachment.go | 6 +++--- modules/mail/api_getmail.go | 2 +- modules/mail/api_readmail.go | 2 +- modules/mail/module.go | 4 +++- modules/modulebase.go | 20 +------------------- modules/pack/api.go | 2 ++ modules/pack/api_getlist.go | 10 +++++----- modules/pack/api_sellItem.go | 2 +- modules/pack/api_useItem.go | 2 +- modules/pack/cache_comp.go | 10 ++++++---- modules/pack/module.go | 18 +++++++++++------- modules/pack/pack_test.go | 27 +++++++++++++++++++++++++++ modules/user/login_comp.go | 13 +++++++++++-- 17 files changed, 83 insertions(+), 66 deletions(-) create mode 100644 modules/pack/pack_test.go diff --git a/modules/cache_comp.go b/modules/cache_comp.go index a14251860..86f701be9 100644 --- a/modules/cache_comp.go +++ b/modules/cache_comp.go @@ -1,7 +1,6 @@ package modules import ( - "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/core/cbase" "go_dreamfactory/lego/sys/redis" @@ -13,16 +12,12 @@ import ( */ type MComp_CacheComp struct { cbase.ModuleCompBase - S base.IRPCXService //rpc服务对象 - M IModule //当前业务模块 Redis redis.ISys } //组件初始化接口 func (this *MComp_CacheComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.ModuleCompBase.Init(service, module, comp, options) - this.S = service.(base.IRPCXService) - this.M = module.(IModule) this.Redis = cache.Redis() return } diff --git a/modules/db_comp.go b/modules/db_comp.go index 5c2254caa..7b956b021 100644 --- a/modules/db_comp.go +++ b/modules/db_comp.go @@ -2,7 +2,6 @@ package modules import ( "go_dreamfactory/comm" - "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/core/cbase" "go_dreamfactory/lego/sys/log" @@ -17,9 +16,6 @@ import ( */ type MComp_DBComp struct { cbase.ModuleCompBase - S base.IRPCXService //rpc服务对象 - M IModule //当前业务模块 - DB mgo.ISys } @@ -28,8 +24,6 @@ const () //组件初始化接口 func (this *MComp_DBComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.ModuleCompBase.Init(service, module, comp, options) - this.S = service.(base.IRPCXService) - this.M = module.(IModule) this.DB = db.Mgo() return } diff --git a/modules/gate_comp.go b/modules/gate_comp.go index 84533b4e1..a00e645cc 100644 --- a/modules/gate_comp.go +++ b/modules/gate_comp.go @@ -27,17 +27,17 @@ var typeOfError = reflect.TypeOf((*error)(nil)).Elem() */ type MComp_GateComp struct { cbase.ModuleCompBase - S base.IRPCXService //rpc服务对象 - M IModule //当前业务模块 - comp core.IModuleComp //网关组件自己 - scomp comm.ISC_GateRouteComp + service base.IRPCXService //rpc服务对象 + module core.IModule //当前业务模块 + comp core.IModuleComp //网关组件自己 + scomp comm.ISC_GateRouteComp } //组件初始化接口 func (this *MComp_GateComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.ModuleCompBase.Init(service, module, comp, options) - this.S = service.(base.IRPCXService) - this.M = module.(IModule) + this.service = service.(base.IRPCXService) + this.module = module this.comp = comp return } @@ -49,7 +49,7 @@ func (this *MComp_GateComp) Start() (err error) { } var comp core.IServiceComp //注册远程路由 - if comp, err = this.S.GetComp(comm.SC_ServiceGateRouteComp); err != nil { + if comp, err = this.service.GetComp(comm.SC_ServiceGateRouteComp); err != nil { return } this.scomp = comp.(comm.ISC_GateRouteComp) @@ -96,7 +96,7 @@ func (this *MComp_GateComp) reflectionRouteHandle(method reflect.Method) bool { if returnType := mtype.Out(0); returnType != typeOfError { return false } - this.scomp.RegisterRoute(fmt.Sprintf("%s.%s", this.M.GetType(), strings.ToLower(mname)), reflect.ValueOf(this.comp), replyType, method) + this.scomp.RegisterRoute(fmt.Sprintf("%s.%s", this.module.GetType(), strings.ToLower(mname)), reflect.ValueOf(this.comp), replyType, method) return true } diff --git a/modules/mail/api_delmail.go b/modules/mail/api_delmail.go index eafc9113e..0a9e10f12 100644 --- a/modules/mail/api_delmail.go +++ b/modules/mail/api_delmail.go @@ -19,12 +19,12 @@ func (this *Api_Comp) DelUserMailReq(ctx context.Context, session comm.IUserSess code = pb.ErrorCode_NoLogin return } - bRet := this.M.DB().(*DB_Comp).Mail_DelUserMail(req.ObjID) + bRet := this.module.db_comp.Mail_DelUserMail(req.ObjID) if !bRet { code = pb.ErrorCode_DBError return } - if mailinfo, err = this.M.DB().(*DB_Comp).Mail_QueryUserMail(session.GetUserId()); err != nil { + if mailinfo, err = this.module.db_comp.Mail_QueryUserMail(session.GetUserId()); err != nil { log.Errorf("QueryUserMailResp err:%v", err) code = pb.ErrorCode_CacheReadError return diff --git a/modules/mail/api_getAttachment.go b/modules/mail/api_getAttachment.go index e9a73caf0..319e264c1 100644 --- a/modules/mail/api_getAttachment.go +++ b/modules/mail/api_getAttachment.go @@ -20,12 +20,12 @@ func (this *Api_Comp) GetUserMailAttachmentReq(ctx context.Context, session comm code = pb.ErrorCode_NoLogin return } - _bGet := this.M.DB().(*DB_Comp).Mail_GetMailAttachmentState(req.ObjID) + _bGet := this.module.db_comp.Mail_GetMailAttachmentState(req.ObjID) if !_bGet { code = pb.ErrorCode_StateInvalid return } - _data, err := this.M.DB().(*DB_Comp).Mail_GetMailAttachment(req.ObjID) + _data, err := this.module.db_comp.Mail_GetMailAttachment(req.ObjID) if err != nil { if len(_data) > 0 { // todo 领取附件 @@ -36,7 +36,7 @@ func (this *Api_Comp) GetUserMailAttachmentReq(ctx context.Context, session comm bRet := this.pack.AddItemsToUserPack(mail.UserId, _items) if bRet != nil { // 修改状态 - this.M.DB().(*DB_Comp).Mail_UpdateMailAttachmentState(req.ObjID) + this.module.db_comp.Mail_UpdateMailAttachmentState(req.ObjID) mail.Reward = true return } diff --git a/modules/mail/api_getmail.go b/modules/mail/api_getmail.go index aedbf4e71..4a65cb6e0 100644 --- a/modules/mail/api_getmail.go +++ b/modules/mail/api_getmail.go @@ -19,7 +19,7 @@ func (this *Api_Comp) QueryUserMailReq(ctx context.Context, session comm.IUserSe code = pb.ErrorCode_NoLogin return } - if mailinfo, err = this.M.DB().(*DB_Comp).Mail_QueryUserMail(session.GetUserId()); err != nil { + if mailinfo, err = this.module.db_comp.Mail_QueryUserMail(session.GetUserId()); err != nil { log.Errorf("QueryUserMailResp err:%v", err) code = pb.ErrorCode_CacheReadError return diff --git a/modules/mail/api_readmail.go b/modules/mail/api_readmail.go index 6bf7a98fe..8df2a766d 100644 --- a/modules/mail/api_readmail.go +++ b/modules/mail/api_readmail.go @@ -20,7 +20,7 @@ func (this *Api_Comp) ReadUserMailReq(ctx context.Context, session comm.IUserSes return } - mail, err = this.M.DB().(*DB_Comp).Mail_ReadOneMail(req.ObjID) + mail, err = this.module.db_comp.Mail_ReadOneMail(req.ObjID) if err != nil { code = pb.ErrorCode_ReqParameterError } diff --git a/modules/mail/module.go b/modules/mail/module.go index d77e47e93..3a2507d38 100644 --- a/modules/mail/module.go +++ b/modules/mail/module.go @@ -26,6 +26,7 @@ func NewModule() core.IModule { type Mail struct { modules.ModuleBase api_comp *Api_Comp + db_comp *DB_Comp configure_comp *Configure_Comp } @@ -36,6 +37,7 @@ func (this *Mail) GetType() core.M_Modules { func (this *Mail) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp) + this.db_comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp) this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) } @@ -50,7 +52,7 @@ func (this *Mail) CreateNewMail(uId string) { Check: false, Reward: false, } - err := this.DB().(*DB_Comp).Mail_InsertUserMail(mail) + err := this.db_comp.Mail_InsertUserMail(mail) if err != nil { log.Error("create mail failed") } diff --git a/modules/modulebase.go b/modules/modulebase.go index adb7eb40e..33d4c03c1 100644 --- a/modules/modulebase.go +++ b/modules/modulebase.go @@ -20,25 +20,7 @@ import ( */ type ModuleBase struct { cbase.ModuleBase - service base.IRPCXService - Api_Comp IAPI_Comp - Cache_Comp ICache_Comp - Db_Comp IDB_Comp - Configure_Comp IConfigure_Comp -} - -func (this *ModuleBase) API() IAPI_Comp { - return this.Api_Comp -} -func (this *ModuleBase) Cache() ICache_Comp { - return this.Cache_Comp -} -func (this *ModuleBase) DB() IDB_Comp { - return this.Db_Comp -} - -func (this *ModuleBase) Configure() IConfigure_Comp { - return this.Configure_Comp + service base.IRPCXService } //模块初始化接口 diff --git a/modules/pack/api.go b/modules/pack/api.go index ed0ef61f2..ce2ba47fc 100644 --- a/modules/pack/api.go +++ b/modules/pack/api.go @@ -17,10 +17,12 @@ const ( //消息回复的头名称 */ type Api_Comp struct { modules.MComp_GateComp + module *Pack } //组件初始化接口 func (this *Api_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.MComp_GateComp.Init(service, module, comp, options) + this.module = module.(*Pack) return } diff --git a/modules/pack/api_getlist.go b/modules/pack/api_getlist.go index 6982add0f..fa871f0e1 100644 --- a/modules/pack/api_getlist.go +++ b/modules/pack/api_getlist.go @@ -29,23 +29,23 @@ func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, re dels []string ) defer func() { - session.SendMsg(string(this.M.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids}) + session.SendMsg(string(this.module.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids}) if code == pb.ErrorCode_Success { go func() { //异步处理修改数据 - this.M.Cache().(*Cache_Comp).Pack_UpdateUserPack(session.GetUserId(), modifys...) - this.M.Cache().(*Cache_Comp).Pack_DeleteUserPack(session.GetUserId(), dels...) + this.module.cache_comp.Pack_UpdateUserPack(session.GetUserId(), modifys...) + this.module.cache_comp.Pack_DeleteUserPack(session.GetUserId(), dels...) }() } }() if code = this.Getlist_Check(ctx, session, req); code != pb.ErrorCode_Success { return } - if items, err = this.M.Cache().(*Cache_Comp).Pack_QueryUserPack(session.GetUserId()); err != nil { + if items, err = this.module.cache_comp.Pack_QueryUserPack(session.GetUserId()); err != nil { log.Errorf("QueryUserPackReq err:%v", err) code = pb.ErrorCode_CacheReadError return } else { - tempgrids = this.M.Configure().(*Configure_Comp).GetPackItemByType(items, req.IType) + tempgrids = this.module.configure_comp.GetPackItemByType(items, req.IType) modifys = make([]*pb.DB_UserItemData, 0, len(tempgrids)) dels = make([]string, 0, len(tempgrids)) grids = make([]*pb.DB_UserItemData, 0, len(grids)) diff --git a/modules/pack/api_sellItem.go b/modules/pack/api_sellItem.go index 63ea1dd46..da9a3f651 100644 --- a/modules/pack/api_sellItem.go +++ b/modules/pack/api_sellItem.go @@ -21,7 +21,7 @@ func (this *Api_Comp) SellItem(ctx context.Context, session comm.IUserSession, r code pb.ErrorCode ) defer func() { - session.SendMsg(string(this.M.GetType()), SellItemResp, code, &pb.SellItemResp{}) + session.SendMsg(string(this.module.GetType()), SellItemResp, code, &pb.SellItemResp{}) }() if code = this.SellItem_Check(ctx, session, req); code != pb.ErrorCode_Success { return diff --git a/modules/pack/api_useItem.go b/modules/pack/api_useItem.go index fa42d4967..930296641 100644 --- a/modules/pack/api_useItem.go +++ b/modules/pack/api_useItem.go @@ -21,7 +21,7 @@ func (this *Api_Comp) Useitem(ctx context.Context, session comm.IUserSession, re code pb.ErrorCode ) defer func() { - session.SendMsg(string(this.M.GetType()), UseItemResp, code, &pb.UseItemResp{}) + session.SendMsg(string(this.module.GetType()), UseItemResp, code, &pb.UseItemResp{}) }() if code = this.Useitem_Check(ctx, session, req); code != pb.ErrorCode_Success { return diff --git a/modules/pack/cache_comp.go b/modules/pack/cache_comp.go index 3ca2412ca..8000b93bc 100644 --- a/modules/pack/cache_comp.go +++ b/modules/pack/cache_comp.go @@ -15,11 +15,13 @@ import ( ///背包缓存数据管理组件 type Cache_Comp struct { modules.MComp_CacheComp + module *Pack } //组件初始化接口 func (this *Cache_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.ModuleCompBase.Init(service, module, comp, options) + this.module = module.(*Pack) return } @@ -36,7 +38,7 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemD } return } else if err == redis.RedisNil { - if itmes, err = this.M.DB().(*DB_Comp).Pack_QueryUserPack(uId); err == nil { + if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil { temp = make(map[string]interface{}) for _, v := range itmes { temp[v.GridId] = v @@ -59,7 +61,7 @@ func (this *Cache_Comp) Pack_QueryUserPackByGridId(uId string, grid string) (itm if err = this.Redis.HGet(fmt.Sprintf(Redis_PackCache, uId), grid, itme); err == nil { return } else if err == redis.RedisNil { - if itmes, err = this.M.DB().(*DB_Comp).Pack_QueryUserPack(uId); err == nil { + if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil { temp = make(map[string]interface{}) for _, v := range itmes { temp[v.GridId] = v @@ -86,7 +88,7 @@ func (this *Cache_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItem temp[v.GridId] = v } if err = this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp); err != nil { - this.M.DB().(*DB_Comp).Pack_UpdateGridToUserPack(uId, itmes...) + this.module.db_comp.Pack_UpdateGridToUserPack(uId, itmes...) } return @@ -95,7 +97,7 @@ func (this *Cache_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItem //更新用户的背包信息 func (this *Cache_Comp) Pack_DeleteUserPack(uId string, gridIds ...string) (err error) { if err = this.Redis.HDel(fmt.Sprintf(Redis_PackCache, uId), gridIds...); err != nil { - err = this.M.DB().(*DB_Comp).Pack_DeleteGridToUserPack(uId, gridIds...) + err = this.module.db_comp.Pack_DeleteGridToUserPack(uId, gridIds...) } return } diff --git a/modules/pack/module.go b/modules/pack/module.go index 395f200b8..853a653a0 100644 --- a/modules/pack/module.go +++ b/modules/pack/module.go @@ -20,6 +20,10 @@ func NewModule() core.IModule { type Pack struct { modules.ModuleBase + api_comp *Api_Comp + cache_comp *Cache_Comp + db_comp *DB_Comp + configure_comp *Configure_Comp } //模块名称 @@ -36,24 +40,24 @@ func (this *Pack) Init(service core.IService, module core.IModule, options core. //装备组件 func (this *Pack) OnInstallComp() { this.ModuleBase.OnInstallComp() - this.Api_Comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp) - this.Cache_Comp = this.RegisterComp(new(Cache_Comp)).(*Cache_Comp) - this.Db_Comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp) - this.Configure_Comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) + this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp) + this.cache_comp = this.RegisterComp(new(Cache_Comp)).(*Cache_Comp) + this.db_comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp) + this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) } //IPack------------------------------------------------------------------------------------------------------------------------------- ///查询用户背包物品数量 func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) { defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount) - amount = this.Cache_Comp.(*Cache_Comp).Pack_QueryUserPackItemAmount(uId, itemid) + amount = this.cache_comp.Pack_QueryUserPackItemAmount(uId, itemid) return } ///添加单个物品到背包 (可以加物品和减物品) func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error) { defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil) - if err = this.Cache_Comp.(*Cache_Comp).Pack_AddItemToUserPack(uId, itemid, addnum); err != nil { + if err = this.cache_comp.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil { log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err) } return @@ -62,7 +66,7 @@ func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error ///添加多个物品到背包 (可以加物品和减物品) func (this *Pack) AddItemsToUserPack(uId string, items map[int32]int32) (err error) { defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil) - if err = this.Cache_Comp.(*Cache_Comp).Pack_AddItemsToUserPack(uId, items); err != nil { + if err = this.cache_comp.Pack_AddItemsToUserPack(uId, items); err != nil { log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err) } return diff --git a/modules/pack/pack_test.go b/modules/pack/pack_test.go new file mode 100644 index 000000000..2d794c304 --- /dev/null +++ b/modules/pack/pack_test.go @@ -0,0 +1,27 @@ +package pack_test + +import ( + "fmt" + "go_dreamfactory/sys/cache" + "go_dreamfactory/sys/configure" + "go_dreamfactory/sys/db" + "os" + "testing" +) + +//测试环境下初始化db和cache 系统 +func TestMain(m *testing.M) { + if err := db.OnInit(nil, db.Set_MongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), db.Set_MongodbDatabase("dreamfactory")); err != nil { + fmt.Printf("err:%v\n", err) + return + } + if err := cache.OnInit(nil, cache.Set_Redis_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}), cache.Set_Redis_Password("")); err != nil { + fmt.Printf("err:%v\n", err) + return + } + if err := configure.OnInit(nil); err != nil { + fmt.Printf("err:%v\n", err) + return + } + defer os.Exit(m.Run()) +} diff --git a/modules/user/login_comp.go b/modules/user/login_comp.go index c984af2a0..6ce106e79 100644 --- a/modules/user/login_comp.go +++ b/modules/user/login_comp.go @@ -11,6 +11,8 @@ import ( "go_dreamfactory/utils" "time" + "go_dreamfactory/lego/base" + "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/event" "go_dreamfactory/lego/sys/log" @@ -20,6 +22,13 @@ import ( type LoginComp struct { modules.MComp_GateComp + service base.IRPCXService +} + +func (this *LoginComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.MComp_GateComp.Init(service, module, comp, options) + this.service = service.(base.IRPCXService) + return } //解码 @@ -81,9 +90,9 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req log.Errorf("User_CreateUser err %v", err) return } - session.Bind(user.Uid, this.S.GetId()) + session.Bind(user.Uid, this.service.GetId()) } else { - session.Bind(db_user.Uid, this.S.GetId()) + session.Bind(db_user.Uid, this.service.GetId()) } cache_user := &pb.Cache_UserData{