@@ -0,0 +1,52 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const maxAccountPage = 10000
|
||||
|
||||
type accountPage struct {
|
||||
Current int
|
||||
Total int
|
||||
Count int64
|
||||
PrevURL string
|
||||
NextURL string
|
||||
}
|
||||
|
||||
func accountPageNum(c *gin.Context) (int, bool) {
|
||||
if c.Request.URL.Query().Has("page") {
|
||||
return 0, false
|
||||
}
|
||||
raw := strings.TrimSpace(c.Param("page"))
|
||||
if raw == "" {
|
||||
return 1, true
|
||||
}
|
||||
page, err := strconv.Atoi(raw)
|
||||
if err != nil || page < 1 || page > maxAccountPage {
|
||||
return 0, false
|
||||
}
|
||||
return page, true
|
||||
}
|
||||
|
||||
func makeAccountPage(path string, page, size int, count int64) accountPage {
|
||||
total := int((count + int64(size) - 1) / int64(size))
|
||||
p := accountPage{Current: page, Total: total, Count: count}
|
||||
link := func(n int) string {
|
||||
path = strings.TrimRight(path, "/")
|
||||
if n <= 1 {
|
||||
return path
|
||||
}
|
||||
return path + "/page/" + strconv.Itoa(n)
|
||||
}
|
||||
if page > 1 {
|
||||
p.PrevURL = link(page - 1)
|
||||
}
|
||||
if page < total {
|
||||
p.NextURL = link(page + 1)
|
||||
}
|
||||
return p
|
||||
}
|
||||
Reference in New Issue
Block a user