-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse.go
63 lines (53 loc) · 1.39 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
package common
import (
"github.com/gin-gonic/gin"
"net/http"
)
type (
PageResult struct {
List interface{} `json:"list"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
Response struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
}
)
const (
SUCCESS = 200
ERROR = 1000
ErrorRequestParameter = 1001
ErrorTokenGenerate = 1002
ErrorUserNameExist = 1003
)
func Result(code int, data interface{}, msg string, c *gin.Context) {
c.JSON(http.StatusOK, Response{
Code: code,
Data: data,
Msg: msg,
})
}
func Ok(c *gin.Context) {
Result(SUCCESS, map[string]interface{}{}, "operation success", c)
}
func OkWithMessage(message string, c *gin.Context) {
Result(SUCCESS, map[string]interface{}{}, message, c)
}
func OkWithData(data interface{}, c *gin.Context) {
Result(SUCCESS, data, "operation success", c)
}
func OkWithDetailed(data interface{}, message string, c *gin.Context) {
Result(SUCCESS, data, message, c)
}
func FailWithMessage(code int, message string, c *gin.Context) {
Result(code, map[string]interface{}{}, message, c)
}
func FailWithCode(code int, c *gin.Context) {
Result(code, map[string]interface{}{}, "operation failed", c)
}
func FailWithDetailed(code int, data interface{}, message string, c *gin.Context) {
Result(code, data, message, c)
}