/
dd_response.go
167 lines (143 loc) · 3.52 KB
/
dd_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package gin
import (
"encoding/json"
"encoding/xml"
"fmt"
"html/template"
"net/http"
"net/url"
)
var _ = IResponse(&Context{})
// IResponse代表返回方法
type IResponse interface {
// Json输出
IJson(obj interface{}) IResponse
// Jsonp输出
IJsonp(obj interface{}) IResponse
//xml输出
IXml(obj interface{}) IResponse
// html输出
IHtml(template string, obj interface{}) IResponse
// string
IText(format string, values ...interface{}) IResponse
// 重定向
IRedirect(path string) IResponse
// header
ISetHeader(key string, val string) IResponse
// Cookie
ISetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse
// 设置状态码
ISetStatus(code int) IResponse
// 设置200状态
ISetOkStatus() IResponse
}
// Jsonp输出
func (c *Context) IJsonp(obj interface{}) IResponse {
// 获取请求参数callback
callbackFunc, _ := c.DefaultQueryString("callback", "callback_function")
c.ISetHeader("Content-Type", "application/javascript")
// 输出到前端页面的时候需要注意下进行字符过滤,否则有可能造成xss攻击
callback := template.JSEscapeString(callbackFunc)
// 输出函数名
_, err := c.Writer.Write([]byte(callback))
if err != nil {
return c
}
// 输出左括号
_, err = c.Writer.Write([]byte("("))
if err != nil {
return c
}
// 数据函数参数
ret, err := json.Marshal(obj)
if err != nil {
return c
}
_, err = c.Writer.Write(ret)
if err != nil {
return c
}
// 输出右括号
_, err = c.Writer.Write([]byte(")"))
if err != nil {
return c
}
return c
}
//xml输出
func (c *Context) IXml(obj interface{}) IResponse {
byt, err := xml.Marshal(obj)
if err != nil {
return c.ISetStatus(http.StatusInternalServerError)
}
c.Header("Content-Type", "application/html")
c.Writer.Write(byt)
return c
}
// html输出
func (c *Context) IHtml(file string, obj interface{}) IResponse {
// 读取模版文件,创建template实例
t, err := template.New("output").ParseFiles(file)
if err != nil {
return c
}
// 执行Execute方法将obj和模版进行结合
if err := t.Execute(c.Writer, obj); err != nil {
return c
}
c.ISetHeader("Content-Type", "application/html")
return c
}
// string
func (c *Context) IText(format string, values ...interface{}) IResponse {
out := fmt.Sprintf(format, values...)
c.ISetHeader("Content-Type", "application/text")
c.Writer.Write([]byte(out))
return c
}
// 重定向
func (c *Context) IRedirect(path string) IResponse {
http.Redirect(c.Writer, c.Request, path, http.StatusMovedPermanently)
return c
}
// header
func (c *Context) ISetHeader(key string, val string) IResponse {
c.Writer.Header().Add(key, val)
return c
}
// Cookie
func (c *Context) ISetCookie(key string, val string, maxAge int, path string, domain string, secure bool, httpOnly bool) IResponse {
if path == "" {
path = "/"
}
http.SetCookie(c.Writer, &http.Cookie{
Name: key,
Value: url.QueryEscape(val),
MaxAge: maxAge,
Path: path,
Domain: domain,
SameSite: 1,
Secure: secure,
HttpOnly: httpOnly,
})
return c
}
// 设置状态码
func (c *Context) ISetStatus(code int) IResponse {
c.Writer.WriteHeader(code)
return c
}
// 设置200状态
func (c *Context) ISetOkStatus() IResponse {
c.Writer.WriteHeader(http.StatusOK)
return c
}
func (c *Context) IJson(obj interface{}) IResponse {
byt, err := json.Marshal(obj)
if err != nil {
return c.ISetStatus(http.StatusInternalServerError)
}
c.ISetHeader("Content-Type", "application/json")
c.Writer.Write(byt)
return c
}