Merge branch 'main' into alpha
This commit is contained in:
62
common/page_info.go
Normal file
62
common/page_info.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PageInfo struct {
|
||||||
|
Page int `json:"page"` // page num 页码
|
||||||
|
PageSize int `json:"page_size"` // page size 页大小
|
||||||
|
StartTimestamp int64 `json:"start_timestamp"` // 秒级
|
||||||
|
EndTimestamp int64 `json:"end_timestamp"` // 秒级
|
||||||
|
|
||||||
|
Total int `json:"total"` // 总条数,后设置
|
||||||
|
Items any `json:"items"` // 数据,后设置
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageInfo) GetStartIdx() int {
|
||||||
|
return (p.Page - 1) * p.PageSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageInfo) GetEndIdx() int {
|
||||||
|
return p.Page * p.PageSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageInfo) GetPageSize() int {
|
||||||
|
return p.PageSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageInfo) GetPage() int {
|
||||||
|
return p.Page
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageInfo) SetTotal(total int) {
|
||||||
|
p.Total = total
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageInfo) SetItems(items any) {
|
||||||
|
p.Items = items
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPageQuery(c *gin.Context) (*PageInfo, error) {
|
||||||
|
pageInfo := &PageInfo{}
|
||||||
|
err := c.BindQuery(pageInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if pageInfo.Page < 1 {
|
||||||
|
// 兼容
|
||||||
|
page, _ := strconv.Atoi(c.Query("p"))
|
||||||
|
if page != 0 {
|
||||||
|
pageInfo.Page = page
|
||||||
|
} else {
|
||||||
|
pageInfo.Page = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if pageInfo.PageSize == 0 {
|
||||||
|
pageInfo.PageSize = ItemsPerPage
|
||||||
|
}
|
||||||
|
return pageInfo, nil
|
||||||
|
}
|
||||||
@@ -246,15 +246,15 @@ func Register(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetAllUsers(c *gin.Context) {
|
func GetAllUsers(c *gin.Context) {
|
||||||
p, _ := strconv.Atoi(c.Query("p"))
|
pageInfo, err := common.GetPageQuery(c)
|
||||||
pageSize, _ := strconv.Atoi(c.Query("page_size"))
|
if err != nil {
|
||||||
if p < 1 {
|
c.JSON(http.StatusOK, gin.H{
|
||||||
p = 1
|
"success": false,
|
||||||
|
"message": "parse page query failed",
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if pageSize < 0 {
|
users, total, err := model.GetAllUsers(pageInfo)
|
||||||
pageSize = common.ItemsPerPage
|
|
||||||
}
|
|
||||||
users, total, err := model.GetAllUsers((p-1)*pageSize, pageSize)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
@@ -262,15 +262,13 @@ func GetAllUsers(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pageInfo.SetTotal(int(total))
|
||||||
|
pageInfo.SetItems(users)
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": "",
|
"message": "",
|
||||||
"data": gin.H{
|
"data": pageInfo,
|
||||||
"items": users,
|
|
||||||
"total": total,
|
|
||||||
"page": p,
|
|
||||||
"page_size": pageSize,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ func GetMaxUserId() int {
|
|||||||
return user.Id
|
return user.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllUsers(startIdx int, num int) (users []*User, total int64, err error) {
|
func GetAllUsers(pageInfo *common.PageInfo) (users []*User, total int64, err error) {
|
||||||
// Start transaction
|
// Start transaction
|
||||||
tx := DB.Begin()
|
tx := DB.Begin()
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
@@ -134,7 +134,7 @@ func GetAllUsers(startIdx int, num int) (users []*User, total int64, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get paginated users within same transaction
|
// Get paginated users within same transaction
|
||||||
err = tx.Unscoped().Order("id desc").Limit(num).Offset(startIdx).Omit("password").Find(&users).Error
|
err = tx.Unscoped().Order("id desc").Limit(pageInfo.GetPageSize()).Offset(pageInfo.GetStartIdx()).Omit("password").Find(&users).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
|
|||||||
Reference in New Issue
Block a user