Explorar o código

Merge branch 'main' into alpha

CaIon hai 6 meses
pai
achega
389a4c3e4c
Modificáronse 3 ficheiros con 76 adicións e 16 borrados
  1. 62 0
      common/page_info.go
  2. 12 14
      controller/user.go
  3. 2 2
      model/user.go

+ 62 - 0
common/page_info.go

@@ -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
+}

+ 12 - 14
controller/user.go

@@ -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"))
-	pageSize, _ := strconv.Atoi(c.Query("page_size"))
-	if p < 1 {
-		p = 1
-	}
-	if pageSize < 0 {
-		pageSize = common.ItemsPerPage
+	pageInfo, err := common.GetPageQuery(c)
+	if err != nil {
+		c.JSON(http.StatusOK, gin.H{
+			"success": false,
+			"message": "parse page query failed",
+		})
+		return
 	}
 	}
-	users, total, err := model.GetAllUsers((p-1)*pageSize, pageSize)
+	users, total, err := model.GetAllUsers(pageInfo)
 	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{
-			"items":     users,
-			"total":     total,
-			"page":      p,
-			"page_size": pageSize,
-		},
+		"data":    pageInfo,
 	})
 	})
 	return
 	return
 }
 }

+ 2 - 2
model/user.go

@@ -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