From 5036f6b2b0cd1a46c1c39ba87156b9b7f6fe4565 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 13 Jul 2022 16:50:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0API=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E6=89=AB=E6=8F=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/comp_gate.go | 17 ++++++++++++++++- modules/friend/api_del.go | 4 ++-- modules/gateway/agent.go | 4 ++-- modules/notify/api.go | 4 ++-- modules/notify/api_getlist.go | 6 +++--- modules/notify/modelNotify.go | 4 ++-- modules/notify/module.go | 12 ++++++------ modules/notify/module_test.go | 5 +++-- modules/user/api_logout.go | 6 ++++-- services/worker/main.go | 2 ++ 10 files changed, 42 insertions(+), 22 deletions(-) diff --git a/modules/comp_gate.go b/modules/comp_gate.go index 23c1b2999..135938dae 100644 --- a/modules/comp_gate.go +++ b/modules/comp_gate.go @@ -67,26 +67,37 @@ func (this *MCompGate) suitableMethods() { typ := reflect.TypeOf(this.comp) for m := 0; m < typ.NumMethod(); m++ { method := typ.Method(m) + mname := method.Name + if mname == "Start" || + mname == "Init" || + mname == "Destroy" || + strings.HasSuffix(mname, "Check") { + continue + } this.reflectionRouteHandle(typ, method) } } //反射注册路由处理函数 -func (this *MCompGate) reflectionRouteHandle(typ reflect.Type, method reflect.Method) { +func (this *MCompGate) reflectionRouteHandle(typ reflect.Type, method reflect.Method) (ret bool) { mtype := method.Type mname := method.Name if method.PkgPath != "" { + log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname) return } if mtype.NumIn() != 3 { + log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname) return } sessionType := mtype.In(1) if !sessionType.Implements(typeOfSession) { + log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname) return } agrType := mtype.In(2) if !agrType.Implements(typeOfMessage) { + log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname) return } // if agrType.Kind() != reflect.Ptr { @@ -97,14 +108,17 @@ func (this *MCompGate) reflectionRouteHandle(typ reflect.Type, method reflect.Me // } if mtype.NumOut() != 2 { + log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname) return } returnCodeType := mtype.Out(0) if returnCodeType != typeOfErrorCode { + log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname) return } returnDataType := mtype.Out(1) if !returnDataType.Implements(typeOfMessage) { + log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname) return } //寻找校验函数 @@ -119,6 +133,7 @@ func (this *MCompGate) reflectionRouteHandle(typ reflect.Type, method reflect.Me log.Panicf("反射注册用户处理函数错误 [%s-%s]校验函数格式异常:%v", this.module.GetType(), mname, err) return } + return true } //反射注册路由校验函数 diff --git a/modules/friend/api_del.go b/modules/friend/api_del.go index aa81bbb0c..0ae546d00 100644 --- a/modules/friend/api_del.go +++ b/modules/friend/api_del.go @@ -15,10 +15,10 @@ func (this *apiComp) DelCheck(session comm.IUserSession, req *pb.FriendDelReq) ( } //删除好友 -func (this *apiComp) Del(session comm.IUserSession, chk map[string]interface{}, req *pb.FriendDelReq) (code pb.ErrorCode, data proto.Message) { +func (this *apiComp) Del(session comm.IUserSession, req *pb.FriendDelReq) (code pb.ErrorCode, data proto.Message) { if code = this.DelCheck(session, req); code != pb.ErrorCode_Success { return } - + return } diff --git a/modules/gateway/agent.go b/modules/gateway/agent.go index a6a266b8e..07fad20ce 100644 --- a/modules/gateway/agent.go +++ b/modules/gateway/agent.go @@ -147,7 +147,7 @@ func (this *Agent) decodeUserData(msg *pb.UserMessage) error { } //只有login的时候才需要设置Data - if msg.MainType == "user" && msg.SubType == "login" { + if msg.MainType == string(comm.ModuleUser) && msg.SubType == "login" { serverId := jsonRet.Get("serverId").Int() account := jsonRet.Get("account").String() req := &pb.UserLoginReq{ @@ -160,7 +160,7 @@ func (this *Agent) decodeUserData(msg *pb.UserMessage) error { } msg.Data = ad } else { - if this.UserId() == "" { + if msg.MainType != string(comm.ModuleNotify) && this.UserId() == "" { return fmt.Errorf("no login") } } diff --git a/modules/notify/api.go b/modules/notify/api.go index 2158dd643..c0cc80339 100644 --- a/modules/notify/api.go +++ b/modules/notify/api.go @@ -12,13 +12,13 @@ API type apiComp struct { modules.MCompGate service core.IService - module *Notification + module *Notify } //组件初始化接口 func (this *apiComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.MCompGate.Init(service, module, comp, options) - this.module = module.(*Notification) + this.module = module.(*Notify) this.service = service return } diff --git a/modules/notify/api_getlist.go b/modules/notify/api_getlist.go index 00c8028a2..b493c3f0b 100644 --- a/modules/notify/api_getlist.go +++ b/modules/notify/api_getlist.go @@ -4,17 +4,17 @@ import ( "go_dreamfactory/comm" "go_dreamfactory/pb" - "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/proto" ) //参数校验 -func (this *apiComp) GetlistCheck(session comm.IUserSession, req *pb.NotifyGetListReq) (code pb.ErrorCode) { +func (this *apiComp) GetListCheck(session comm.IUserSession, req *pb.NotifyGetListReq) (code pb.ErrorCode) { return } ///获取系统公告 -func (this *apiComp) Getlist(session comm.IUserSession, req *pb.NotifyGetListReq) (code pb.ErrorCode, data proto.Message) { +func (this *apiComp) GetList(session comm.IUserSession, req *pb.NotifyGetListReq) (code pb.ErrorCode, data proto.Message) { var ( err error userexpand *pb.DBUserExpand diff --git a/modules/notify/modelNotify.go b/modules/notify/modelNotify.go index dcb684ae4..16c9ed060 100644 --- a/modules/notify/modelNotify.go +++ b/modules/notify/modelNotify.go @@ -16,13 +16,13 @@ import ( ///论坛 数据组件 type modelNotifyComp struct { modules.MCompModel - module *Notification + module *Notify } //组件初始化接口 func (this *modelNotifyComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) { this.MCompModel.Init(service, module, comp, opt) - this.module = module.(*Notification) + this.module = module.(*Notify) this.TableName = "notify" //创建uid索引 this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ diff --git a/modules/notify/module.go b/modules/notify/module.go index 2b857732a..9368783d1 100644 --- a/modules/notify/module.go +++ b/modules/notify/module.go @@ -13,29 +13,29 @@ import ( 开发:李伟 */ func NewModule() core.IModule { - m := new(Notification) + m := new(Notify) return m } -type Notification struct { +type Notify struct { modules.ModuleBase api_comp *apiComp modelNotify *modelNotifyComp } //模块名 -func (this *Notification) GetType() core.M_Modules { - return comm.ModuleEquipment +func (this *Notify) GetType() core.M_Modules { + return comm.ModuleNotify } //模块初始化接口 注册用户创建角色事件 -func (this *Notification) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { +func (this *Notify) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) return } //装备组件 -func (this *Notification) OnInstallComp() { +func (this *Notify) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp) this.modelNotify = this.RegisterComp(new(modelNotifyComp)).(*modelNotifyComp) diff --git a/modules/notify/module_test.go b/modules/notify/module_test.go index 40eabc811..85f21f400 100644 --- a/modules/notify/module_test.go +++ b/modules/notify/module_test.go @@ -1,4 +1,4 @@ -package notify +package notify_test import ( "fmt" @@ -7,6 +7,7 @@ import ( "go_dreamfactory/lego/base/rpcx" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" + "go_dreamfactory/modules/notify" "go_dreamfactory/services" "go_dreamfactory/sys/cache" "go_dreamfactory/sys/configure" @@ -49,7 +50,7 @@ func (this *TestService) InitSys() { var service core.IService var s_gateComp comm.ISC_GateRouteComp = services.NewGateRouteComp() -var module = new(Notification) +var module = new(notify.Notify) //测试环境下初始化db和cache 系统 func TestMain(m *testing.M) { diff --git a/modules/user/api_logout.go b/modules/user/api_logout.go index 995048311..b2172c72e 100644 --- a/modules/user/api_logout.go +++ b/modules/user/api_logout.go @@ -4,14 +4,16 @@ import ( "go_dreamfactory/comm" "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" + + "google.golang.org/protobuf/proto" ) -func (this *apiComp) LogoutCheck(session comm.IUserSession, req *pb.UserLoginReq) (result map[string]interface{}, code comm.ErrorCode) { +func (this *apiComp) LogoutCheck(session comm.IUserSession, req *pb.UserLoginReq) (code pb.ErrorCode) { return } //注销 -func (this *apiComp) Logout(session comm.IUserSession, result map[string]interface{}, rsp *pb.UserLoginReq) (code pb.ErrorCode) { +func (this *apiComp) Logout(session comm.IUserSession, rsp *pb.UserLoginReq) (code pb.ErrorCode, data proto.Message) { log.Debugf("User - Logout: session:%v rsp:%v", session.ToString(), rsp) return diff --git a/services/worker/main.go b/services/worker/main.go index 9e2e6c9d1..9813e9099 100644 --- a/services/worker/main.go +++ b/services/worker/main.go @@ -8,6 +8,7 @@ import ( "go_dreamfactory/modules/hero" "go_dreamfactory/modules/items" "go_dreamfactory/modules/mail" + "go_dreamfactory/modules/notify" "go_dreamfactory/modules/shop" "go_dreamfactory/modules/story" "go_dreamfactory/modules/task" @@ -50,6 +51,7 @@ func main() { task.NewModule(), story.NewModule(), shop.NewModule(), + notify.NewModule(), ) }