base.go
1.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package controllers
import (
"github.com/astaxie/beego/validation"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/log"
"github.com/tiptok/gocomm/pkg/mybeego"
)
type BaseController struct {
mybeego.BaseController
}
//Valid valid struct
func (this *BaseController) Valid(obj interface{}) (result bool, msg *mybeego.Message) {
/*校验*/
var err error
valid := validation.Validation{}
result, err = valid.Valid(obj)
if err != nil {
msg = mybeego.NewMessage(1)
return
}
if !result {
for _, err := range valid.Errors {
log.Error(err.Key, err.Message)
}
msg = mybeego.NewMessage(2)
return
}
return
}
//GenMessage genarate a response message
func (this *BaseController) GenMessage(rsp interface{}, err error) *mybeego.Message {
var msg *mybeego.Message
if err == nil {
msg = mybeego.NewMessage(0)
msg.Data = rsp
return msg
}
if e, ok := err.(common.Error); ok {
msg = mybeego.NewMessage(e.Code)
msg.Data = rsp
return msg
}
msg = mybeego.NewMessage(1)
return msg
}