system_menu.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package model
  2. import (
  3. "sort"
  4. "time"
  5. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  6. "go.uber.org/zap"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  9. )
  10. type SystemMenu struct {
  11. Id int64 `json:"id"`
  12. Name string `json:"name"`
  13. Title string `json:"title"`
  14. Path string `json:"path"`
  15. Icon string `json:"icon"`
  16. Component string `json:"component"`
  17. Redirect string `json:"redirect"`
  18. Auths string `json:"auths"`
  19. MenuType pasturePb.MenuType_Kind `json:"menu_type"`
  20. ParentId int64 `json:"parent_id"`
  21. Rank int32 `json:"rank"`
  22. ExtraIcon string `json:"extra_icon"`
  23. EnterTransition string `json:"enter_transition"`
  24. ActivePath string `json:"active_path"`
  25. FrameSrc string `json:"frame_src"`
  26. FrameLoading pasturePb.IsShow_Kind `json:"frame_loading"`
  27. Keepalive pasturePb.IsShow_Kind `json:"keepalive"`
  28. HiddenTag pasturePb.IsShow_Kind `json:"hidden_tag"`
  29. ShowLink pasturePb.IsShow_Kind `json:"show_link"`
  30. ShowParent pasturePb.IsShow_Kind `json:"show_parent"`
  31. IsShow pasturePb.IsShow_Kind `json:"is_show"`
  32. IsDelete pasturePb.IsShow_Kind `json:"is_delete"`
  33. CreatedAt int64 `json:"created_at"`
  34. UpdatedAt int64 `json:"updated_at"`
  35. }
  36. func (s *SystemMenu) TableName() string {
  37. return "system_menu"
  38. }
  39. func NewSystemMenu(req *operationPb.AddMenuRequest) *SystemMenu {
  40. return &SystemMenu{
  41. Name: req.Name,
  42. Title: req.Title,
  43. Path: req.Path,
  44. Component: req.Component,
  45. Icon: req.Icon,
  46. Redirect: req.Redirect,
  47. ParentId: int64(req.ParentId),
  48. IsShow: pasturePb.IsShow_Ok,
  49. IsDelete: pasturePb.IsShow_Ok,
  50. }
  51. }
  52. type SystemMenuSlice []*SystemMenu
  53. func (s SystemMenuSlice) ToPB() []*pasturePb.SearchMenuRequest {
  54. res := make([]*pasturePb.SearchMenuRequest, len(s))
  55. for i, menu := range s {
  56. var frameLoading, keepalive, hiddenTag, showLink, showParent bool
  57. if menu.FrameLoading == pasturePb.IsShow_Ok {
  58. frameLoading = true
  59. }
  60. if menu.Keepalive == pasturePb.IsShow_Ok {
  61. keepalive = true
  62. }
  63. if menu.HiddenTag == pasturePb.IsShow_Ok {
  64. hiddenTag = true
  65. }
  66. if menu.ShowLink == pasturePb.IsShow_Ok {
  67. showLink = true
  68. }
  69. if menu.ShowParent == pasturePb.IsShow_Ok {
  70. showParent = true
  71. }
  72. res[i] = &pasturePb.SearchMenuRequest{
  73. Id: int32(menu.Id),
  74. Name: menu.Name,
  75. ParentId: int32(menu.ParentId),
  76. MenuType: menu.MenuType,
  77. Title: menu.Title,
  78. Path: menu.Path,
  79. IsShow: menu.IsShow,
  80. Component: menu.Component,
  81. Icon: menu.Icon,
  82. Redirect: menu.Redirect,
  83. Auths: menu.Auths,
  84. Rank: menu.Rank,
  85. ExtraIcon: menu.ExtraIcon,
  86. EnterTransition: menu.EnterTransition,
  87. ActivePath: menu.ActivePath,
  88. FrameSrc: menu.FrameSrc,
  89. FrameLoading: frameLoading,
  90. Keepalive: keepalive,
  91. HiddenTag: hiddenTag,
  92. ShowLink: showLink,
  93. ShowParent: showParent,
  94. CreatedAtFormat: time.Unix(menu.CreatedAt, 0).Format(LayoutTime),
  95. UpdatedAtFormat: time.Unix(menu.UpdatedAt, 0).Format(LayoutTime),
  96. }
  97. }
  98. return res
  99. }
  100. func (s SystemMenuSlice) ToTree() []*pasturePb.MenuTree {
  101. res := make([]*pasturePb.MenuTree, len(s))
  102. if 0 == len(s) {
  103. return res
  104. }
  105. // 正排序
  106. sort.Slice(s, func(i, j int) bool {
  107. return s[i].ParentId < s[j].ParentId
  108. })
  109. for i, v := range s {
  110. res[i] = &pasturePb.MenuTree{
  111. Id: int32(v.Id),
  112. Path: v.Path,
  113. ParentId: int32(v.ParentId),
  114. Meta: &pasturePb.MenuMeta{
  115. Icon: v.Icon,
  116. Title: v.Title,
  117. Rank: v.Rank,
  118. },
  119. Children: make([]*pasturePb.MenuTree, 0),
  120. }
  121. }
  122. // 构建树
  123. return buildTree(res)
  124. }
  125. func buildTree(menuTreeList []*pasturePb.MenuTree) []*pasturePb.MenuTree {
  126. tree := make([]*pasturePb.MenuTree, 0)
  127. for _, menu := range menuTreeList {
  128. if menu.ParentId == 0 {
  129. tree = append(tree, menu)
  130. } else {
  131. parent := findByID(menuTreeList, menu.ParentId)
  132. if parent != nil {
  133. parent.Children = append(parent.Children, menu)
  134. }
  135. }
  136. }
  137. zaplog.Info("tree", zap.Any("tree", tree))
  138. return tree
  139. }
  140. func findByID(menuTreeList []*pasturePb.MenuTree, id int32) *pasturePb.MenuTree {
  141. for _, v := range menuTreeList {
  142. if id == v.Id {
  143. return v
  144. }
  145. if child := findByID(v.Children, id); child != nil {
  146. return child
  147. }
  148. }
  149. return nil
  150. }
  151. func BooleanToIsShow(isShow bool) pasturePb.IsShow_Kind {
  152. showKind := pasturePb.IsShow_No
  153. if isShow {
  154. showKind = pasturePb.IsShow_Ok
  155. }
  156. return showKind
  157. }