forked from go-admin-team/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.go
328 lines (285 loc) · 8.4 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package models
import (
"errors"
"go-admin/global/orm"
"go-admin/tools"
)
type Menu struct {
MenuId int `json:"menuId" gorm:"primary_key;AUTO_INCREMENT"`
MenuName string `json:"menuName" gorm:"type:varchar(128);"`
Title string `json:"title" gorm:"type:varchar(64);"`
Icon string `json:"icon" gorm:"type:varchar(128);"`
Path string `json:"path" gorm:"type:varchar(128);"`
Paths string `json:"paths" gorm:"type:varchar(128);"`
MenuType string `json:"menuType" gorm:"type:varchar(1);"`
Action string `json:"action" gorm:"type:varchar(16);"`
Permission string `json:"permission" gorm:"type:varchar(32);"`
ParentId int `json:"parentId" gorm:"type:int(11);"`
NoCache bool `json:"noCache" gorm:"type:char(1);"`
Breadcrumb string `json:"breadcrumb" gorm:"type:varchar(255);"`
Component string `json:"component" gorm:"type:varchar(255);"`
Sort int `json:"sort" gorm:"type:int(4);"`
Visible string `json:"visible" gorm:"type:char(1);"`
CreateBy string `json:"createBy" gorm:"type:varchar(128);"`
UpdateBy string `json:"updateBy" gorm:"type:varchar(128);"`
IsFrame string `json:"isFrame" gorm:"type:int(1);DEFAULT:0;"`
DataScope string `json:"dataScope" gorm:"-"`
Params string `json:"params" gorm:"-"`
RoleId int `gorm:"-"`
Children []Menu `json:"children" gorm:"-"`
IsSelect bool `json:"is_select" gorm:"-"`
BaseModel
}
func (Menu) TableName() string {
return "sys_menu"
}
type MenuLable struct {
Id int `json:"id" gorm:"-"`
Label string `json:"label" gorm:"-"`
Children []MenuLable `json:"children" gorm:"-"`
}
type Menus struct {
MenuId int `json:"menuId" gorm:"column:menu_id;primary_key"`
MenuName string `json:"menuName" gorm:"column:menu_name"`
Title string `json:"title" gorm:"column:title"`
Icon string `json:"icon" gorm:"column:icon"`
Path string `json:"path" gorm:"column:path"`
MenuType string `json:"menuType" gorm:"column:menu_type"`
Action string `json:"action" gorm:"column:action"`
Permission string `json:"permission" gorm:"column:permission"`
ParentId int `json:"parentId" gorm:"column:parent_id"`
NoCache bool `json:"noCache" gorm:"column:no_cache"`
Breadcrumb string `json:"breadcrumb" gorm:"column:breadcrumb"`
Component string `json:"component" gorm:"column:component"`
Sort int `json:"sort" gorm:"column:sort"`
Visible string `json:"visible" gorm:"column:visible"`
Children []Menu `json:"children" gorm:"-"`
CreateBy string `json:"createBy" gorm:"column:create_by"`
UpdateBy string `json:"updateBy" gorm:"column:update_by"`
DataScope string `json:"dataScope" gorm:"-"`
Params string `json:"params" gorm:"-"`
BaseModel
}
func (Menus) TableName() string {
return "sys_menu"
}
type MenuRole struct {
Menus
IsSelect bool `json:"is_select" gorm:"-"`
}
type MS []Menu
func (e *Menu) GetByMenuId() (Menu Menu, err error) {
table := orm.Eloquent.Table(e.TableName())
table = table.Where("menu_id = ?", e.MenuId)
if err = table.Find(&Menu).Error; err != nil {
return
}
return
}
func (e *Menu) SetMenu() (m []Menu, err error) {
menulist, err := e.GetPage()
m = make([]Menu, 0)
for i := 0; i < len(menulist); i++ {
if menulist[i].ParentId != 0 {
continue
}
menusInfo := DiguiMenu(&menulist, menulist[i])
m = append(m, menusInfo)
}
return
}
func DiguiMenu(menulist *[]Menu, menu Menu) Menu {
list := *menulist
min := make([]Menu, 0)
for j := 0; j < len(list); j++ {
if menu.MenuId != list[j].ParentId {
continue
}
mi := Menu{}
mi.MenuId = list[j].MenuId
mi.MenuName = list[j].MenuName
mi.Title = list[j].Title
mi.Icon = list[j].Icon
mi.Path = list[j].Path
mi.MenuType = list[j].MenuType
mi.Action = list[j].Action
mi.Permission = list[j].Permission
mi.ParentId = list[j].ParentId
mi.NoCache = list[j].NoCache
mi.Breadcrumb = list[j].Breadcrumb
mi.Component = list[j].Component
mi.Sort = list[j].Sort
mi.Visible = list[j].Visible
mi.Children = []Menu{}
if mi.MenuType != "F" {
ms := DiguiMenu(menulist, mi)
min = append(min, ms)
} else {
min = append(min, mi)
}
}
menu.Children = min
return menu
}
func (e *Menu) SetMenuLable() (m []MenuLable, err error) {
menulist, err := e.Get()
m = make([]MenuLable, 0)
for i := 0; i < len(menulist); i++ {
if menulist[i].ParentId != 0 {
continue
}
e := MenuLable{}
e.Id = menulist[i].MenuId
e.Label = menulist[i].Title
menusInfo := DiguiMenuLable(&menulist, e)
m = append(m, menusInfo)
}
return
}
func DiguiMenuLable(menulist *[]Menu, menu MenuLable) MenuLable {
list := *menulist
min := make([]MenuLable, 0)
for j := 0; j < len(list); j++ {
if menu.Id != list[j].ParentId {
continue
}
mi := MenuLable{}
mi.Id = list[j].MenuId
mi.Label = list[j].Title
mi.Children = []MenuLable{}
if list[j].MenuType != "F" {
ms := DiguiMenuLable(menulist, mi)
min = append(min, ms)
} else {
min = append(min, mi)
}
}
menu.Children = min
return menu
}
func (e *Menu) SetMenuRole(rolename string) (m []Menu, err error) {
menulist, err := e.GetByRoleName(rolename)
m = make([]Menu, 0)
for i := 0; i < len(menulist); i++ {
if menulist[i].ParentId != 0 {
continue
}
menusInfo := DiguiMenu(&menulist, menulist[i])
m = append(m, menusInfo)
}
return
}
func (e *MenuRole) Get() (Menus []MenuRole, err error) {
table := orm.Eloquent.Table(e.TableName())
if e.MenuName != "" {
table = table.Where("menu_name = ?", e.MenuName)
}
if err = table.Order("sort").Find(&Menus).Error; err != nil {
return
}
return
}
func (e *Menu) GetByRoleName(rolename string) (Menus []Menu, err error) {
table := orm.Eloquent.Table(e.TableName()).Select("sys_menu.*").Joins("left join sys_role_menu on sys_role_menu.menu_id=sys_menu.menu_id")
table = table.Where("sys_role_menu.role_name=? and menu_type in ('M','C')", rolename)
if err = table.Order("sort").Find(&Menus).Error; err != nil {
return
}
return
}
func (e *Menu) Get() (Menus []Menu, err error) {
table := orm.Eloquent.Table(e.TableName())
if e.MenuName != "" {
table = table.Where("menu_name = ?", e.MenuName)
}
if e.Path != "" {
table = table.Where("path = ?", e.Path)
}
if e.Action != "" {
table = table.Where("action = ?", e.Action)
}
if e.MenuType != "" {
table = table.Where("menu_type = ?", e.MenuType)
}
if err = table.Order("sort").Find(&Menus).Error; err != nil {
return
}
return
}
func (e *Menu) GetPage() (Menus []Menu, err error) {
table := orm.Eloquent.Table(e.TableName())
if e.MenuName != "" {
table = table.Where("menu_name = ?", e.MenuName)
}
if e.Title != "" {
table = table.Where("title = ?", e.Title)
}
if e.Visible != "" {
table = table.Where("visible = ?", e.Visible)
}
if e.MenuType != "" {
table = table.Where("menu_type = ?", e.MenuType)
}
// 数据权限控制
dataPermission := new(DataPermission)
dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
table, err = dataPermission.GetDataScope("sys_menu", table)
if err != nil {
return nil, err
}
if err = table.Order("sort").Find(&Menus).Error; err != nil {
return
}
return
}
func (e *Menu) Create() (id int, err error) {
result := orm.Eloquent.Table(e.TableName()).Create(&e)
if result.Error != nil {
err = result.Error
return
}
err = InitPaths(e)
if err != nil {
return
}
id = e.MenuId
return
}
func InitPaths(menu *Menu) (err error) {
parentMenu := new(Menu)
if int(menu.ParentId) != 0 {
orm.Eloquent.Table("sys_menu").Where("menu_id = ?", menu.ParentId).First(parentMenu)
if parentMenu.Paths == "" {
err = errors.New("父级paths异常,请尝试对当前节点父级菜单进行更新操作!")
return
}
menu.Paths = parentMenu.Paths + "/" + tools.IntToString(menu.MenuId)
} else {
menu.Paths = "/0/" + tools.IntToString(menu.MenuId)
}
orm.Eloquent.Table("sys_menu").Where("menu_id = ?", menu.MenuId).Update("paths", menu.Paths)
return
}
func (e *Menu) Update(id int) (update Menu, err error) {
if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
return
}
//参数1:是要修改的数据
//参数2:是修改的数据
if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
return
}
err = InitPaths(e)
if err != nil {
return
}
return
}
func (e *Menu) Delete(id int) (success bool, err error) {
if err = orm.Eloquent.Table(e.TableName()).Where("menu_id = ?", id).Delete(&Menu{}).Error; err != nil {
success = false
return
}
success = true
return
}