@@ -10,19 +10,29 @@ import (
10
10
"gorm.io/gorm"
11
11
)
12
12
13
- type MenuService struct {}
13
+ type MenuService struct {
14
+ GuardName string
15
+ }
14
16
15
17
// 初始化
16
18
func NewMenuService () * MenuService {
17
- return & MenuService {}
19
+ return & MenuService {
20
+ GuardName : "admin" ,
21
+ }
22
+ }
23
+
24
+ // 设置守卫名称
25
+ func (p * MenuService ) SetGuardName (guardName string ) * MenuService {
26
+ p .GuardName = guardName
27
+ return p
18
28
}
19
29
20
30
// 获取菜单列表
21
31
func (p * MenuService ) GetList () (menus []model.Menu , Error error ) {
22
32
list := []model.Menu {}
23
33
24
34
err := db .Client .
25
- Where ("guard_name = ?" , "admin" ).
35
+ Where ("guard_name = ?" , p . GuardName ).
26
36
Where ("status = ?" , 1 ).
27
37
Order ("sort asc,id asc" ).
28
38
Select ("name" , "id" , "pid" ).
@@ -47,7 +57,7 @@ func (p *MenuService) GetListWithRoot() (menus []model.Menu, Error error) {
47
57
func (p * MenuService ) FindParentTreeNode (chrildPid int ) (list []model.Menu ) {
48
58
menus := []model.Menu {}
49
59
db .Client .
50
- Where ("guard_name = ?" , "admin" ).
60
+ Where ("guard_name = ?" , p . GuardName ).
51
61
Where ("id = ?" , chrildPid ).
52
62
Where ("status = ?" , 1 ).
53
63
Where ("type IN ?" , []int {1 , 2 , 3 }).
@@ -69,13 +79,13 @@ func (p *MenuService) FindParentTreeNode(chrildPid int) (list []model.Menu) {
69
79
return menus
70
80
}
71
81
72
- // 通过管理员ID权限菜单
73
- func (p * MenuService ) GetListByAdminId ( adminId int ) (menuList interface {}, err error ) {
82
+ // 通过用户ID获取菜单
83
+ func (p * MenuService ) GetListByUserId ( userId int ) (menuList interface {}, err error ) {
74
84
menus := []model.Menu {}
75
85
76
- if adminId == 1 {
86
+ if userId == 1 {
77
87
db .Client .
78
- Where ("guard_name" , "admin" ).
88
+ Where ("guard_name" , p . GuardName ).
79
89
Where ("status = ?" , 1 ).
80
90
Where ("type IN ?" , []int {1 , 2 , 3 }).
81
91
Order ("sort asc" ).
@@ -85,7 +95,7 @@ func (p *MenuService) GetListByAdminId(adminId int) (menuList interface{}, err e
85
95
}
86
96
87
97
var menuIds []int
88
- roleHasMenus , err := NewCasbinService ().GetUserMenus (adminId )
98
+ roleHasMenus , err := NewCasbinService ().GetUserMenus (userId )
89
99
if err != nil {
90
100
return menuList , err
91
101
}
@@ -99,7 +109,7 @@ func (p *MenuService) GetListByAdminId(adminId int) (menuList interface{}, err e
99
109
100
110
// 最底层列表
101
111
db .Client .
102
- Where ("guard_name = ?" , "admin" ).
112
+ Where ("guard_name = ?" , p . GuardName ).
103
113
Where ("status = ?" , 1 ).
104
114
Where ("id in ?" , menuIds ).
105
115
Where ("type IN ?" , []int {1 , 2 , 3 }).
@@ -115,7 +125,7 @@ func (p *MenuService) GetListByAdminId(adminId int) (menuList interface{}, err e
115
125
116
126
// 所有列表
117
127
db .Client .
118
- Where ("guard_name = ?" , "admin" ).
128
+ Where ("guard_name = ?" , p . GuardName ).
119
129
Where ("status = ?" , 1 ).
120
130
Where ("id in ?" , menuIds ).
121
131
Order ("sort asc" ).
0 commit comments