system_menu.go 4.7 KB

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