-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
87 lines (70 loc) · 1.3 KB
/
response.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package core
import (
"encoding/json"
"net/http"
)
var ResponseDataNil = struct {
}{}
type Response struct {
Code string `json:"retcode"`
Msg string `json:"desc"`
Data interface{} `json:"biz"`
}
func (res Response) GetBytes() []byte {
b, _ := json.Marshal(res)
return b
}
func (res Response) GetString() string {
b, _ := json.Marshal(res)
return string(b[:])
}
func getDefaultErrorResponse(err IError) Response {
var data interface{}
if err.Error() == "操作失败" {
data = err.GetDetail()
}
if data == nil {
data = ResponseDataNil
}
//if _,ok := data.(string); ok {
// data = ResponseDataNil
//}
return Response{
err.GetCode(), err.GetMsg(), data,
}
}
func getResponseWithCode(code string, data ...interface{}) Response {
if code == "" {
code = SuccessCode
}
msg := DefaultCodeMapping.GetCodeInfo(code)
var r = Response{
Code: code,
Msg: msg,
Data: nil,
}
if data == nil {
return r
}
l := len(data)
if l > 0 {
if l == 1 {
r.Data = data[0]
} else {
r.Data = data
}
}
return r
}
func ResponseStr(c *Context, str string) {
rStr(c, str)
}
func ResponseJson(c *Context, data interface{}) {
rJson(c, data)
}
func rStr(c *Context, str string) {
c.String(http.StatusOK, str)
}
func rJson(c *Context, data interface{}) {
c.JSON(http.StatusOK, data)
}