Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d76c3ebe
authored
Jun 27, 2025
by
Xyfacai
Committed by
GitHub
Jun 27, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1298 from xiangyuanliu/feat/page-format
feat: 优化分页组件
parents
6f4f05f2
b206a7c6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
16 deletions
+76
-16
common/page_info.go
+62
-0
controller/user.go
+12
-14
model/user.go
+2
-2
No files found.
common/page_info.go
0 → 100644
View file @
d76c3ebe
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
}
controller/user.go
View file @
d76c3ebe
...
...
@@ -246,15 +246,15 @@ func Register(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
p
ageInfo
,
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
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
...
...
@@ -262,15 +262,13 @@ func GetAllUsers(c *gin.Context) {
})
return
}
pageInfo
.
SetTotal
(
int
(
total
))
pageInfo
.
SetItems
(
users
)
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
gin
.
H
{
"items"
:
users
,
"total"
:
total
,
"page"
:
p
,
"page_size"
:
pageSize
,
},
"data"
:
pageInfo
,
})
return
}
...
...
model/user.go
View file @
d76c3ebe
...
...
@@ -114,7 +114,7 @@ func GetMaxUserId() int {
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
tx
:=
DB
.
Begin
()
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
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
{
tx
.
Rollback
()
return
nil
,
0
,
err
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment