Commit b3d7971992cff398296f5663ff41786afd683106

Authored by zhangqijia
1 parent 58e37bfe

fix: 返回值如果code=0则成功,返回data

如果code!=0则失败,返回message
Showing 2 changed files with 19 additions and 14 deletions   Show diff stats
cmd/httpserver/http.go
@@ -21,7 +21,7 @@ func NewAccountServer(version string, port ...string) *AccountServer { @@ -21,7 +21,7 @@ func NewAccountServer(version string, port ...string) *AccountServer {
21 return &AccountServer{IHttp: components.NewHttpServer(version, port...)} 21 return &AccountServer{IHttp: components.NewHttpServer(version, port...)}
22 } 22 }
23 23
24 -func (s *AccountServer) Init() error { 24 +func (s *AccountServer) Init() error {
25 //mgo init 25 //mgo init
26 err := db.ConnectMongo(common.GlobalConf.AccountConf.MongoConf) 26 err := db.ConnectMongo(common.GlobalConf.AccountConf.MongoConf)
27 27
@@ -40,7 +40,7 @@ func (s *AccountServer) Init() error { @@ -40,7 +40,7 @@ func (s *AccountServer) Init() error {
40 return nil 40 return nil
41 } 41 }
42 42
43 -func (s *AccountServer) Start() error { 43 +func (s *AccountServer) Start() error {
44 if err := s.Init(); err != nil { 44 if err := s.Init(); err != nil {
45 return err 45 return err
46 } 46 }
@@ -60,10 +60,10 @@ func main() { @@ -60,10 +60,10 @@ func main() {
60 }() 60 }()
61 61
62 select { 62 select {
63 - case e := <- err:  
64 - logger.Error("game server error: %v", e) 63 + case e := <-err:
  64 + logger.Error("http server error: %v", e)
65 case <-stopChan: 65 case <-stopChan:
66 - logger.Debug("game stop") 66 + logger.Debug("http stop")
67 web.Stop() 67 web.Stop()
68 } 68 }
69 -}  
70 \ No newline at end of file 69 \ No newline at end of file
  70 +}
common/components/http.go
@@ -10,11 +10,11 @@ import ( @@ -10,11 +10,11 @@ import (
10 type HttpServer struct { 10 type HttpServer struct {
11 IHttp 11 IHttp
12 version string 12 version string
13 - port []string 13 + port []string
14 Handler interface{} 14 Handler interface{}
15 } 15 }
16 16
17 -func Pong (c *gin.Context) { 17 +func Pong(c *gin.Context) {
18 c.JSON(200, gin.H{ 18 c.JSON(200, gin.H{
19 "message": "pong", 19 "message": "pong",
20 }) 20 })
@@ -24,22 +24,27 @@ func NewHttpServer(version string, port ...string) *HttpServer { @@ -24,22 +24,27 @@ func NewHttpServer(version string, port ...string) *HttpServer {
24 return &HttpServer{version: version, port: port} 24 return &HttpServer{version: version, port: port}
25 } 25 }
26 26
27 -func GetRoutePath(objName, objFunc string) string { 27 +func GetRoutePath(objName, objFunc string) string {
28 return strings.ToLower(objName + "/" + objFunc) 28 return strings.ToLower(objName + "/" + objFunc)
29 } 29 }
30 30
31 -func (h *HttpServer)HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { 31 +func (h *HttpServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc {
32 return func(c *gin.Context) { 32 return func(c *gin.Context) {
33 v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(c)}) 33 v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(c)})
34 if len(v) != 2 { 34 if len(v) != 2 {
35 - c.JSON(http.StatusNotFound, gin.H{"code": -100, "data": ""}) 35 + c.JSON(http.StatusNotFound, gin.H{"code": -100, "message": "request param len is error", "data": ""})
36 return 36 return
37 } 37 }
38 - c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "data": v[1].Interface()}) 38 + code := v[0].Int()
  39 + if code == 0 {
  40 + c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "message": "success", "data": v[1].Interface()})
  41 + } else {
  42 + c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "message": v[1].Interface()})
  43 + }
39 } 44 }
40 } 45 }
41 46
42 -func (h *HttpServer) BindHandler(handler interface{}) { 47 +func (h *HttpServer) BindHandler(handler interface{}) {
43 h.Handler = handler 48 h.Handler = handler
44 } 49 }
45 50
@@ -59,4 +64,4 @@ func (h *HttpServer) Start() error { @@ -59,4 +64,4 @@ func (h *HttpServer) Start() error {
59 r.POST(GetRoutePath(h.version, method.Name), h.HandlerFuncObj(method.Func, val)) 64 r.POST(GetRoutePath(h.version, method.Name), h.HandlerFuncObj(method.Func, val))
60 } 65 }
61 return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") 66 return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
62 -}  
63 \ No newline at end of file 67 \ No newline at end of file
  68 +}