-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
116 lines (92 loc) · 1.97 KB
/
service.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
// Copyright 2022 YuWenYu All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package services
import (
"github.com/spf13/cast"
"github.com/yuw-pot/pot/routes"
"math"
)
const (
defaultPage int = 1
defaultSize int = 10
)
type (
Services struct {
}
)
func New() *Services {
return &Services {
}
}
func (srv *Services) GeTPath(service, controller, action string) (interface{}, error) {
return routes.GeTPath(service, controller, action)
}
func (srv *Services) Paginator(page int, pageNums int, pageSize ...int) (paginator map[string]interface{}) {
var (
nums int = pageNums
size int
prePage int
sufPage int
)
if len(pageSize) > 0 {
size = pageSize[0]
} else {
size = defaultSize
}
var totalPage int = int(math.Ceil(float64(nums) / float64(size)))
if page > totalPage {
page = totalPage
}
if page <= 0 {
page = 1
}
var pages []int
switch {
case page >= totalPage-5 && totalPage > 5:
start := totalPage-5+1
prePage = page-1
sufPage = int(math.Min(float64(totalPage), float64(page+1)))
pages = make([]int, 5)
for i, _ := range pages {
pages[i] = start+i
}
case page >= 3 && totalPage > 5:
start := page-3+1
pages = make([]int, 5)
prePage = page-3
for i, _ := range pages {
pages[i] = start + i
}
prePage = page-1
sufPage = page+1
default:
pages = make([]int, int(math.Min(5, float64(totalPage))))
for i, _ := range pages {
pages[i] = i + 1
}
prePage = int(math.Max(float64(1), float64(page-1)))
sufPage = page+1
}
paginator = map[string]interface{}{
"pages": pages,
"total": totalPage,
"cur_page": page,
"pre_page": prePage,
"suf_page": sufPage,
}
return
}
func (srv *Services) PaginatorParams(strPage string, strSize string) (page int, size int) {
if strPage != "" {
page = cast.ToInt(strPage)
} else {
page = defaultPage
}
if strSize != "" {
size = cast.ToInt(strSize)
} else {
size = defaultSize
}
return
}