-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
58 lines (51 loc) · 1.18 KB
/
app.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
package app
import (
"github.com/gin-gonic/gin"
"github.com/hayuzi/blogserver/global"
"github.com/hayuzi/blogserver/pkg/errcode"
"net/http"
)
type Response struct {
Ctx *gin.Context
}
func NewResponse(ctx *gin.Context) *Response {
return &Response{
Ctx: ctx,
}
}
func (r *Response) ToResponse(data interface{}) {
if data == nil {
data = make(map[string]interface{})
}
r.Ctx.JSON(http.StatusOK, gin.H{
"code": errcode.Success.Code(),
"msg": "",
"data": data,
})
}
func (r *Response) ToResponseList(list interface{}, totalRows int64) {
data := make(map[string]interface{})
data["lists"] = list
data["pageNum"] = GetPageNum(r.Ctx)
data["pageSize"] = GetPageSize(r.Ctx)
data["total"] = totalRows
r.Ctx.JSON(http.StatusOK, gin.H{
"code": errcode.Success.Code(),
"msg": "",
"data": data,
})
}
func (r *Response) ToResponseError(err *errcode.Error) {
global.Logger.WithCaller(2).Errorf(r.Ctx, "%v", err.Error())
response := gin.H{
"code": err.Code(),
"msg": err.Msg(),
"data": make(map[string]interface{}),
}
details := err.Details()
if len(details) > 0 {
response["details"] = details
response["msg"] = err.Msg()
}
r.Ctx.JSON(err.StatusCode(), response)
}