forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.go
193 lines (157 loc) · 4.06 KB
/
menu.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package menu
import (
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/connections"
"github.com/chenhg5/go-admin/modules/language"
"strconv"
"sync"
"sync/atomic"
)
type Menu struct {
GlobalMenuList []MenuItem
GlobalMenuOption []map[string]string
MaxOrder int64
}
var GlobalMenu = &Menu{
GlobalMenuList: []MenuItem{},
GlobalMenuOption: []map[string]string{},
MaxOrder: 0,
}
var InitMenuOnce = &Once{
done: 0,
}
type Once struct {
m sync.Mutex
done uint32
}
func (o *Once) Do(f func()) {
if atomic.LoadUint32(&o.done) == 1 {
return
}
// Slow-path.
o.m.Lock()
defer o.m.Unlock()
if o.done == 0 {
defer atomic.StoreUint32(&o.done, 1)
f()
}
}
func (o *Once) unlock() {
atomic.StoreUint32(&o.done, 0)
}
func InitMenu(user auth.User) {
InitMenuOnce.Do(func() {
SetGlobalMenu(user)
})
}
func GetGlobalMenu(user auth.User) Menu {
InitMenu(user)
return *GlobalMenu
}
func Unlock() {
InitMenuOnce.unlock()
}
func SetGlobalMenu(user auth.User) {
var (
menus []map[string]interface{}
menuOption = make([]map[string]string, 0)
)
if user.IsSuperAdmin() {
menus, _ = connections.GetConnection().Query("select * from goadmin_menu where id > 0 order by `order` asc")
} else {
qmark := ""
var ids []interface{}
for i := 0; i < len(user.Menus); i++ {
qmark += "?,"
ids = append(ids, user.Menus[i])
}
menus, _ = connections.GetConnection().Query("select * from goadmin_menu where id in ("+qmark[:len(qmark)-1]+") "+
"order by `order` asc", ids...)
}
var title string
for i := 0; i < len(menus); i++ {
if menus[i]["type"].(int64) == 1 {
title = language.Get(menus[i]["title"].(string))
} else {
title = menus[i]["title"].(string)
}
menuOption = append(menuOption, map[string]string{
"id": strconv.FormatInt(menus[i]["id"].(int64), 10),
"title": title,
})
}
menulist := ConstructMenuTree(menus, 0)
(*GlobalMenu).GlobalMenuOption = menuOption
(*GlobalMenu).GlobalMenuList = menulist
(*GlobalMenu).MaxOrder = menus[len(menus)-1]["parent_id"].(int64)
}
func (menu *Menu) SexMaxOrder(order int64) {
menu.MaxOrder = order
}
func ConstructMenuTree(menus []map[string]interface{}, parentId int64) []MenuItem {
branch := make([]MenuItem, 0)
var title string
for j := 0; j < len(menus); j++ {
if parentId == menus[j]["parent_id"].(int64) {
childList := ConstructMenuTree(menus, menus[j]["id"].(int64))
if menus[j]["type"].(int64) == 1 {
title = language.Get(menus[j]["title"].(string))
} else {
title = menus[j]["title"].(string)
}
child := MenuItem{
Name: title,
ID: strconv.FormatInt(menus[j]["id"].(int64), 10),
Url: menus[j]["uri"].(string),
Icon: menus[j]["icon"].(string),
Active: "",
ChildrenList: childList,
}
branch = append(branch, child)
}
}
return branch
}
func GetMenuItemById(id string) MenuItem {
menu, _ := connections.GetConnection().Query("select * from goadmin_menu where id = ?", id)
return MenuItem{
Name: menu[0]["title"].(string),
ID: strconv.FormatInt(menu[0]["id"].(int64), 10),
Url: menu[0]["uri"].(string),
Icon: menu[0]["icon"].(string),
Active: "",
ChildrenList: []MenuItem{},
}
}
func (menu *Menu) SetActiveClass(path string) {
for i := 0; i < len((*menu).GlobalMenuList); i++ {
(*menu).GlobalMenuList[i].Active = ""
}
Loop:
for i := 0; i < len((*menu).GlobalMenuList); i++ {
if (*menu).GlobalMenuList[i].Url == path {
(*menu).GlobalMenuList[i].Active = "active"
break Loop
} else {
for j := 0; j < len((*menu).GlobalMenuList[i].ChildrenList); j++ {
if (*menu).GlobalMenuList[i].ChildrenList[j].Url == path {
(*menu).GlobalMenuList[i].Active = "active"
break Loop
} else {
(*menu).GlobalMenuList[i].Active = ""
}
}
}
}
}
type MenuItem struct {
Name string
ID string
Url string
Icon string
Active string
ChildrenList []MenuItem
}
func (menu *Menu) GetEditMenuList() []MenuItem {
return (*menu).GlobalMenuList
}