统一处理分页参数简化查询接口
This commit is contained in:
@@ -6,15 +6,52 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrResp 定义通用错误响应格式
|
||||
type ErrResp struct {
|
||||
Message string `json:"message"`
|
||||
Error bool `json:"error"`
|
||||
// region req
|
||||
|
||||
type PageReqInter interface {
|
||||
GetPage() int
|
||||
GetSize() int
|
||||
GetOffset() int
|
||||
GetLimit() int
|
||||
}
|
||||
|
||||
type PageReq struct {
|
||||
Page int `json:"page"`
|
||||
Size int `json:"size"`
|
||||
RawPage int `json:"page"`
|
||||
RawSize int `json:"size"`
|
||||
}
|
||||
|
||||
func (p *PageReq) GetPage() int {
|
||||
if p.RawPage < 1 {
|
||||
return 1
|
||||
}
|
||||
return p.RawPage
|
||||
}
|
||||
|
||||
func (p *PageReq) GetSize() int {
|
||||
if p.RawSize < 1 {
|
||||
return 10
|
||||
}
|
||||
if p.RawSize > 100 {
|
||||
return 100
|
||||
}
|
||||
return p.RawSize
|
||||
}
|
||||
|
||||
func (p *PageReq) GetOffset() int {
|
||||
return (p.GetPage() - 1) * p.GetSize()
|
||||
}
|
||||
|
||||
func (p *PageReq) GetLimit() int {
|
||||
return p.GetSize()
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region resp
|
||||
|
||||
type ErrResp struct {
|
||||
Message string `json:"message"`
|
||||
Error bool `json:"error"`
|
||||
}
|
||||
|
||||
type PageResp struct {
|
||||
@@ -24,6 +61,8 @@ type PageResp struct {
|
||||
List any `json:"list"`
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region LocalDateTime
|
||||
|
||||
type LocalDateTime time.Time
|
||||
|
||||
Reference in New Issue
Block a user