roleMenuService.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package service
  2. import (
  3. "iot_manager_service/app/system/dao"
  4. "iot_manager_service/util/common"
  5. )
  6. // 角色菜单管理服务
  7. var RoleMenuService = new(roleMenuService)
  8. type roleMenuService struct{}
  9. func (s *roleMenuService) UpdateRoleMenus(roleIds []int, menuIds []int) error {
  10. roleMenu := &dao.RoleMenu{}
  11. err := roleMenu.DeleteRoleIds(roleIds)
  12. if err != nil {
  13. return err
  14. }
  15. var roleMenus []dao.RoleMenu
  16. for _, roleId := range roleIds {
  17. for _, menuId := range menuIds {
  18. roleMenus = append(roleMenus, dao.RoleMenu{
  19. MenuId: menuId,
  20. RoleId: roleId,
  21. })
  22. }
  23. }
  24. return roleMenu.BatchSave(roleMenus)
  25. }
  26. func (s *roleMenuService) GetMenuByRole(roleId int) []dao.RoleMenu {
  27. roleMenu := &dao.RoleMenu{RoleId: roleId}
  28. if common.IsAdmin(roleId) {
  29. menus, err := roleMenu.GetAll()
  30. if err != nil {
  31. return nil
  32. }
  33. return menus
  34. } else {
  35. menus, err := roleMenu.GetMenusByRole()
  36. if err != nil {
  37. return nil
  38. }
  39. return menus
  40. }
  41. }
  42. func (s *roleMenuService) GetMenuIdByRoleId(roleId int) []int {
  43. rmDao := &dao.RoleMenu{RoleId: roleId}
  44. menus, err := rmDao.GetMenusByRole()
  45. if err != nil {
  46. return nil
  47. }
  48. var ids []int
  49. for _, menu := range menus {
  50. ids = append(ids, menu.MenuId)
  51. }
  52. return ids
  53. }