terry 1 anno fa
parent
commit
3c4237f7de
2 ha cambiato i file con 37 aggiunte e 3 eliminazioni
  1. 10 0
      app/system/model/menu.go
  2. 27 3
      app/system/service/menuService.go

+ 10 - 0
app/system/model/menu.go

@@ -16,6 +16,16 @@ type MenuRouteDetail struct {
 	Children    []MenuRouteDetail `json:"children"`
 }
 
+type MenuTreeDetail struct {
+	ID          string           `json:"id"`
+	ParentId    string           `json:"parentId"`
+	HasChildren bool             `json:"hasChildren"`
+	Title       string           `json:"title"`
+	Key         string           `json:"key"`
+	Value       string           `json:"value"`
+	Children    []MenuTreeDetail `json:"children"`
+}
+
 type RsqMenuLazyList struct {
 	List []MenuLazyListDetail `json:"data"`
 }

+ 27 - 3
app/system/service/menuService.go

@@ -5,6 +5,7 @@ import (
 	"iot_manager_service/app/system/model"
 	"iot_manager_service/util/common"
 	"sort"
+	"strconv"
 )
 
 var MenuService = new(menuService)
@@ -47,9 +48,9 @@ func (s *menuService) GetAll() []dao.Menu {
 	return menus
 }
 
-func (s *menuService) Tree() ([]model.MenuRouteDetail, error) {
+func (s *menuService) Tree() ([]model.MenuTreeDetail, error) {
 	allMenus := s.GetAll()
-	var rsp []model.MenuRouteDetail
+	var rsp []model.MenuTreeDetail
 	//上级角色——多个下级角色数组
 	tmp := make(map[int64][]dao.Menu)
 	for _, menu := range allMenus {
@@ -66,7 +67,7 @@ func (s *menuService) Tree() ([]model.MenuRouteDetail, error) {
 		return nil, nil
 	}
 	roots, _ := tmp[0]
-	rsp = menuDfs(rsp, roots, tmp)
+	rsp = menuTreeDfs(rsp, roots, tmp)
 
 	return rsp, nil
 }
@@ -129,6 +130,29 @@ func menuDfs(result []model.MenuRouteDetail, menus []dao.Menu, tmp map[int64][]d
 	return result
 }
 
+func menuTreeDfs(result []model.MenuTreeDetail, menus []dao.Menu, tmp map[int64][]dao.Menu) []model.MenuTreeDetail {
+	if len(menus) == 0 {
+		return nil
+	}
+	for _, child := range menus {
+		detail := model.MenuTreeDetail{}
+		detail.Title = child.Name
+		detail.ID = strconv.FormatInt(child.ID, 10)
+		detail.Key = strconv.FormatInt(child.ID, 10)
+		detail.Value = strconv.FormatInt(child.ID, 10)
+		childes, isExist := tmp[child.ID]
+		if isExist {
+			detail.HasChildren = true
+			detail.Children = menuTreeDfs([]model.MenuTreeDetail{}, childes, tmp)
+			result = append(result, detail)
+		} else {
+			detail.HasChildren = false
+			result = append(result, detail)
+		}
+	}
+	return result
+}
+
 func (s *menuService) LazyList(parentId int64, name, code, alias string) ([]model.MenuLazyListDetail, error) {
 	menu := &dao.Menu{
 		ParentId: parentId,