-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
88 lines (78 loc) · 2.02 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
package plover
import (
"github.com/unrolled/render"
"net/http"
)
type Response struct {
http.ResponseWriter
statusCode int
render *render.Render
}
func (response *Response) Reset(responser Response) {
response.render = responser.render
response.statusCode = response.statusCode
response.ResponseWriter = responser.ResponseWriter
}
func (response *Response) StatusCode(statusCode int) *Response {
response.statusCode = statusCode
return response
}
// 输出分页数据
// @param interface data
// @param int page
// @param int limit
// @param int total
func (response *Response) Page(data interface{}, page int, limit int, total int) {
response.render.JSON(response, response.statusCode, map[string]interface{}{
"code": 0,
"message": "Success",
"data": data,
"page": page,
"limit": limit,
"total": total,
})
return
}
// @title 输出Json内容
// @param code int
// @param message int
// @param interface data
func (response *Response) Xml(code int, message string, data interface{}) {
response.render.XML(response, response.statusCode, map[string]interface{}{
"code": code,
"message": message,
"data": data,
})
return
}
// @title 输出Json内容
// @param code int
// @param message int
// @param interface data
func (response *Response) Json(code int, message string, data interface{}) {
response.render.JSON(response, response.statusCode, map[string]interface{}{
"code": code,
"message": message,
"data": data,
})
return
}
// @title 输出Json数据
// @param data interface
func (response *Response) JsonData(data interface{}) {
response.render.JSON(response, response.statusCode, data)
return
}
// 渲染模板
// @param string template
// @param interface data
func (response *Response) View(template string, data interface{}) {
response.render.HTML(response, response.statusCode, template, data)
return
}
// 输出string数据
// @param string data
func (response *Response) String(data string) {
response.render.Text(response, response.statusCode, data)
return
}