-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_result.go
102 lines (94 loc) · 2.46 KB
/
http_result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package http_result
import (
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
)
/*
*
Success(200,"操作成功",data)
Error(500,"服务器错误",data)
*/
type Instance interface {
Success(message string, data interface{}) http.Response
Error(code int, message string, data interface{}) http.Response
ValidError(message string, errors map[string]map[string]string) http.Response
SearchByParams(params map[string]string, excepts ...string) *HttpResult
ResultPagination(dest any) (http.Response, error)
}
type HttpResult struct {
Query orm.Query
Context http.Context
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
Response http.Response
}
func NewResult(ctx http.Context) *HttpResult {
return &HttpResult{
Context: ctx,
Query: facades.Orm().Query(),
}
}
/*
Success 默认成功返回
*/
func (h *HttpResult) Success(message string, data interface{}) http.Response {
if message == "" {
message = facades.Config().GetString("http_result.Message")
}
//查询的结果有可能存在data为nil的情况,判断如果是nil则,给定一个[]string{}类型的数据,避免前端页面报错
//或者判断data的slice类型的长度为0
res := []string{}
if data == nil {
return h.Context.Response().Success().Json(http.Json{
"message": message,
"data": res,
})
}
slice, ok := data.([]interface{})
if ok && len(slice) == 0 {
return h.Context.Response().Success().Json(http.Json{
"message": message,
"data": res,
})
}
return h.Context.Response().Success().Json(http.Json{
"message": message,
"data": data,
})
}
/*
*
Error
自定义错误
*/
func (h *HttpResult) Error(code int, message string, data interface{}) http.Response {
if message == "" {
message = facades.Config().GetString("http_result.Message")
}
if code == 0 {
code = facades.Config().GetInt("http_result.Code")
}
h.Context.Request().AbortWithStatusJson(code, http.Json{
"message": message,
"data": data,
})
return nil
}
/*
*
ValidError
表单验证服务
*/
func (h *HttpResult) ValidError(message string, errors map[string]map[string]string) http.Response {
h.Code = 422
if message == "" {
message = facades.Config().GetString("http_result.Message", "验证失败")
}
h.Context.Request().AbortWithStatusJson(h.Code, http.Json{
"message": message,
"errors": errors,
})
return nil
}