From f47005cc185a0272d7af4911ab24ee7eddf25429 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 13 Jul 2022 18:20:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=BC=96=E8=A7=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego/sys/codec/factory/factory_map.go | 32 +++++++++++++++++++-------- lego/sys/codec/options.go | 2 ++ modules/gm/api.go | 1 + modules/gm/api_createnotify.go | 29 ++++++++++++------------ modules/gm/gm_test.http | 21 ++++++++++++++++++ 5 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 modules/gm/gm_test.http diff --git a/lego/sys/codec/factory/factory_map.go b/lego/sys/codec/factory/factory_map.go index 8562b28d1..6288609ab 100644 --- a/lego/sys/codec/factory/factory_map.go +++ b/lego/sys/codec/factory/factory_map.go @@ -108,22 +108,36 @@ func (this *mapEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *mapEncoder) EncodeToMapJson(ptr unsafe.Pointer) (ret map[string]string, err error) { ret = make(map[string]string) + var ( + k, v string + ) + keystream := this.codec.BorrowStream() elemstream := this.codec.BorrowStream() iter := this.mapType.UnsafeIterate(ptr) for i := 0; iter.HasNext(); i++ { key, elem := iter.UnsafeNext() - this.keyEncoder.Encode(key, keystream) - if keystream.Error() != nil && keystream.Error() != io.EOF { - err = keystream.Error() - return + if this.keyEncoder.GetType() != reflect.String { + this.keyEncoder.Encode(key, keystream) + if keystream.Error() != nil && keystream.Error() != io.EOF { + err = keystream.Error() + return + } + k = BytesToString(keystream.Buffer()) + } else { + k = *((*string)(key)) } - this.elemEncoder.Encode(elem, elemstream) - if elemstream.Error() != nil && elemstream.Error() != io.EOF { - err = elemstream.Error() - return + if this.elemEncoder.GetType() != reflect.String { + this.elemEncoder.Encode(elem, elemstream) + if elemstream.Error() != nil && elemstream.Error() != io.EOF { + err = elemstream.Error() + return + } + v = BytesToString(elemstream.Buffer()) + } else { + v = *((*string)(elem)) } - ret[BytesToString(keystream.Buffer())] = BytesToString(elemstream.Buffer()) + ret[k] = v keystream.Reset(512) elemstream.Reset(512) } diff --git a/lego/sys/codec/options.go b/lego/sys/codec/options.go index 28b5cd723..86567c3ff 100644 --- a/lego/sys/codec/options.go +++ b/lego/sys/codec/options.go @@ -9,6 +9,7 @@ import ( func newOptions(config map[string]interface{}, opts ...core.Option) core.Options { options := core.Options{ IndentionStep: 2, + TagKey: "json", } if config != nil { mapstructure.Decode(config, &options) @@ -25,6 +26,7 @@ func newOptions(config map[string]interface{}, opts ...core.Option) core.Options func newOptionsByOption(opts ...core.Option) core.Options { options := core.Options{ IndentionStep: 2, + TagKey: "json", } for _, o := range opts { o(&options) diff --git a/modules/gm/api.go b/modules/gm/api.go index 5ee8a6734..6e140a486 100644 --- a/modules/gm/api.go +++ b/modules/gm/api.go @@ -24,5 +24,6 @@ func (this *Api_Comp) Init(service core.IService, module core.IModule, comp core this.gin, err = gin.NewSys(gin.SetListenPort(this.options.Port)) this.gin.POST("/register", this.Register) this.gin.GET("/serverlist", this.ServerList) + this.gin.GET("/createnotify", this.CreateNotify) return } diff --git a/modules/gm/api_createnotify.go b/modules/gm/api_createnotify.go index b98a73a05..ba66d53ea 100644 --- a/modules/gm/api_createnotify.go +++ b/modules/gm/api_createnotify.go @@ -5,7 +5,6 @@ import ( "go_dreamfactory/lego/sys/gin/engine" "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" - "net/http" ) type CreateNotifyReq struct { @@ -16,31 +15,31 @@ type CreateNotifyReq struct { //服务列表 func (this *Api_Comp) CreateNotify(c *engine.Context) { req := &CreateNotifyReq{} - c.BindJSON(&req) - defer log.Debugf("CreateNotify:%+v", req) + err := c.BindJSON(&req) + log.Debugf("CreateNotify:%+v err:%v", req, err) var ( - code pb.ErrorCode - msg string - data interface{} - err error + // code pb.ErrorCode + // msg string + // data interface{} + // err error ) - c.JSON(http.StatusOK, &Respond{Code: code, Message: msg, Data: data}) + // defer c.JSON(http.StatusOK, &Respond{Code: code, Message: msg, Data: data}) if sign := gin.ParamSign(this.options.Key, map[string]interface{}{"Title": req.Title, "Ctime": req.Ctime, "Rtime": req.Rtime}); sign != req.Sign { log.Errorf("LoginByCaptchaReq SignError sgin:%s", sign) - code = pb.ErrorCode_SignError - msg = pb.GetErrorCodeMsg(code) + // code = pb.ErrorCode_SignError + // msg = pb.GetErrorCodeMsg(code) return } if len(req.Title) == 0 { - code = pb.ErrorCode_ReqParameterError - msg = pb.GetErrorCodeMsg(code) + // code = pb.ErrorCode_ReqParameterError + // msg = pb.GetErrorCodeMsg(code) return } if err = this.module.modelNotify.CreateSystemNotify(&req.DBSystemNotify); err != nil { log.Errorf("LoginByCaptchaReq CreateSystemNotify err:%v", err) - code = pb.ErrorCode_DBError - msg = pb.GetErrorCodeMsg(code) + // code = pb.ErrorCode_DBError + // msg = pb.GetErrorCodeMsg(code) return } - msg = pb.GetErrorCodeMsg(code) + // msg = pb.GetErrorCodeMsg(code) } diff --git a/modules/gm/gm_test.http b/modules/gm/gm_test.http new file mode 100644 index 000000000..c3ad62105 --- /dev/null +++ b/modules/gm/gm_test.http @@ -0,0 +1,21 @@ +package gm_test + +### +GET http://127.0.0.1:8000/createnotify HTTP/2.0 +Content-Type:application/json + +{ + "title": "游戏公告", + "content":"hello", + "sign": "f5c3fddfe9002563082f61838154c890", +} + +### 注册账号测试 +POST http://127.0.0.1:8000/register HTTP/1.1 +Content-Type:application/json + +{ + "register": "liwei2", + "content":0, + "sign": "f5c3fddfe9002563082f61838154c890", +} \ No newline at end of file