-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.go
140 lines (124 loc) · 3.28 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
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
package tsx
import (
"encoding/json"
"fmt"
"github.com/ftlynx/tsx/request"
"net/http"
)
const RequestIdKey = "request_id" //用来记录每次请求ID的key
type CodeValue struct {
HttpCode int
Msg string
}
var CodeMap map[int]CodeValue //定义code
// 设置code的消息
func SetCodeMsg(code int, msg CodeValue) {
if msg.HttpCode==0 {
msg.HttpCode=http.StatusInternalServerError
}
CodeMap[code] = msg
}
type Response struct {
HttpCode int `json:"-"`
ErrCode int `json:"code"` //自定义code
Success bool `json:"success"`
ErrMsg string `json:"-"`
Message string `json:"message,omitempty"` //错误提示
Data interface{} `json:"data,omitempty"`
RequestId string `json:"request_id"` //请求id
}
func (r *Response) Error() string {
return r.ErrMsg
}
func NewOk(data ...interface{}) *Response {
var d interface{}
if len(data) > 0 {
d = data[0]
}
return &Response{
HttpCode: http.StatusOK,
ErrCode: http.StatusOK,
Success: true,
Message: "ok",
ErrMsg: "",
Data: d,
RequestId: "",
}
}
func NewResponse(err error) *Response {
if exception, ok := err.(*Exception); ok {
message := "未定义的code"
httpCode := http.StatusInternalServerError
// 如果errCode是0,则设置成500
if exception.ErrCode == 0 {
exception.ErrCode = http.StatusInternalServerError
}
if _, ok := CodeMap[exception.ErrCode]; ok {
message = CodeMap[exception.ErrCode].Msg
httpCode = CodeMap[exception.ErrCode].HttpCode
}
return &Response{
HttpCode: httpCode,
ErrCode: exception.ErrCode,
ErrMsg: exception.Error(),
Message: message,
Data: nil,
RequestId: "",
}
}
return &Response{
HttpCode: http.StatusInternalServerError,
ErrCode: http.StatusInternalServerError,
Message: "未知错误",
ErrMsg: err.Error(),
}
}
// PageData 数据分页数据
type PageData struct {
PageSize int `json:"page_size"` // 一页多少条
PageIndex int `json:"page_index"` // 当前第几页
SizeCount int64 `json:"total"` // 总共多少条数据
IndexCount int `json:"index_count"` // 总共多少页面
List interface{} `json:"data"` // 页面数据
Success bool `json:"success"`
}
func (p *PageData) Format(sizeCount int64, pageSize int, list interface{}) {
//除尽就不+1
complement := 1
if sizeCount%int64(pageSize) == 0 {
complement = 0
}
p.SizeCount = sizeCount
p.IndexCount = int(sizeCount/int64(pageSize)) + complement
p.List = list
}
// 分页参数
type QueryPaging struct {
PageSize int `form:"pageSize"` //每页多少条
PageIndex int `form:"current"` //第几页
}
func (p *QueryPaging) Convert() (limit int, offset int) {
p.defaultValue()
limit = p.PageSize
offset = p.PageSize * (p.PageIndex - 1)
return
}
func (p *QueryPaging) defaultValue() {
if p.PageIndex == 0 {
p.PageIndex = 1
}
if p.PageSize == 0 {
p.PageSize = 10
}
}
//返回解析
func ResponseParse(result *Response) request.ResultParse {
return func(resp request.HttpResponse) error {
if resp.Code != http.StatusOK {
result.HttpCode = resp.Code
_ = json.Unmarshal(resp.Content, result)
return fmt.Errorf("opsx response http code not 200. is %d", resp.Code)
}
return json.Unmarshal(resp.Content, result)
}
}