-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
83 lines (71 loc) · 1.43 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
package sgin
import (
"fmt"
"github.com/spf13/cast"
)
type Response struct {
Event string `json:"event"`
Status int `json:"status"`
Code int `json:"code"`
Count int `json:"count"`
Message string `json:"msg"`
Data any `json:"data"`
}
// SetStatus 设置 status, code
func (r *Response) SetStatus(status any, code ...any) *Response {
newcode := 0
st := cast.ToInt(status)
if code != nil {
newcode = cast.ToInt(code[0])
if r != nil {
r.Code = newcode
}
}
if r == nil {
return &Response{Status: st, Code: newcode}
}
r.Status = st
return r
}
// SetCode 设置 code
func (r *Response) SetCode(code any) *Response {
if r == nil {
return &Response{Code: cast.ToInt(code)}
}
r.Code = cast.ToInt(code)
return r
}
// SetEvent 设置事件
func (r *Response) SetEvent(event string) *Response {
if r == nil {
return &Response{Message: event}
}
r.Event = event
return r
}
// SetMessage 设置消息
func (r *Response) SetMessage(msg any) *Response {
if r == nil {
return &Response{Message: fmt.Sprint(msg)}
}
r.Message = fmt.Sprint(msg)
return r
}
// SetData 设置数据
func (r *Response) SetData(data any) *Response {
if r == nil {
return &Response{Data: data, Status: 1}
}
r.Data = data
r.Status = 1
return r
}
// SetFailData 设置失败数据
func (r *Response) SetFailData(data any) *Response {
if r == nil {
return &Response{Data: data}
}
r.Data = data
r.Status = 0
return r
}