From 9e8a8104d85052d2c25feab39d99030f9ca2614d Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 15 Jun 2022 10:48:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0api=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- comm/core.go | 1 + modules/gate_comp.go | 70 ++++++++++++++++++++++++++++-------- modules/pack/api_getlist.go | 13 ++----- modules/pack/api_sellItem.go | 13 ++----- modules/pack/api_useItem.go | 14 +++----- services/comp_gateroute.go | 32 +++++++++++------ 6 files changed, 87 insertions(+), 56 deletions(-) diff --git a/comm/core.go b/comm/core.go index 02d6d204c..3a65efd1c 100644 --- a/comm/core.go +++ b/comm/core.go @@ -53,6 +53,7 @@ const ( type ISC_GateRouteComp interface { core.IServiceComp RegisterRoute(methodName string, comp reflect.Value, msg reflect.Type, fn reflect.Method) + RegisterRouteCheck(methodName string, comp reflect.Value, msg reflect.Type, fn reflect.Method) } type Autogenerated struct { diff --git a/modules/gate_comp.go b/modules/gate_comp.go index a00e645cc..69526fafd 100644 --- a/modules/gate_comp.go +++ b/modules/gate_comp.go @@ -1,9 +1,9 @@ package modules import ( - "context" "fmt" "go_dreamfactory/comm" + "go_dreamfactory/pb" "reflect" "strings" "unicode" @@ -18,8 +18,10 @@ import ( 模块网关组件的基类实现 模块接收用户的消息请求都需要通过装备继承此组件的api组件来实现 */ -var typeOfContext = reflect.TypeOf((*context.Context)(nil)).Elem() +// var typeOfContext = reflect.TypeOf((*context.Context)(nil)).Elem() var typeOfSession = reflect.TypeOf((*comm.IUserSession)(nil)).Elem() +var typeOfMapStringIntface = reflect.TypeOf((map[string]interface{})(nil)).Elem() +var typeOfErrorCode = reflect.TypeOf((*pb.ErrorCode)(nil)).Elem() var typeOfError = reflect.TypeOf((*error)(nil)).Elem() /* @@ -62,42 +64,80 @@ func (this *MComp_GateComp) suitableMethods(typ reflect.Type) { for m := 0; m < typ.NumMethod(); m++ { method := typ.Method(m) this.reflectionRouteHandle(method) + this.reflectionRouteCheck(method) } } -//反射路由处理函数 -func (this *MComp_GateComp) reflectionRouteHandle(method reflect.Method) bool { +//反射注册路由处理函数 +func (this *MComp_GateComp) reflectionRouteHandle(method reflect.Method) { mtype := method.Type mname := method.Name if method.PkgPath != "" { - return false + return } if mtype.NumIn() != 4 { - return false + return } ctxType := mtype.In(1) - if !ctxType.Implements(typeOfContext) { - return false + if !ctxType.Implements(typeOfSession) { + return } argType := mtype.In(2) - if !argType.Implements(typeOfSession) { - return false + if !argType.Implements(typeOfMapStringIntface) { + return } replyType := mtype.In(3) if replyType.Kind() != reflect.Ptr { - return false + return } if !this.isExportedOrBuiltinType(replyType) { - return false + return } if mtype.NumOut() != 1 { - return false + return } if returnType := mtype.Out(0); returnType != typeOfError { - return false + return } this.scomp.RegisterRoute(fmt.Sprintf("%s.%s", this.module.GetType(), strings.ToLower(mname)), reflect.ValueOf(this.comp), replyType, method) - return true +} + +//反射注册路由校验函数 +func (this *MComp_GateComp) reflectionRouteCheck(method reflect.Method) { + mtype := method.Type + mname := strings.Split(method.Name, "_") + if len(mname) != 2 || mname[1] != "Check" { + return + } + if method.PkgPath != "" { + return + } + if mtype.NumIn() != 3 { + return + } + ctxType := mtype.In(1) + if !ctxType.Implements(typeOfSession) { + return + } + replyType := mtype.In(2) + if replyType.Kind() != reflect.Ptr { + return + } + if !this.isExportedOrBuiltinType(replyType) { + return + } + if mtype.NumOut() != 2 { + return + } + returnMapType := mtype.Out(0) + if !returnMapType.Implements(typeOfMapStringIntface) { + return + } + returnCodeType := mtype.Out(1) + if returnCodeType != typeOfErrorCode { + return + } + this.scomp.RegisterRouteCheck(fmt.Sprintf("%s.%s", this.module.GetType(), strings.ToLower(mname[0])), reflect.ValueOf(this.comp), replyType, method) } func (this *MComp_GateComp) isExportedOrBuiltinType(t reflect.Type) bool { diff --git a/modules/pack/api_getlist.go b/modules/pack/api_getlist.go index fa871f0e1..0f9fcd79c 100644 --- a/modules/pack/api_getlist.go +++ b/modules/pack/api_getlist.go @@ -1,7 +1,6 @@ package pack import ( - "context" "go_dreamfactory/comm" "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" @@ -9,16 +8,12 @@ import ( ) //参数校验 -func (this *Api_Comp) Getlist_Check(ctx context.Context, session comm.IUserSession, req *pb.GetlistReq) (code pb.ErrorCode) { - if !session.IsLogin() { - code = pb.ErrorCode_NoLogin - return - } +func (this *Api_Comp) Getlist_Check(session comm.IUserSession, req *pb.GetlistReq) (result map[string]interface{}, code pb.ErrorCode) { return } ///获取用户道具 -func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, req *pb.GetlistReq) (err error) { +func (this *Api_Comp) Getlist(session comm.IUserSession, agrs map[string]interface{}, req *pb.GetlistReq) (err error) { var ( code pb.ErrorCode items []*pb.DB_UserItemData @@ -37,9 +32,7 @@ func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, re }() } }() - if code = this.Getlist_Check(ctx, session, req); code != pb.ErrorCode_Success { - return - } + if items, err = this.module.cache_comp.Pack_QueryUserPack(session.GetUserId()); err != nil { log.Errorf("QueryUserPackReq err:%v", err) code = pb.ErrorCode_CacheReadError diff --git a/modules/pack/api_sellItem.go b/modules/pack/api_sellItem.go index da9a3f651..b5ddf36ea 100644 --- a/modules/pack/api_sellItem.go +++ b/modules/pack/api_sellItem.go @@ -1,30 +1,23 @@ package pack import ( - "context" "go_dreamfactory/comm" "go_dreamfactory/pb" ) //参数校验 -func (this *Api_Comp) SellItem_Check(ctx context.Context, session comm.IUserSession, req *pb.SellItemReq) (code pb.ErrorCode) { - if !session.IsLogin() { - code = pb.ErrorCode_NoLogin - return - } +func (this *Api_Comp) SellItem_Check(session comm.IUserSession, req *pb.SellItemReq) (result map[string]interface{}, code pb.ErrorCode) { + return } //出售道具 -func (this *Api_Comp) SellItem(ctx context.Context, session comm.IUserSession, req *pb.SellItemReq) (err error) { +func (this *Api_Comp) SellItem(session comm.IUserSession, agrs map[string]interface{}, req *pb.SellItemReq) (err error) { var ( code pb.ErrorCode ) defer func() { session.SendMsg(string(this.module.GetType()), SellItemResp, code, &pb.SellItemResp{}) }() - if code = this.SellItem_Check(ctx, session, req); code != pb.ErrorCode_Success { - return - } return } diff --git a/modules/pack/api_useItem.go b/modules/pack/api_useItem.go index 930296641..ce6302207 100644 --- a/modules/pack/api_useItem.go +++ b/modules/pack/api_useItem.go @@ -1,30 +1,24 @@ package pack import ( - "context" "go_dreamfactory/comm" "go_dreamfactory/pb" ) //参数校验 -func (this *Api_Comp) Useitem_Check(ctx context.Context, session comm.IUserSession, req *pb.UseItemReq) (code pb.ErrorCode) { - if !session.IsLogin() { - code = pb.ErrorCode_NoLogin - return - } +func (this *Api_Comp) Useitem_Check(session comm.IUserSession, req *pb.UseItemReq) (result map[string]interface{}, code pb.ErrorCode) { + return } //使用道具 -func (this *Api_Comp) Useitem(ctx context.Context, session comm.IUserSession, req *pb.UseItemReq) (err error) { +func (this *Api_Comp) Useitem(session comm.IUserSession, agrs map[string]interface{}, req *pb.UseItemReq) (err error) { var ( code pb.ErrorCode ) defer func() { session.SendMsg(string(this.module.GetType()), UseItemResp, code, &pb.UseItemResp{}) }() - if code = this.Useitem_Check(ctx, session, req); code != pb.ErrorCode_Success { - return - } + return } diff --git a/services/comp_gateroute.go b/services/comp_gateroute.go index a44c50692..bc6bf11a8 100644 --- a/services/comp_gateroute.go +++ b/services/comp_gateroute.go @@ -2,7 +2,6 @@ package services import ( "context" - "fmt" "go_dreamfactory/comm" "go_dreamfactory/pb" "reflect" @@ -11,6 +10,7 @@ import ( "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/core/cbase" + "go_dreamfactory/lego/sys/event" "go_dreamfactory/lego/sys/log" "github.com/golang/protobuf/proto" @@ -51,6 +51,7 @@ func (this *SComp_GateRouteComp) GetName() core.S_Comps { func (this *SComp_GateRouteComp) Init(service core.IService, comp core.IServiceComp, options core.ICompOptions) (err error) { err = this.ServiceCompBase.Init(service, comp, options) this.service = service.(base.IRPCXService) + this.msgcheck = make(map[string]*msghandle) this.msghandles = make(map[string]*msghandle) return err } @@ -59,15 +60,17 @@ func (this *SComp_GateRouteComp) Init(service core.IService, comp core.IServiceC func (this *SComp_GateRouteComp) Start() (err error) { this.service.RegisterFunctionName(string(comm.Rpc_GatewayRoute), this.ReceiveMsg) //注册网关路由接收接口 err = this.ServiceCompBase.Start() - for k, v := range this.msghandles { - if v1, ok := this.msgcheck[k]; !ok { - err = fmt.Errorf("注册用户消息处理函数:%s 没有实现参数校验接口", k) - return - } else if v.msgType != v1.msgType { - err = fmt.Errorf("注册用户消息处理函数:%s 实现参数校验接口不一致 请检查代码!", k) - return + event.RegisterGO(core.Event_ServiceStartEnd, func() { + for k, v := range this.msghandles { + if v1, ok := this.msgcheck[k]; !ok { + log.Panicf("注册用户消息处理函数:%s 没有实现参数校验接口", k) + return + } else if v.msgType != v1.msgType { + log.Panicf("注册用户消息处理函数:%s 实现参数校验接口不一致 请检查代码!", k) + return + } } - } + }) return } @@ -123,8 +126,15 @@ func (this *SComp_GateRouteComp) ReceiveMsg(ctx context.Context, args *pb.AgentM log.Errorf("UserMessage:%s Unmarshal err:%v", args.Method, err) return err } - msghandle.fn.Func.Call([]reflect.Value{msgcheck.rcvr, reflect.ValueOf(ctx), reflect.ValueOf(session), reflect.ValueOf(msg)}) - msghandle.fn.Func.Call([]reflect.Value{msghandle.rcvr, reflect.ValueOf(ctx), reflect.ValueOf(session), reflect.ValueOf(msg)}) + returnValues := msgcheck.fn.Func.Call([]reflect.Value{msgcheck.rcvr, reflect.ValueOf(ctx), reflect.ValueOf(session), reflect.ValueOf(msg)}) + // The return value for the method is an error. + code := returnValues[1].Int() + if pb.ErrorCode(code) != pb.ErrorCode_Success { + log.Errorf("HandleUserMsg:%s msg:%v code:%d", args.Method, msg, code) + return nil + } + // result := .Interface().(map[string]interface{}) + msghandle.fn.Func.Call([]reflect.Value{msghandle.rcvr, reflect.ValueOf(session), reflect.ValueOf(returnValues[0]), reflect.ValueOf(msg)}) } else { reply.Code = pb.ErrorCode_ReqParameterError // reply.Msg = pb.GetErrorCodeMsg(pb.ErrorCode_ReqParameterError)