上传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 {
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 {

View File

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

View File

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

View File

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

View File

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

View File

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