/
resp.go
87 lines (73 loc) · 1.69 KB
/
resp.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
package apiresp
import (
"strconv"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
)
const NbReqStatusHeader = "Nb-Req-Status"
type ApiResp struct {
Code int `json:"code"`
Msg string `json:"msg,omitempty"`
Data interface{} `json:"data,omitempty"`
}
func NewResp(d interface{}, msgs ...string) *ApiResp {
var msg string
if len(msgs) > 0 {
msg = msgs[0]
}
return &ApiResp{
Code: 200,
Msg: msg,
Data: d,
}
}
func NewErr(msg string, ds ...interface{}) *ApiResp {
var d interface{}
if len(ds) > 0 {
d = ds[0]
}
return &ApiResp{
Code: 207,
Msg: msg,
Data: d,
}
}
func NewDetail(code int, msg string, ds ...interface{}) *ApiResp {
var d interface{}
if len(ds) > 0 {
d = ds[0]
}
return &ApiResp{
Code: code,
Msg: msg,
Data: d,
}
}
func (self *ApiResp) IsSuccess() bool {
return self.Code == 200
}
// BeegoServeJSON 返回 json 内容
// @deprecated
func (self *ApiResp) BeegoServeJSON(c beego.Controller) {
c.Ctx.Output.SetStatus(self.Code)
c.Data["json"] = self
c.ServeJSON()
c.StopRun()
}
// ReturnJSON 返回 json 内容
// httpCode 默认为200, 业务请求状态码在 response header 中的 "nb-req-status" 体现
func (self *ApiResp) ReturnJSON(c beego.Controller, httpCode ...int) {
httpCode = append(httpCode, 200)
c.Ctx.Output.SetStatus(httpCode[0])
c.Ctx.Output.Header(NbReqStatusHeader, strconv.Itoa(self.Code))
c.Data["json"] = self
c.ServeJSON()
c.StopRun()
}
func (self *ApiResp) JSON(ctx *context.Context, httpCode ...int) {
httpCode = append(httpCode, 200)
ctx.Output.SetStatus(httpCode[0])
ctx.Output.Header(NbReqStatusHeader, strconv.Itoa(self.Code))
ctx.Output.JSON(self, false, false)
panic(beego.ErrAbort)
}