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
b206a7c6
authored
Jun 25, 2025
by
Xiangyuan-liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 优化分页组件
parent
6f4f05f2
Show 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 @
b206a7c6
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 @
b206a7c6
...
@@ -246,15 +246,15 @@ func Register(c *gin.Context) {
...
@@ -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"
)
)
p
ageInfo
,
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"
,
if
pageSize
<
0
{
})
pageSize
=
common
.
ItemsPerPage
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) {
...
@@ -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
}
}
...
...
model/user.go
View file @
b206a7c6
...
@@ -114,7 +114,7 @@ func GetMaxUserId() int {
...
@@ -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)
...
@@ -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
...
...
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