-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
57 lines (48 loc) · 1.27 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
package main
import (
"fmt"
"net/http"
"github.com/pquerna/ffjson/ffjson"
)
// NewResponse 创建一个新的response对象
func NewResponse(w http.ResponseWriter, r *http.Request) Response {
r.ParseForm()
return Response{
w: w,
r: r,
}
}
// ReturnSuccess 返回正确的信息
func (r *Response) ReturnSuccess(data interface{}) {
r.Return(data, 200)
}
// ReturnError 返回错误信息
func (r *Response) ReturnError(statuscode, code int, errMsg string) {
r.Return(map[string]interface{}{"errcode": code, "errmsg": errMsg}, statuscode)
}
// Return 向客户返回回数据
func (r *Response) Return(data interface{}, code int) {
//jsonp := r.IsJSONP()
rs, err := ffjson.Marshal(data)
if err != nil {
code = 500
rs = []byte(fmt.Sprintf(`{"errcode":500, "errmsg":"%s"}`, err.Error()))
}
r.w.WriteHeader(code)
r.w.Header().Add("Content-Type", "application/json")
r.w.Write(rs)
//if jsonp == "" {
// r.w.Header().Add("Content-Type", "application/json")
// r.w.Write(rs)
//} else {
// r.w.Header().Add("Content-Type", "application/javascript")
// r.w.Write([]byte(fmt.Sprintf(`%s(%s)`, jsonp, rs)))
//}
}
// IsJSONP 是否为jsonp 请求
func (r *Response) IsJSONP() string {
if r.r.Form.Get("callback") != "" {
return r.r.Form.Get("callback")
}
return ""
}