-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
91 lines (72 loc) · 1.67 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
88
89
90
91
package gintool
import (
"github.com/gin-gonic/gin"
"github.com/lucky-lee/gutil/gLog"
"net/http"
"time"
)
const (
MESS_SUCCESS = "success"
MESS_FAIL = "fail"
)
type Empty struct {
}
var successCode int = 200
//set success code
func SetSuccessCode(code int) {
successCode = code
}
//base bean
type JsonRetBase struct {
Code int `json:"code"`
Msg string `json:"msg"`
Timestamp int64 `json:"timestamp"`
}
//json return struct
type JsonRet struct {
JsonRetBase
Data interface{} `json:"data"`
}
//output json string
func Render(c *gin.Context, code int, msg string, obj interface{}) {
s := RetStruct(code, msg, obj)
gLog.Json("json struct", s)
c.JSON(http.StatusOK, s)
}
//output json string with message
func RenderMess(c *gin.Context, code int, mess string) {
Render(c, code, mess, nil)
}
//output json string with default message
func RenderData(c *gin.Context, code int, obj interface{}) {
Render(c, code, "", obj)
}
//output json string with suceess code message
func RenderSuccData(c *gin.Context, obj interface{}) {
Render(c, successCode, MESS_SUCCESS, obj)
}
//output json string with code and no message data
func RenderCode(c *gin.Context, code int) {
Render(c, code, "", nil)
}
//output json string with status
func RenderStatus(c *gin.Context, errCode int, status bool) {
if status {
RenderMess(c, successCode, MESS_SUCCESS)
} else {
RenderMess(c, errCode, MESS_FAIL)
}
}
//get ret struct
func RetStruct(code int, msg string, obj interface{}) JsonRet {
var bean JsonRet
bean.Code = code
bean.Msg = msg
bean.Timestamp = time.Now().UnixNano() / 1e6
if obj != nil {
bean.Data = obj
} else {
bean.Data = Empty{}
}
return bean
}