forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
133 lines (112 loc) · 2.77 KB
/
api.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package share
import (
"fmt"
"strings"
"github.com/gin-gonic/gin"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/utils"
)
// IsAllow 鉴权处理程序
func (api API) IsAllow(v interface{}) bool {
c, ok := v.(*gin.Context)
if !ok {
return false
}
guards := strings.Split(api.Guard, ",")
for _, guard := range guards {
guard = strings.TrimSpace(guard)
handler, has := gou.HTTPGuards[guard]
if has {
handler(c)
fmt.Println(api.Guard, c.IsAborted())
return c.IsAborted()
}
}
return false
}
// ValidateLoop 循环引用校验
func (api API) ValidateLoop(name string) API {
if strings.ToLower(api.Process) == strings.ToLower(name) {
exception.New("循环引用 %s", 400, name).Throw()
}
return api
}
// ProcessIs 检查处理器名称
func (api API) ProcessIs(name string) bool {
return strings.ToLower(api.Process) == strings.ToLower(name)
}
// DefaultQueryParams 读取参数 QueryParam
func (api API) DefaultQueryParams(i int, defaults ...gou.QueryParam) gou.QueryParam {
param := gou.QueryParam{}
if len(defaults) > 0 {
param = defaults[0]
}
if api.Default[i] == nil || len(api.Default) <= i {
return param
}
param, ok := api.Default[i].(gou.QueryParam)
if !ok {
param, ok = gou.AnyToQueryParam(api.Default[i])
}
return param
}
// DefaultInt 读取参数 Int
func (api API) DefaultInt(i int, defaults ...int) int {
value := 0
ok := false
if len(defaults) > 0 {
value = defaults[0]
}
if api.Default[i] == nil || len(api.Default) <= i {
return value
}
value, ok = api.Default[i].(int)
if !ok {
value = any.Of(api.Default[i]).CInt()
}
return value
}
// DefaultString 读取参数 String
func (api API) DefaultString(i int, defaults ...string) string {
value := ""
ok := false
if len(defaults) > 0 {
value = defaults[0]
}
if api.Default[i] == nil || len(api.Default) <= i {
return value
}
value, ok = api.Default[i].(string)
if !ok {
value = any.Of(api.Default[i]).CString()
}
return value
}
// MergeDefaultQueryParam 合并默认查询参数
func (api API) MergeDefaultQueryParam(param gou.QueryParam, i int) gou.QueryParam {
if len(api.Default) > i && api.Default[i] != nil {
defaults, ok := gou.AnyToQueryParam(api.Default[i])
if !ok {
exception.New("参数默认值数据结构错误", 400).Ctx(api.Default[i]).Throw()
}
if defaults.Withs != nil {
param.Withs = defaults.Withs
}
if defaults.Select != nil {
param.Select = defaults.Select
utils.Dump(param.Select)
}
if defaults.Wheres != nil {
if param.Wheres == nil {
param.Wheres = []gou.QueryWhere{}
}
param.Wheres = append(param.Wheres, defaults.Wheres...)
}
if defaults.Orders != nil {
param.Orders = append(param.Orders, defaults.Orders...)
}
}
return param
}