Skip to content

Commit 95f72ac

Browse files
committed
updatereadme
1 parent 9924119 commit 95f72ac

File tree

4 files changed

+119
-12
lines changed

4 files changed

+119
-12
lines changed

README.md

+109-1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,112 @@ httpfacades.NewResult(ctx).Error(500, "用户不存在", "no users find")
4949
三、表单验证错误:
5050
```go
5151
httpfacades.NewResult(ctx).ValidError("验证失败", errors.All())
52-
```
52+
```
53+
四、分页查询:
54+
```go
55+
func (r *UserController) Indexs(ctx http.Context) http.Response {
56+
users := []models.User{}
57+
facades.Orm().Query().Model(&models.User{}).Find(&users)
58+
request := ctx.Request()
59+
result, err := r.ResultPagination.SearchByParams(request.Queries()).ResultPagination(ctx, &users)
60+
if err != nil {
61+
return nil
62+
}
63+
return result
64+
}
65+
```
66+
说明: 配合前端表格渲染,其中固定参数为pageSize,currentPage,sort,order,其他参数将默认采用like模糊查询
67+
##### example:?name=xxx&pageSize=10&currentPage=1&sort=age&order=desc
68+
##### 解释一:根据表中的列name模糊查询,每页10条,当前第一页,按照age列降序排列
69+
##### 解释二:查询参数:order仅支持asc,desc默认采用asc,sort表示需要查询的列,order参数和sort参数需要同时出现
70+
##### 解释三:前端,可以配合vue组件,https://vxetable.cn,使用效率更高
71+
```go
72+
// Index 用户分页查询,支持搜索,路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
73+
// @Summary 用户分页查询
74+
// @Description 用户分页查询
75+
// @Tags 用户分页查询
76+
// @Accept json
77+
// @Produce json
78+
// @Security ApiKeyAuth
79+
// @Param Authorization header string false "Bearer 用户令牌"
80+
// @Param name query string false "name"
81+
// @Param pageSize query string false "pageSize"
82+
// @Param currentPage query string false "currentPage"
83+
// @Param sort query string false "sort"
84+
// @Param order query string false "order"
85+
// @Success 200 {string} json {
86+
//"data": [
87+
//{
88+
//"id": 6,
89+
//"created_at": "2024-05-19 11:22:22",
90+
//"updated_at": "2024-05-19 11:22:22",
91+
//"name": "Karolann Waelchi",
92+
//"mobile": "Annalise Koss",
93+
//"password": "eyJpdiI6Im5JbVNXQ2pWV0FOVkxFTG0iLCJ2YWx1ZSI6IkJKYTc5bWt0WWRrUFRPYVJlMW5NcWN0SXFWK29iYVBqIn0=",
94+
//"area": "",
95+
//"contact": "Juana Russel",
96+
//"contact_mobile": "9210102772",
97+
//"address": "95469 New Bypassshire",
98+
//"id_card": "4034197872575788",
99+
//"control_arr": null,
100+
//"pid": 0,
101+
//"parent": null,
102+
//"children": null,
103+
//"deleted_at": null
104+
//}
105+
//],
106+
//"total": 1,
107+
//"links": {
108+
//"first": "http://localhost:3000//api/user/indexs?pageSize=2&currentPage=1",
109+
//"last": "http://localhost:3000//api/user/indexs2&currentPage=0",
110+
//"prev": "http://localhost:3000//api/user/indexs2&currentPage=0",
111+
//"next": "http://localhost:3000//api/user/indexs2&currentPage=2"
112+
//},
113+
//"meta": {
114+
//"total_page": 1,
115+
//"current_page": 1,
116+
//"per_page": 2,
117+
//"total": 1
118+
//}
119+
//}
120+
// @Router /api/user [get]
121+
```
122+
123+
##### 解释四:返回结果
124+
```json
125+
{
126+
"data": [
127+
{
128+
"id": 6,
129+
"created_at": "2024-05-19 11:22:22",
130+
"updated_at": "2024-05-19 11:22:22",
131+
"name": "Karolann Waelchi",
132+
"mobile": "Annalise Koss",
133+
"password": "eyJpdiI6Im5JbVNXQ2pWV0FOVkxFTG0iLCJ2YWx1ZSI6IkJKYTc5bWt0WWRrUFRPYVJlMW5NcWN0SXFWK29iYVBqIn0=",
134+
"area": "",
135+
"contact": "Juana Russel",
136+
"contact_mobile": "9210102772",
137+
"address": "95469 New Bypassshire",
138+
"id_card": "4034197872575788",
139+
"control_arr": null,
140+
"pid": 0,
141+
"parent": null,
142+
"children": null,
143+
"deleted_at": null
144+
}
145+
],
146+
"total": 1,
147+
"links": {
148+
"first": "http://localhost:3000//api/user/indexs?pageSize=2&currentPage=1",
149+
"last": "http://localhost:3000//api/user/indexs2&currentPage=0",
150+
"prev": "http://localhost:3000//api/user/indexs2&currentPage=0",
151+
"next": "http://localhost:3000//api/user/indexs2&currentPage=2"
152+
},
153+
"meta": {
154+
"total_page": 1,
155+
"current_page": 1,
156+
"per_page": 2,
157+
"total": 1
158+
}
159+
}
160+
```

contracts/http_result.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package contracts
22

3-
import (
4-
"github.com/goravel/framework/contracts/database/orm"
5-
"github.com/goravel/framework/contracts/http"
6-
)
7-
3+
import "github.com/goravel/framework/contracts/http"
4+
import HttpResultIns "github.com/hulutech-web/http_result"
85
type HttpResult interface {
96
Success(message string, data interface{}) http.Response
107
Error(code int, message string, data interface{}) http.Response
118
ValidError(message string, errors map[string]map[string]string) http.Response
12-
SearchByParams(params map[string]string, excepts ...string) func(methods orm.Query) orm.Query
9+
ResultPagination(ctx http.Context, dest any) (http.Response, error)
10+
SearchByParams(params map[string]string, excepts ...string) *HttpResultIns
1311
}

http_result.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ type Instance interface {
1515
Success(message string, data interface{}) http.Response
1616
Error(code int, message string, data interface{}) http.Response
1717
ValidError(message string, errors map[string]map[string]string) http.Response
18-
SearchByParams(params map[string]string, excepts ...string) func(methods orm.Query) orm.Query
18+
SearchByParams(params map[string]string, excepts ...string) *HttpResult
19+
ResultPagination(ctx http.Context, dest any) (http.Response, error)
1920
}
2021

2122
type HttpResult struct {

paginator.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ func (r *HttpResult) ResultPagination(ctx http.Context, dest any) (http.Response
6767
}
6868
// Corrected links generation
6969
links := Links{
70-
First: proto + request.Origin().Host + "/" + URL_PATH + "?pageSize=" + pageSize + "&currentPage=1",
71-
Last: proto + request.Origin().Host + "/" + URL_PATH + pageSize + "&currentPage=" + strconv.Itoa(int(total)/pageSizeInt),
72-
Prev: proto + request.Origin().Host + "/" + URL_PATH + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt-1),
73-
Next: proto + request.Origin().Host + "/" + URL_PATH + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt+1),
70+
First: proto + request.Origin().Host + URL_PATH + "?pageSize=" + pageSize + "&currentPage=1",
71+
Last: proto + request.Origin().Host + URL_PATH + pageSize + "&currentPage=" + strconv.Itoa(int(total)/pageSizeInt),
72+
Prev: proto + request.Origin().Host + URL_PATH + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt-1),
73+
Next: proto + request.Origin().Host + URL_PATH + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt+1),
7474
}
7575

7676
// Corrected total page calculation

0 commit comments

Comments
 (0)