@@ -49,4 +49,112 @@ httpfacades.NewResult(ctx).Error(500, "用户不存在", "no users find")
49
49
三、表单验证错误:
50
50
``` go
51
51
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¤tPage=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¤tPage=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¤tPage=1",
109
+ // "last": "http://localhost:3000//api/user/indexs2¤tPage=0",
110
+ // "prev": "http://localhost:3000//api/user/indexs2¤tPage=0",
111
+ // "next": "http://localhost:3000//api/user/indexs2¤tPage=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¤tPage=1" ,
149
+ "last" : " http://localhost:3000//api/user/indexs2¤tPage=0" ,
150
+ "prev" : " http://localhost:3000//api/user/indexs2¤tPage=0" ,
151
+ "next" : " http://localhost:3000//api/user/indexs2¤tPage=2"
152
+ },
153
+ "meta" : {
154
+ "total_page" : 1 ,
155
+ "current_page" : 1 ,
156
+ "per_page" : 2 ,
157
+ "total" : 1
158
+ }
159
+ }
160
+ ```
0 commit comments