上传api 接口检测代码

This commit is contained in:
liwei1dao 2022-06-15 10:48:01 +08:00
parent 76cdd23c91
commit 9e8a8104d8
6 changed files with 87 additions and 56 deletions

View File

@ -53,6 +53,7 @@ const (
type ISC_GateRouteComp interface { type ISC_GateRouteComp interface {
core.IServiceComp core.IServiceComp
RegisterRoute(methodName string, comp reflect.Value, msg reflect.Type, fn reflect.Method) 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 { type Autogenerated struct {

View File

@ -1,9 +1,9 @@
package modules package modules
import ( import (
"context"
"fmt" "fmt"
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/pb"
"reflect" "reflect"
"strings" "strings"
"unicode" "unicode"
@ -18,8 +18,10 @@ import (
模块网关组件的基类实现 模块接收用户的消息请求都需要通过装备继承此组件的api组件来实现 模块网关组件的基类实现 模块接收用户的消息请求都需要通过装备继承此组件的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 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() 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++ { for m := 0; m < typ.NumMethod(); m++ {
method := typ.Method(m) method := typ.Method(m)
this.reflectionRouteHandle(method) 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 mtype := method.Type
mname := method.Name mname := method.Name
if method.PkgPath != "" { if method.PkgPath != "" {
return false return
} }
if mtype.NumIn() != 4 { if mtype.NumIn() != 4 {
return false return
} }
ctxType := mtype.In(1) ctxType := mtype.In(1)
if !ctxType.Implements(typeOfContext) { if !ctxType.Implements(typeOfSession) {
return false return
} }
argType := mtype.In(2) argType := mtype.In(2)
if !argType.Implements(typeOfSession) { if !argType.Implements(typeOfMapStringIntface) {
return false return
} }
replyType := mtype.In(3) replyType := mtype.In(3)
if replyType.Kind() != reflect.Ptr { if replyType.Kind() != reflect.Ptr {
return false return
} }
if !this.isExportedOrBuiltinType(replyType) { if !this.isExportedOrBuiltinType(replyType) {
return false return
} }
if mtype.NumOut() != 1 { if mtype.NumOut() != 1 {
return false return
} }
if returnType := mtype.Out(0); returnType != typeOfError { 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) 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 { func (this *MComp_GateComp) isExportedOrBuiltinType(t reflect.Type) bool {

View File

@ -1,7 +1,6 @@
package pack package pack
import ( import (
"context"
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb" "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) { func (this *Api_Comp) Getlist_Check(session comm.IUserSession, req *pb.GetlistReq) (result map[string]interface{}, code pb.ErrorCode) {
if !session.IsLogin() {
code = pb.ErrorCode_NoLogin
return
}
return 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 ( var (
code pb.ErrorCode code pb.ErrorCode
items []*pb.DB_UserItemData 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 { if items, err = this.module.cache_comp.Pack_QueryUserPack(session.GetUserId()); err != nil {
log.Errorf("QueryUserPackReq err:%v", err) log.Errorf("QueryUserPackReq err:%v", err)
code = pb.ErrorCode_CacheReadError code = pb.ErrorCode_CacheReadError

View File

@ -1,30 +1,23 @@
package pack package pack
import ( import (
"context"
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/pb" "go_dreamfactory/pb"
) )
//参数校验 //参数校验
func (this *Api_Comp) SellItem_Check(ctx context.Context, session comm.IUserSession, req *pb.SellItemReq) (code pb.ErrorCode) { func (this *Api_Comp) SellItem_Check(session comm.IUserSession, req *pb.SellItemReq) (result map[string]interface{}, code pb.ErrorCode) {
if !session.IsLogin() {
code = pb.ErrorCode_NoLogin
return
}
return 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 ( var (
code pb.ErrorCode code pb.ErrorCode
) )
defer func() { defer func() {
session.SendMsg(string(this.module.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
}
return return
} }

View File

@ -1,30 +1,24 @@
package pack package pack
import ( import (
"context"
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/pb" "go_dreamfactory/pb"
) )
//参数校验 //参数校验
func (this *Api_Comp) Useitem_Check(ctx context.Context, session comm.IUserSession, req *pb.UseItemReq) (code pb.ErrorCode) { func (this *Api_Comp) Useitem_Check(session comm.IUserSession, req *pb.UseItemReq) (result map[string]interface{}, code pb.ErrorCode) {
if !session.IsLogin() {
code = pb.ErrorCode_NoLogin
return
}
return 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 ( var (
code pb.ErrorCode code pb.ErrorCode
) )
defer func() { defer func() {
session.SendMsg(string(this.module.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
}
return return
} }

View File

@ -2,7 +2,6 @@ package services
import ( import (
"context" "context"
"fmt"
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"reflect" "reflect"
@ -11,6 +10,7 @@ import (
"go_dreamfactory/lego/base" "go_dreamfactory/lego/base"
"go_dreamfactory/lego/core" "go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase" "go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/event"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
"github.com/golang/protobuf/proto" "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) { func (this *SComp_GateRouteComp) Init(service core.IService, comp core.IServiceComp, options core.ICompOptions) (err error) {
err = this.ServiceCompBase.Init(service, comp, options) err = this.ServiceCompBase.Init(service, comp, options)
this.service = service.(base.IRPCXService) this.service = service.(base.IRPCXService)
this.msgcheck = make(map[string]*msghandle)
this.msghandles = make(map[string]*msghandle) this.msghandles = make(map[string]*msghandle)
return err return err
} }
@ -59,15 +60,17 @@ func (this *SComp_GateRouteComp) Init(service core.IService, comp core.IServiceC
func (this *SComp_GateRouteComp) Start() (err error) { func (this *SComp_GateRouteComp) Start() (err error) {
this.service.RegisterFunctionName(string(comm.Rpc_GatewayRoute), this.ReceiveMsg) //注册网关路由接收接口 this.service.RegisterFunctionName(string(comm.Rpc_GatewayRoute), this.ReceiveMsg) //注册网关路由接收接口
err = this.ServiceCompBase.Start() err = this.ServiceCompBase.Start()
for k, v := range this.msghandles { event.RegisterGO(core.Event_ServiceStartEnd, func() {
if v1, ok := this.msgcheck[k]; !ok { for k, v := range this.msghandles {
err = fmt.Errorf("注册用户消息处理函数:%s 没有实现参数校验接口", k) if v1, ok := this.msgcheck[k]; !ok {
return log.Panicf("注册用户消息处理函数:%s 没有实现参数校验接口", k)
} else if v.msgType != v1.msgType { return
err = fmt.Errorf("注册用户消息处理函数:%s 实现参数校验接口不一致 请检查代码!", k) } else if v.msgType != v1.msgType {
return log.Panicf("注册用户消息处理函数:%s 实现参数校验接口不一致 请检查代码!", k)
return
}
} }
} })
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) log.Errorf("UserMessage:%s Unmarshal err:%v", args.Method, err)
return err return err
} }
msghandle.fn.Func.Call([]reflect.Value{msgcheck.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)})
msghandle.fn.Func.Call([]reflect.Value{msghandle.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 { } else {
reply.Code = pb.ErrorCode_ReqParameterError reply.Code = pb.ErrorCode_ReqParameterError
// reply.Msg = pb.GetErrorCodeMsg(pb.ErrorCode_ReqParameterError) // reply.Msg = pb.GetErrorCodeMsg(pb.ErrorCode_ReqParameterError)