作者 唐旭辉

错误处理跳转

1 ## 目录 1 ## 目录
2 -* [代码声明](#代码声明)  
3 - * [命名规范](#命名规范)  
4 - * [项目约定](#项目约定)  
5 - * [注意事项](#注意事项) 2 +
  3 +- [代码声明](#代码声明)
  4 + - [命名规范](#命名规范)
  5 + - [项目约定](#项目约定)
  6 + - [注意事项](#注意事项)
6 7
7 ### 代码声明 8 ### 代码声明
8 9
@@ -66,6 +67,7 @@ func Foo(){} @@ -66,6 +67,7 @@ func Foo(){}
66 ``` 67 ```
67 68
68 3.目录结构 69 3.目录结构
  70 +
69 ``` 71 ```
70 * conf //配置 72 * conf //配置
71 * dev.conf 73 * dev.conf
@@ -85,7 +87,7 @@ func Foo(){} @@ -85,7 +87,7 @@ func Foo(){}
85 * static //静态数据 87 * static //静态数据
86 * views //视图 88 * views //视图
87 89
88 -* main.go 90 +* main.go
89 * Dockerfile 91 * Dockerfile
90 * README.md 92 * README.md
91 * *.sh //脚本 93 * *.sh //脚本
@@ -93,24 +95,24 @@ func Foo(){} @@ -93,24 +95,24 @@ func Foo(){}
93 95
94 #### 项目约定 96 #### 项目约定
95 97
96 -1. 入参跟返回值都要定义一个协议结构 protocol.XxxRequest *protocol.XxxResponse,方便扩展 98 +1. 入参跟返回值都要定义一个协议结构 protocol.XxxRequest \*protocol.XxxResponse,方便扩展
97 2. 函数方法的变量都定义在函数的头几行 var( ... ) 99 2. 函数方法的变量都定义在函数的头几行 var( ... )
98 3. 服务调用尽量封装成接口,方便测试扩展 比如:ISmsServe{ Send() error } 100 3. 服务调用尽量封装成接口,方便测试扩展 比如:ISmsServe{ Send() error }
99 -4. 公用的基础代码库,需要做一下封装放在 [gocomm](http://gitlab.fjmaimaimai.com/mmm-go/gocomm) ,方便在其他项目中调用  
100 -4. 可以使用代码生成固定格式, [项目地址](http://gitlab.fjmaimaimai.com/mmm-go/gencode) 脚本:.\gencode.exe new -c Auth -m Login 101 +4. 公用的基础代码库,需要做一下封装放在 [gocomm](http://gitlab.fjmaimaimai.com/mmm-go/gocomm) ,方便在其他项目中调用
  102 +5. 可以使用代码生成固定格式, [项目地址](http://gitlab.fjmaimaimai.com/mmm-go/gencode) 脚本:.\gencode.exe new -c Auth -m Login
101 103
102 ``` 104 ```
103 package pkg 105 package pkg
104 //Login 106 //Login
105 func(this *AuthController)Login(){ 107 func(this *AuthController)Login(){
106 - var msg *mybeego.Message 108 + var msg *protocol.ResponseMessage
107 defer func(){ 109 defer func(){
108 this.Resp(msg) 110 this.Resp(msg)
109 }() 111 }()
110 var request *protocol.LoginRequest 112 var request *protocol.LoginRequest
111 if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ 113 if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
112 log.Error(err) 114 log.Error(err)
113 - msg = mybeego.NewMessage(1) 115 + msg = protocol.ReturnResponse(1)
114 return 116 return
115 } 117 }
116 if b,m :=this.Valid(request);!b{ 118 if b,m :=this.Valid(request);!b{
@@ -129,20 +131,18 @@ type LoginResponse struct { @@ -129,20 +131,18 @@ type LoginResponse struct {
129 131
130 func Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error){ 132 func Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error){
131 var ( 133 var (
132 - 134 +
133 ) 135 )
134 rsp =&protocol.LoginResponse{} 136 rsp =&protocol.LoginResponse{}
135 return 137 return
136 } 138 }
137 ``` 139 ```
138 140
139 -  
140 #### 注意事项 141 #### 注意事项
141 142
142 -1. 启动一个groutine(eg:go func(){}()),需要在函数内进行recover,不然groutine里面panic,会导致外部程序一起崩溃掉;  
143 -2. 当接受者是map, chan, func, 不要使用指针传递,因为它们本身就是引用类型 143 +1. 启动一个 groutine(eg:go func(){}()),需要在函数内进行 recover,不然 groutine 里面 panic,会导致外部程序一起崩溃掉;
  144 +2. 当接受者是 map, chan, func, 不要使用指针传递,因为它们本身就是引用类型
144 3. 当接受者类型是一个结构体并且很庞大,或者是一个大数组,建议使用指针传递来提高性能,其他场景使用值传递即可 145 3. 当接受者类型是一个结构体并且很庞大,或者是一个大数组,建议使用指针传递来提高性能,其他场景使用值传递即可
145 -4. 当接受者是一个结构体,并且包含了sync.Mutex或者类似的用于同步的成员。必须使用指针传递,避免成员拷贝 146 +4. 当接受者是一个结构体,并且包含了 sync.Mutex 或者类似的用于同步的成员。必须使用指针传递,避免成员拷贝
146 5. 当函数内部需要修改接受者,必须使用指针传递 147 5. 当函数内部需要修改接受者,必须使用指针传递
147 -6. 声明空的slice应该使用下面的格式: var t []string 不要使用 t := []string{} ,前者声明了一个nil slice而后者是一个长度为0的非nil的slice。  
148 - 148 +6. 声明空的 slice 应该使用下面的格式: var t []string 不要使用 t := []string{} ,前者声明了一个 nil slice 而后者是一个长度为 0 的非 nil 的 slice。
1 [dev] 1 [dev]
2 #数据库相关 2 #数据库相关
3 -data_source = "root:123456@tcp(192.168.100.102:3306)/ability_display?loc=Local" 3 +# data_source = "root:123456@tcp(192.168.100.102:3306)/ability_display?loc=Local"
4 #data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display" 4 #data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display"
5 - 5 +data_source = "root:root@tcp(localhost:3306)/ability_display?loc=Local"
6 6
7 #redis相关配置 7 #redis相关配置
8 redis_add_port = "192.168.100.102:6379" 8 redis_add_port = "192.168.100.102:6379"
@@ -47,42 +47,42 @@ func init() { @@ -47,42 +47,42 @@ func init() {
47 var DefaultController *BaseController = &BaseController{} 47 var DefaultController *BaseController = &BaseController{}
48 48
49 //Valid valid struct 49 //Valid valid struct
50 -func (this *BaseController) Valid(obj interface{}) (result bool, msg *mybeego.Message) { 50 +func (this *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
51 /*校验*/ 51 /*校验*/
52 var err error 52 var err error
53 valid := validation.Validation{} 53 valid := validation.Validation{}
54 result, err = valid.Valid(obj) 54 result, err = valid.Valid(obj)
55 if err != nil { 55 if err != nil {
56 - msg = mybeego.NewMessage(1) 56 + msg = protocol.BadRequestParam(1)
57 return 57 return
58 } 58 }
59 if !result { 59 if !result {
60 for _, err := range valid.Errors { 60 for _, err := range valid.Errors {
61 log.Error(err.Key, err.Message) 61 log.Error(err.Key, err.Message)
62 } 62 }
63 - msg = mybeego.NewMessage(2) 63 + msg = protocol.BadRequestParam(2)
64 return 64 return
65 } 65 }
66 return 66 return
67 } 67 }
68 68
69 //GenMessage genarate a response message 69 //GenMessage genarate a response message
70 -func (this *BaseController) GenMessage(rsp interface{}, err error) *mybeego.Message {  
71 - var msg *mybeego.Message  
72 - if err == nil {  
73 - msg = mybeego.NewMessage(0)  
74 - msg.Data = rsp  
75 - return msg  
76 - }  
77 - //log.Error(err)  
78 - if e, ok := err.(common.Error); ok {  
79 - msg = mybeego.NewMessage(e.Code)  
80 - msg.Data = rsp  
81 - return msg  
82 - }  
83 - msg = mybeego.NewMessage(1)  
84 - return msg  
85 -} 70 +// func (this *BaseController) GenMessage(rsp interface{}, err error) *protocol.ResponseMessage {
  71 +// var msg *protocol.ResponseMessage
  72 +// if err == nil {
  73 +// msg = protocol.ReturnResponse(0)
  74 +// msg.Data = rsp
  75 +// return msg
  76 +// }
  77 +// //log.Error(err)
  78 +// if e, ok := err.(common.Error); ok {
  79 +// msg = protocol.ReturnResponse(e.Code)
  80 +// msg.Data = rsp
  81 +// return msg
  82 +// }
  83 +// msg = protocol.ReturnResponse(1)
  84 +// return msg
  85 +// }
86 86
87 //获取请求头信息 87 //获取请求头信息
88 func GetRequestHeader(ctx *context.Context) *protocol.RequestHeader { 88 func GetRequestHeader(ctx *context.Context) *protocol.RequestHeader {
@@ -149,7 +149,7 @@ func CheckSign(ctx *context.Context) (result bool) { @@ -149,7 +149,7 @@ func CheckSign(ctx *context.Context) (result bool) {
149 sha256.Write([]byte(sign)) 149 sha256.Write([]byte(sign))
150 signHex = hex.EncodeToString(sha256.Sum(nil)) 150 signHex = hex.EncodeToString(sha256.Sum(nil))
151 if strings.Compare(signHex, h.Sign) != 0 { 151 if strings.Compare(signHex, h.Sign) != 0 {
152 - msg := mybeego.NewMessage(113) 152 + msg := protocol.BadRequestParam(113)
153 log.Error(fmt.Sprintf("%v req:%v resp:%v %v", ctx.Request.RequestURI, common.AssertJson(h), common.AssertJson(msg), signHex)) 153 log.Error(fmt.Sprintf("%v req:%v resp:%v %v", ctx.Request.RequestURI, common.AssertJson(h), common.AssertJson(msg), signHex))
154 ctx.Output.JSON(msg, false, false) 154 ctx.Output.JSON(msg, false, false)
155 result = false 155 result = false
@@ -161,7 +161,7 @@ func CheckSign(ctx *context.Context) (result bool) { @@ -161,7 +161,7 @@ func CheckSign(ctx *context.Context) (result bool) {
161 //检查access_token 161 //检查access_token
162 func CheckToken(ctx *context.Context) (result bool) { 162 func CheckToken(ctx *context.Context) (result bool) {
163 var ( 163 var (
164 - msg *mybeego.Message 164 + msg *protocol.ResponseMessage
165 ) 165 )
166 result = true 166 result = true
167 defer func() { 167 defer func() {
@@ -172,7 +172,7 @@ func CheckToken(ctx *context.Context) (result bool) { @@ -172,7 +172,7 @@ func CheckToken(ctx *context.Context) (result bool) {
172 }() 172 }()
173 token := ctx.Input.Header("x-mmm-accesstoken") 173 token := ctx.Input.Header("x-mmm-accesstoken")
174 if rsp, err := auth.CheckToken(&protocol.CheckTokenRequest{Token: token}); err != nil || rsp.UserInfo == nil { 174 if rsp, err := auth.CheckToken(&protocol.CheckTokenRequest{Token: token}); err != nil || rsp.UserInfo == nil {
175 - msg = DefaultController.GenMessage(rsp, err) 175 + msg = protocol.NewReturnResponse(rsp, err)
176 log.Error(fmt.Sprintf("%v req:%v resp:%v", ctx.Request.RequestURI, token, common.AssertJson(msg))) 176 log.Error(fmt.Sprintf("%v req:%v resp:%v", ctx.Request.RequestURI, token, common.AssertJson(msg)))
177 return 177 return
178 } else { 178 } else {
@@ -187,7 +187,7 @@ func CheckToken(ctx *context.Context) (result bool) { @@ -187,7 +187,7 @@ func CheckToken(ctx *context.Context) (result bool) {
187 //检查Uuid 187 //检查Uuid
188 func CheckUuid(ctx *context.Context) (result bool) { 188 func CheckUuid(ctx *context.Context) (result bool) {
189 var ( 189 var (
190 - msg *mybeego.Message 190 + msg *protocol.ResponseMessage
191 ) 191 )
192 result = true 192 result = true
193 defer func() { 193 defer func() {
@@ -197,7 +197,7 @@ func CheckUuid(ctx *context.Context) (result bool) { @@ -197,7 +197,7 @@ func CheckUuid(ctx *context.Context) (result bool) {
197 } 197 }
198 }() 198 }()
199 uuid := ctx.Input.Header("x-mmm-uuid") 199 uuid := ctx.Input.Header("x-mmm-uuid")
200 - msg = DefaultController.GenMessage(auth.CheckUuid(&protocol.CheckUuidRequest{Uuid: uuid})) 200 + msg = protocol.NewReturnResponse(auth.CheckUuid(&protocol.CheckUuidRequest{Uuid: uuid}))
201 if msg != nil { 201 if msg != nil {
202 log.Error(fmt.Sprintf("%v req:%v resp:%v", ctx.Request.RequestURI, uuid, common.AssertJson(msg))) 202 log.Error(fmt.Sprintf("%v req:%v resp:%v", ctx.Request.RequestURI, uuid, common.AssertJson(msg)))
203 } 203 }
@@ -8,7 +8,6 @@ import ( @@ -8,7 +8,6 @@ import (
8 s_auth "ability/services/auth" 8 s_auth "ability/services/auth"
9 9
10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
11 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"  
12 ) 11 )
13 12
14 var ( 13 var (
@@ -22,99 +21,99 @@ type AuthController struct { @@ -22,99 +21,99 @@ type AuthController struct {
22 // Login 21 // Login
23 // @router /login [post] 22 // @router /login [post]
24 func (this *AuthController) Login() { 23 func (this *AuthController) Login() {
25 - var msg *mybeego.Message 24 + var msg *protocol.ResponseMessage
26 defer func() { 25 defer func() {
27 this.Resp(msg) 26 this.Resp(msg)
28 }() 27 }()
29 var request *protocol.LoginRequest 28 var request *protocol.LoginRequest
30 if err := json.Unmarshal(this.ByteBody, &request); err != nil { 29 if err := json.Unmarshal(this.ByteBody, &request); err != nil {
31 log.Error(err) 30 log.Error(err)
32 - msg = mybeego.NewMessage(1) 31 + msg = protocol.BadRequestParam(1)
33 return 32 return
34 } 33 }
35 if b, m := this.Valid(request); !b { 34 if b, m := this.Valid(request); !b {
36 msg = m 35 msg = m
37 return 36 return
38 } 37 }
39 - msg = this.GenMessage(auth.Login(request)) 38 + msg = protocol.NewReturnResponse(auth.Login(request))
40 } 39 }
41 40
42 //SmsCode 41 //SmsCode
43 // @router /smsCode [post] 42 // @router /smsCode [post]
44 func (this *AuthController) SmsCode() { 43 func (this *AuthController) SmsCode() {
45 - var msg *mybeego.Message 44 + var msg *protocol.ResponseMessage
46 defer func() { 45 defer func() {
47 this.Resp(msg) 46 this.Resp(msg)
48 }() 47 }()
49 var request *protocol.SmsCodeRequest 48 var request *protocol.SmsCodeRequest
50 if err := json.Unmarshal(this.ByteBody, &request); err != nil { 49 if err := json.Unmarshal(this.ByteBody, &request); err != nil {
51 log.Error(err) 50 log.Error(err)
52 - msg = mybeego.NewMessage(1) 51 + msg = protocol.BadRequestParam(1)
53 return 52 return
54 } 53 }
55 if b, m := this.Valid(request); !b { 54 if b, m := this.Valid(request); !b {
56 msg = m 55 msg = m
57 return 56 return
58 } 57 }
59 - msg = this.GenMessage(auth.SmsCode(request)) 58 + msg = protocol.NewReturnResponse(auth.SmsCode(request))
60 } 59 }
61 60
62 //UpdateDevice 61 //UpdateDevice
63 // @router /updateDevice [post] 62 // @router /updateDevice [post]
64 func (this *AuthController) UpdateDevice() { 63 func (this *AuthController) UpdateDevice() {
65 - var msg *mybeego.Message 64 + var msg *protocol.ResponseMessage
66 defer func() { 65 defer func() {
67 this.Resp(msg) 66 this.Resp(msg)
68 }() 67 }()
69 var request *protocol.UpdateDeviceRequest 68 var request *protocol.UpdateDeviceRequest
70 if err := json.Unmarshal(this.ByteBody, &request); err != nil { 69 if err := json.Unmarshal(this.ByteBody, &request); err != nil {
71 log.Error(err) 70 log.Error(err)
72 - msg = mybeego.NewMessage(1) 71 + msg = protocol.BadRequestParam(1)
73 return 72 return
74 } 73 }
75 if b, m := this.Valid(request); !b { 74 if b, m := this.Valid(request); !b {
76 msg = m 75 msg = m
77 return 76 return
78 } 77 }
79 - msg = this.GenMessage(auth.UpdateDevice(request)) 78 + msg = protocol.NewReturnResponse(auth.UpdateDevice(request))
80 } 79 }
81 80
82 //AccessToken 81 //AccessToken
83 // @router /accessToken [post] 82 // @router /accessToken [post]
84 func (this *AuthController) AccessToken() { 83 func (this *AuthController) AccessToken() {
85 - var msg *mybeego.Message 84 + var msg *protocol.ResponseMessage
86 defer func() { 85 defer func() {
87 this.Resp(msg) 86 this.Resp(msg)
88 }() 87 }()
89 var request *protocol.AccessTokenRequest 88 var request *protocol.AccessTokenRequest
90 if err := json.Unmarshal(this.ByteBody, &request); err != nil { 89 if err := json.Unmarshal(this.ByteBody, &request); err != nil {
91 log.Error(err) 90 log.Error(err)
92 - msg = mybeego.NewMessage(1) 91 + msg = protocol.BadRequestParam(1)
93 return 92 return
94 } 93 }
95 if b, m := this.Valid(request); !b { 94 if b, m := this.Valid(request); !b {
96 msg = m 95 msg = m
97 return 96 return
98 } 97 }
99 - msg = this.GenMessage(auth.AccessToken(request)) 98 + msg = protocol.NewReturnResponse(auth.AccessToken(request))
100 } 99 }
101 100
102 //RefreshToken 101 //RefreshToken
103 // @router /refreshToken [post] 102 // @router /refreshToken [post]
104 func (this *AuthController) RefreshToken() { 103 func (this *AuthController) RefreshToken() {
105 - var msg *mybeego.Message 104 + var msg *protocol.ResponseMessage
106 defer func() { 105 defer func() {
107 this.Resp(msg) 106 this.Resp(msg)
108 }() 107 }()
109 var request *protocol.RefreshTokenRequest 108 var request *protocol.RefreshTokenRequest
110 if err := json.Unmarshal(this.ByteBody, &request); err != nil { 109 if err := json.Unmarshal(this.ByteBody, &request); err != nil {
111 log.Error(err) 110 log.Error(err)
112 - msg = mybeego.NewMessage(1) 111 + msg = protocol.BadRequestParam(1)
113 return 112 return
114 } 113 }
115 if b, m := this.Valid(request); !b { 114 if b, m := this.Valid(request); !b {
116 msg = m 115 msg = m
117 return 116 return
118 } 117 }
119 - msg = this.GenMessage(auth.RefreshToken(request)) 118 + msg = protocol.NewReturnResponse(auth.RefreshToken(request))
120 } 119 }
@@ -6,7 +6,6 @@ import ( @@ -6,7 +6,6 @@ import (
6 "ability/services/upload" 6 "ability/services/upload"
7 7
8 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 8 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
9 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"  
10 ) 9 )
11 10
12 type UploadController struct { 11 type UploadController struct {
@@ -17,7 +16,7 @@ type UploadController struct { @@ -17,7 +16,7 @@ type UploadController struct {
17 // @router /image [post] 16 // @router /image [post]
18 func (this *UploadController) Image() { 17 func (this *UploadController) Image() {
19 var ( 18 var (
20 - msg *mybeego.Message 19 + msg *protocol.ResponseMessage
21 err error 20 err error
22 ) 21 )
23 defer func() { 22 defer func() {
@@ -26,7 +25,7 @@ func (this *UploadController) Image() { @@ -26,7 +25,7 @@ func (this *UploadController) Image() {
26 var request = &protocol.FileRequest{} 25 var request = &protocol.FileRequest{}
27 //if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ 26 //if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
28 // log.Error(err) 27 // log.Error(err)
29 - // msg = mybeego.NewMessage(1) 28 + // msg = protocol.ReturnResponse(1)
30 // return 29 // return
31 //} 30 //}
32 //if b,m :=this.Valid(request);!b{ 31 //if b,m :=this.Valid(request);!b{
@@ -37,14 +36,14 @@ func (this *UploadController) Image() { @@ -37,14 +36,14 @@ func (this *UploadController) Image() {
37 log.Error(err) 36 log.Error(err)
38 return 37 return
39 } 38 }
40 - msg = this.GenMessage(upload.Image(request)) 39 + msg = protocol.NewReturnResponse(upload.Image(request))
41 } 40 }
42 41
43 // Image 42 // Image
44 // @router /voice [post] 43 // @router /voice [post]
45 func (this *UploadController) Voice() { 44 func (this *UploadController) Voice() {
46 var ( 45 var (
47 - msg *mybeego.Message 46 + msg *protocol.ResponseMessage
48 err error 47 err error
49 ) 48 )
50 defer func() { 49 defer func() {
@@ -53,7 +52,7 @@ func (this *UploadController) Voice() { @@ -53,7 +52,7 @@ func (this *UploadController) Voice() {
53 var request = &protocol.FileRequest{} 52 var request = &protocol.FileRequest{}
54 //if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ 53 //if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
55 // log.Error(err) 54 // log.Error(err)
56 - // msg = mybeego.NewMessage(1) 55 + // msg = protocol.ReturnResponse(1)
57 // return 56 // return
58 //} 57 //}
59 //if b,m :=this.Valid(request);!b{ 58 //if b,m :=this.Valid(request);!b{
@@ -64,5 +63,5 @@ func (this *UploadController) Voice() { @@ -64,5 +63,5 @@ func (this *UploadController) Voice() {
64 log.Error(err) 63 log.Error(err)
65 return 64 return
66 } 65 }
67 - msg = this.GenMessage(upload.Voice(request)) 66 + msg = protocol.NewReturnResponse(upload.Voice(request))
68 } 67 }
1 package v1 1 package v1
2 2
3 import ( 3 import (
  4 + "ability/protocol"
  5 +
4 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" 6 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
5 ) 7 )
6 8
@@ -23,17 +25,17 @@ func (this *UserController) URLMapping() { @@ -23,17 +25,17 @@ func (this *UserController) URLMapping() {
23 // @Param body body string true "body" 25 // @Param body body string true "body"
24 // @router /v1/user/login [post] 26 // @router /v1/user/login [post]
25 func (this *UserController) Login() { 27 func (this *UserController) Login() {
26 - var msg *mybeego.Message 28 + var msg *protocol.ResponseMessage
27 defer func() { 29 defer func() {
28 this.Resp(msg) 30 this.Resp(msg)
29 }() 31 }()
30 - msg = mybeego.NewErrMessage(0, "login success") 32 + msg = protocol.NewReturnResponse("login success", nil)
31 } 33 }
32 34
33 func (this *UserController) Logout() { 35 func (this *UserController) Logout() {
34 - var msg *mybeego.Message 36 + var msg *protocol.ResponseMessage
35 defer func() { 37 defer func() {
36 this.Resp(msg) 38 this.Resp(msg)
37 }() 39 }()
38 - msg = mybeego.NewErrMessage(0, "logout success") 40 + msg = protocol.NewReturnResponse("logout success", nil)
39 } 41 }
@@ -8,7 +8,6 @@ import ( @@ -8,7 +8,6 @@ import (
8 "ability/services/version" 8 "ability/services/version"
9 9
10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
11 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"  
12 ) 11 )
13 12
14 type VersionController struct { 13 type VersionController struct {
@@ -17,19 +16,19 @@ type VersionController struct { @@ -17,19 +16,19 @@ type VersionController struct {
17 16
18 //GetLatestVersionInfo 17 //GetLatestVersionInfo
19 func (this *VersionController) GetLatestVersionInfo() { 18 func (this *VersionController) GetLatestVersionInfo() {
20 - var msg *mybeego.Message 19 + var msg *protocol.ResponseMessage
21 defer func() { 20 defer func() {
22 this.Resp(msg) 21 this.Resp(msg)
23 }() 22 }()
24 var request *protocol.GetLatestVersionInfoRequest 23 var request *protocol.GetLatestVersionInfoRequest
25 if err := json.Unmarshal(this.ByteBody, &request); err != nil { 24 if err := json.Unmarshal(this.ByteBody, &request); err != nil {
26 log.Error(err) 25 log.Error(err)
27 - msg = mybeego.NewMessage(1) 26 + msg = protocol.BadRequestParam(1)
28 return 27 return
29 } 28 }
30 if b, m := this.Valid(request); !b { 29 if b, m := this.Valid(request); !b {
31 msg = m 30 msg = m
32 return 31 return
33 } 32 }
34 - msg = this.GenMessage(version.GetLatestVersionInfo(request)) 33 + msg = protocol.NewReturnResponse(version.GetLatestVersionInfo(request))
35 } 34 }
@@ -23,7 +23,7 @@ var errmessge ErrorMap = map[int]string{ @@ -23,7 +23,7 @@ var errmessge ErrorMap = map[int]string{
23 func SearchErr(code int) ErrorCode { 23 func SearchErr(code int) ErrorCode {
24 return errmessge.Search(code) 24 return errmessge.Search(code)
25 } 25 }
26 -func ReturnResponse(data interface{}, eRR error) *ResponseMessage { 26 +func NewReturnResponse(data interface{}, eRR error) *ResponseMessage {
27 var msg *ResponseMessage 27 var msg *ResponseMessage
28 if eRR == nil { 28 if eRR == nil {
29 msg = NewMesage(0) 29 msg = NewMesage(0)
@@ -37,6 +37,10 @@ func ReturnResponse(data interface{}, eRR error) *ResponseMessage { @@ -37,6 +37,10 @@ func ReturnResponse(data interface{}, eRR error) *ResponseMessage {
37 return NewMesage(1) 37 return NewMesage(1)
38 } 38 }
39 39
  40 +func BadRequestParam(code int) *ResponseMessage {
  41 + return NewMesage(code)
  42 +}
  43 +
40 func InitMessageCode() { 44 func InitMessageCode() {
41 // messages := []struct { 45 // messages := []struct {
42 // Code int 46 // Code int
@@ -60,7 +60,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login @@ -60,7 +60,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login
60 user, err = UserRepository.GetUsersByMobile(request.Phone) 60 user, err = UserRepository.GetUsersByMobile(request.Phone)
61 if err != nil { 61 if err != nil {
62 log.Error(err) 62 log.Error(err)
63 - err = common.NewError(2020, err) //账号不存在 63 + err = protocol.NewErrWithMessage(2020, err) //账号不存在
64 return 64 return
65 } 65 }
66 switch request.GrantType { 66 switch request.GrantType {
@@ -68,7 +68,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login @@ -68,7 +68,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login
68 if strings.Compare(user.Password, request.PassWord) == 0 { 68 if strings.Compare(user.Password, request.PassWord) == 0 {
69 goto Success 69 goto Success
70 } else { 70 } else {
71 - err = common.NewError(2021, err) //登录密码错误 71 + err = protocol.NewErrWithMessage(2021, err) //登录密码错误
72 return 72 return
73 } 73 }
74 break 74 break
@@ -169,15 +169,15 @@ func (s *AuthService) Login1(request *protocol.LoginRequest) (rsp *protocol.Logi @@ -169,15 +169,15 @@ func (s *AuthService) Login1(request *protocol.LoginRequest) (rsp *protocol.Logi
169 Name: useridentity.Uname, 169 Name: useridentity.Uname,
170 Icon: useridentity.Icon, 170 Icon: useridentity.Icon,
171 } 171 }
172 - rsp, err := s_im.ParseUserUpdateUinfo(v) 172 + imRsp, err := s_im.ParseUserUpdateUinfo(v)
173 if err != nil { 173 if err != nil {
174 log.Error("s_im.ParseUserUpdateUinfo err:", err) 174 log.Error("s_im.ParseUserUpdateUinfo err:", err)
175 } 175 }
176 if err == nil { 176 if err == nil {
177 - if rsp.Code == 200 { 177 + if imRsp.Code == 200 {
178 break 178 break
179 } else { 179 } else {
180 - log.Error("s_im.ParseUserUpdateUinfo response code:", rsp.Code) 180 + log.Error("s_im.ParseUserUpdateUinfo response code:", imRsp.Code)
181 } 181 }
182 } 182 }
183 } 183 }
@@ -198,12 +198,12 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr @@ -198,12 +198,12 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
198 ) 198 )
199 _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret) 199 _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
200 if err != nil { 200 if err != nil {
201 - err = common.NewError(101, err) 201 + err = protocol.NewErrWithMessage(101, err)
202 return 202 return
203 } 203 }
204 userInfo, err = UserRepository.GetUserInfoByAuthCode(request.AuthCode) 204 userInfo, err = UserRepository.GetUserInfoByAuthCode(request.AuthCode)
205 if err != nil { 205 if err != nil {
206 - err = common.NewError(4139, err) 206 + err = protocol.NewErrWithMessage(4139, err)
207 return 207 return
208 } 208 }
209 userInfo.AccessToken = uid.NewV1().StringNoDash() 209 userInfo.AccessToken = uid.NewV1().StringNoDash()
@@ -228,12 +228,12 @@ func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp * @@ -228,12 +228,12 @@ func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *
228 ) 228 )
229 _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret) 229 _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
230 if err != nil { 230 if err != nil {
231 - common.NewError(101, err) 231 + protocol.NewErrWithMessage(101, err)
232 return 232 return
233 } 233 }
234 userInfo, err = UserRepository.GetUserInfoByRefreshToken(request.RefreshToken) 234 userInfo, err = UserRepository.GetUserInfoByRefreshToken(request.RefreshToken)
235 if err != nil { 235 if err != nil {
236 - common.NewError(4139, err) 236 + protocol.NewErrWithMessage(4139, err)
237 return 237 return
238 } 238 }
239 userInfo.AccessToken = uid.NewV1().StringNoDash() 239 userInfo.AccessToken = uid.NewV1().StringNoDash()
@@ -267,7 +267,7 @@ func (s *AuthService) CheckToken(request *protocol.CheckTokenRequest) (rsp *prot @@ -267,7 +267,7 @@ func (s *AuthService) CheckToken(request *protocol.CheckTokenRequest) (rsp *prot
267 } 267 }
268 userInfo, err = models.GetUserInfoByToken(request.Token) 268 userInfo, err = models.GetUserInfoByToken(request.Token)
269 if err != nil { 269 if err != nil {
270 - err = common.NewError(4141, err) 270 + err = protocol.NewErrWithMessage(4141, err)
271 return 271 return
272 } 272 }
273 rsp = &protocol.CheckTokenResponse{ 273 rsp = &protocol.CheckTokenResponse{
@@ -6,7 +6,6 @@ import ( @@ -6,7 +6,6 @@ import (
6 "strings" 6 "strings"
7 "time" 7 "time"
8 8
9 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"  
10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 9 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
11 ) 10 )
12 11
@@ -69,11 +68,11 @@ func (o LoginByPassword) LoginAuth() (*UserIdentity, error) { @@ -69,11 +68,11 @@ func (o LoginByPassword) LoginAuth() (*UserIdentity, error) {
69 user, err := UserRepository.GetUserInfoByMobile(o.Username) 68 user, err := UserRepository.GetUserInfoByMobile(o.Username)
70 if err != nil { 69 if err != nil {
71 log.Error(err) 70 log.Error(err)
72 - o.err = common.NewError(2020, err) //账号不存在 71 + o.err = protocol.NewErrWithMessage(2020, err) //账号不存在
73 return nil, o.err 72 return nil, o.err
74 } 73 }
75 if strings.Compare(user.Passwd, o.Password) != 0 { 74 if strings.Compare(user.Passwd, o.Password) != 0 {
76 - o.err = common.NewError(2021, err) //登录密码错误 75 + o.err = protocol.NewErrWithMessage(2021, err) //登录密码错误
77 return nil, o.err 76 return nil, o.err
78 } 77 }
79 identity := &UserIdentity{ 78 identity := &UserIdentity{
@@ -91,7 +90,7 @@ func (o LoginBySms) LoginAuth() (*UserIdentity, error) { @@ -91,7 +90,7 @@ func (o LoginBySms) LoginAuth() (*UserIdentity, error) {
91 user, err := UserRepository.GetUserInfoByMobile(o.Phone) 90 user, err := UserRepository.GetUserInfoByMobile(o.Phone)
92 if err != nil { 91 if err != nil {
93 log.Error(err) 92 log.Error(err)
94 - o.err = common.NewError(2020, err) //账号不存在 93 + o.err = protocol.NewErrWithMessage(2020, err) //账号不存在
95 return nil, o.err 94 return nil, o.err
96 } 95 }
97 result, err := CheckSmsCode(o.Phone, o.Code, protocol.SmsLoginCode) 96 result, err := CheckSmsCode(o.Phone, o.Code, protocol.SmsLoginCode)
@@ -7,14 +7,14 @@ package user @@ -7,14 +7,14 @@ package user
7 // "ability/models" 7 // "ability/models"
8 //) 8 //)
9 // 9 //
10 -//func GetUserList(mobile string)*mybeego.Message{ 10 +//func GetUserList(mobile string)*protocol.ResponseMessage{
11 // u,err:=models.GetUserByMobile(mobile) 11 // u,err:=models.GetUserByMobile(mobile)
12 // if err!=nil{ 12 // if err!=nil{
13 // log.Error(err) 13 // log.Error(err)
14 // log.Error(mobile) 14 // log.Error(mobile)
15 // return mybeego.NewErrMessage(1) 15 // return mybeego.NewErrMessage(1)
16 // } 16 // }
17 -// msg :=mybeego.NewMessage(0) 17 +// msg :=protocol.ReturnResponse(0)
18 // msg.Data = u 18 // msg.Data = u
19 // return msg 19 // return msg
20 //} 20 //}
1 package websocket 1 package websocket
2 2
3 import ( 3 import (
  4 + "net/http"
  5 + "strconv"
  6 +
4 "github.com/gorilla/websocket" 7 "github.com/gorilla/websocket"
  8 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
5 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" 9 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
6 ws "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/websocket" 10 ws "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/websocket"
7 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"  
8 - "net/http"  
9 - "strconv"  
10 ) 11 )
11 12
12 var upgrader = websocket.Upgrader{} 13 var upgrader = websocket.Upgrader{}
@@ -20,10 +21,11 @@ func Upgrage(w http.ResponseWriter, r *http.Request) { @@ -20,10 +21,11 @@ func Upgrage(w http.ResponseWriter, r *http.Request) {
20 if err != nil { 21 if err != nil {
21 log.Fatal(err) 22 log.Fatal(err)
22 } 23 }
  24 +
23 wsConn := ws.NewWebsocketConnection(conn, requestHead, WsOnReceive) 25 wsConn := ws.NewWebsocketConnection(conn, requestHead, WsOnReceive)
24 wsConn.Serve() 26 wsConn.Serve()
25 } 27 }
26 28
27 func WsOnReceive(data []byte) *mybeego.Message { 29 func WsOnReceive(data []byte) *mybeego.Message {
28 return mybeego.NewMessage(0) 30 return mybeego.NewMessage(0)
29 -}  
  31 +}