-
Notifications
You must be signed in to change notification settings - Fork 151
/
response.go
64 lines (52 loc) · 1.5 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
package utils
import (
"fmt"
)
type Response struct {
Head map[string]string `json:"head"`
Body interface{} `json:"body"`
}
type ArrayBodyStruct struct {
CurrentPage int `json:"current_page"`
TotalPages int `json:"total_pages"`
PerPage int `json:"per_page"`
NextPage int `json:"next_page"`
PreviousPage int `json:"previous_page"`
Data interface{} `json:"data"`
}
type ArrayDataResponse struct {
Head map[string]string `json:"head"`
Body interface{} `json:"body"`
}
var (
SuccessResponse = Response{Head: map[string]string{"code": "1000", "msg": "Success."}}
ArrayResponse = ArrayDataResponse{Head: map[string]string{"code": "1000", "msg": "Success."}}
)
func BuildError(code string) Response {
return Response{Head: map[string]string{"code": code}}
}
func (errorResponse Response) Error() string {
return fmt.Sprintf("code: %s; msg: %s", errorResponse.Head["code"], errorResponse.Head["msg"])
}
func (arrayResponse *ArrayDataResponse) Init(data interface{}, page, count, per_page int) {
total_page := count / per_page
if (count % per_page) != 0 {
total_page += 1
}
nextPage := page + 1
if nextPage > total_page {
nextPage = total_page
}
previousPage := page - 1
if previousPage < 1 {
previousPage = 1
}
body := ArrayBodyStruct{}
body.Data = data
body.CurrentPage = page
body.TotalPages = total_page
body.PerPage = per_page
body.NextPage = nextPage
body.PreviousPage = previousPage
arrayResponse.Body = body
}