调整request的方法,增加类型转换的方法
This commit is contained in:
@@ -4,14 +4,14 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
commonhttp "git.toowon.com/jimmy/go-common/http"
|
||||
"git.toowon.com/jimmy/go-common/factory"
|
||||
commonhttp "git.toowon.com/jimmy/go-common/http"
|
||||
)
|
||||
|
||||
// ListUserRequest 用户列表请求(包含分页字段)
|
||||
type ListUserRequest struct {
|
||||
Keyword string `json:"keyword"`
|
||||
commonhttp.PaginationRequest // 嵌入分页请求结构
|
||||
Keyword string `json:"keyword"`
|
||||
commonhttp.PaginationRequest // 嵌入分页请求结构
|
||||
}
|
||||
|
||||
// User 用户结构
|
||||
@@ -36,13 +36,13 @@ func GetUserList(w http.ResponseWriter, r *http.Request) {
|
||||
// 方式2:从查询参数解析分页
|
||||
pagination := commonhttp.ParsePaginationRequest(r)
|
||||
req.PaginationRequest = *pagination
|
||||
req.Keyword = commonhttp.GetQuery(r, "keyword", "")
|
||||
req.Keyword = r.URL.Query().Get("keyword")
|
||||
}
|
||||
|
||||
// 使用分页方法
|
||||
page := req.GetPage() // 获取页码(默认1)
|
||||
size := req.GetSize() // 获取每页数量(默认20,最大100)
|
||||
_ = req.GetOffset() // 计算偏移量
|
||||
page := req.GetPage() // 获取页码(默认1)
|
||||
pageSize := req.GetPageSize() // 获取每页数量(默认20,最大100)
|
||||
_ = req.GetOffset() // 计算偏移量
|
||||
|
||||
// 模拟查询数据
|
||||
users := []User{
|
||||
@@ -52,7 +52,7 @@ func GetUserList(w http.ResponseWriter, r *http.Request) {
|
||||
total := int64(100)
|
||||
|
||||
// 返回分页响应(使用factory方法)
|
||||
fac.SuccessPage(w, users, total, page, size)
|
||||
fac.SuccessPage(w, users, total, page, pageSize)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
Reference in New Issue
Block a user