go get -u github.com/hulutech-web/http_result
import "github.com/hulutech-web/http_result"
func init() {
"providers": []foundation.ServiceProvider{
....
&http_result.ServiceProvider{},
}
}
go run . artisan vendor:publish --package=github.com/hulutech-web/http_result
发布资源后,config/http_result.go中的配置文件中有默认的返回状态码和message,可自行修改
config.Add("http_result", map[string]any{
"Code": 500, //自定义修改默认状态码
"Message": "获取成功",//自定义修改默认消息
})
一、成功返回:
import httpfacades "github.com/hulutech-web/http_result"
return httpfacades.NewResult(ctx).Success("", user)
二、失败返回:
import httpfacades "github.com/hulutech-web/http_result"
httpfacades.NewResult(ctx).Error(500, "用户不存在", "no users find")
三、表单验证错误:
httpfacades.NewResult(ctx).ValidError("验证失败", errors.All())
四、分页查询+加上过滤条件:其中conditionMap:map[string]interface{}{}过滤条件,[]string{"excepts"}...为排除的key值
func (r *MaterialController) Index(ctx http.Context) http.Response {
var user models.User
facades.Auth(ctx).User(&user)
partner_id := cast.ToUint(ctx.Value("partner"))
materials := []models.Material{}
queries := ctx.Request().Queries()
conditionMap := map[string]interface{}{
"partner_id": partner_id,
}
result, _ := httpfacades.NewResult(ctx).SearchByParams(queries, conditionMap,[]string{"excepts"}...).ResultPagination(&materials)
return result
}
说明: 配合前端表格渲染,其中固定参数为pageSize,currentPage,sort,order,其他参数将默认采用like模糊查询
解释三:前端,可以配合vue组件,https://vxetable.cn,使用效率更高
// Index 用户分页查询,支持时间,支持搜索,路由参数?name=xxx&pageSize=1¤tPage=1&sort=xxx&order=xxx&created_at[]=xxx&created_at[]=xxx,等其他任意的查询参数
// @Summary 用户分页查询
// @Description 用户分页查询
// @Tags 用户分页查询
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param name query string false "name"
// @Param pageSize query string false "pageSize"
// @Param currentPage query string false "currentPage"
// @Param sort query string false "sort"
// @Param order query string false "order"
// @Success 200 {string} json {}
// @Router /api/user [get]
{
"data": [
{
"id": 6,
"created_at": "2024-05-19 11:22:22",
"updated_at": "2024-05-19 11:22:22",
"name": "Karolann Waelchi",
"mobile": "Annalise Koss",
"password": "eyJpdiI6Im5JbVNXQ2pWV0FOVkxFTG0iLCJ2YWx1ZSI6IkJKYTc5bWt0WWRrUFRPYVJlMW5NcWN0SXFWK29iYVBqIn0=",
"area": "",
"contact": "Juana Russel",
"contact_mobile": "9210102772",
"address": "95469 New Bypassshire",
"id_card": "4034197872575788",
"control_arr": null,
"pid": 0,
"parent": null,
"children": null,
"deleted_at": null
}
],
"total": 1,
"links": {
"first": "http://localhost:3000/api/user/indexs?pageSize=2¤tPage=1",
"last": "http://localhost:3000/api/user/indexs?pageSize=2¤tPage=0",
"prev": "http://localhost:3000/api/user/indexs?pageSize=2¤tPage=0",
"next": "http://localhost:3000/api/user/indexs?pageSize=2¤tPage=2"
},
"meta": {
"total_page": 1,
"current_page": 1,
"per_page": 2,
"total": 1
}
}
五、列表查询+加上过滤条件:其中conditionMap:map[string]interface{}{}过滤条件,[]string{"excepts"}...为排除的key值
func (r *MaterialController) Index(ctx http.Context) http.Response {
var user models.User
facades.Auth(ctx).User(&user)
partner_id := cast.ToUint(ctx.Value("partner"))
materials := []models.Material{}
queries := ctx.Request().Queries()
conditionMap := map[string]interface{}{
"partner_id": partner_id,
}
result, _ := httpfacades.NewResult(ctx).SearchByParams(queries, conditionMap,[]string{"excepts"}...).List(&materials)
return result
}