-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagging.go
61 lines (50 loc) · 1.2 KB
/
pagging.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
package request
import (
"net/http"
"strconv"
)
const (
// DefaultPageSize 默认分页大小
DefaultPageSize = 20
// DefaultPageNumber 默认页号
DefaultPageNumber = 1
)
// NewPageRequestFromHTTP 从HTTP请求中加载分页请求
func NewPageRequestFromHTTP(req *http.Request) *PageRequest {
qs := req.URL.Query()
ps := qs.Get("page_size")
pn := qs.Get("page_number")
os := qs.Get("offset")
psUint64, _ := strconv.ParseUint(ps, 10, 64)
pnUint64, _ := strconv.ParseUint(pn, 10, 64)
osInt64, _ := strconv.ParseInt(os, 10, 64)
if psUint64 == 0 {
psUint64 = DefaultPageSize
}
if pnUint64 == 0 {
pnUint64 = DefaultPageNumber
}
return &PageRequest{
PageSize: psUint64,
PageNumber: pnUint64,
Offset: osInt64,
}
}
func NewDefaultPageRequest() *PageRequest {
return NewPageRequest(DefaultPageSize, DefaultPageNumber)
}
// NewPageRequest 实例化
func NewPageRequest(ps uint, pn uint) *PageRequest {
return &PageRequest{
PageSize: uint64(ps),
PageNumber: uint64(pn),
}
}
// GetOffset skip
// 如果传入了offset则使用传入的offset参数
func (p *PageRequest) ComputeOffset() int64 {
if p.Offset != 0 {
return p.Offset
}
return int64(p.PageSize * (p.PageNumber - 1))
}