s_menu.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package schema
  2. import "time"
  3. // Menu Menu对象
  4. type Menu struct {
  5. RecordID string `json:"record_id"` // 记录id
  6. Name string `json:"name"` // 名称
  7. Path string `json:"path"` // 路径
  8. Component string `json:"component"` // 组件
  9. Meta MenuMeta `json:"meta"` // 元数据
  10. ParentId string `json:"parent_id"` // 父级id
  11. Sequence int `json:"sequence"` // 排序值
  12. CreatedAt time.Time `json:"created_at"` // 创建时间
  13. CreatorId string `json:"creator_id"` // 创建者id
  14. CreatorName string `json:"creator_name"` // 创建者名称
  15. }
  16. type MenuMeta struct {
  17. Icon string `json:"icon"` // 图标
  18. Title string `json:"title"` // 标题
  19. IsLink string `json:"isLink"` // 是否外链
  20. IsHide bool `json:"isHide"` // 是否隐藏
  21. IsFull bool `json:"isFull"` // 是否全屏
  22. IsAffix bool `json:"isAffix"` // 是否固定
  23. IsKeepAlive bool `json:"isKeepAlive"` // 是否缓存
  24. }
  25. // MenuQueryParam 查询条件
  26. type MenuQueryParam struct {
  27. LikeName string
  28. RecordIDs []string
  29. }
  30. // MenuQueryOptions Menu对象查询可选参数项
  31. type MenuQueryOptions struct {
  32. PageParam *PaginationParam // 分页参数
  33. }
  34. type Menus []*Menu
  35. // MenuQueryResult Menu对象查询结果
  36. type MenuQueryResult struct {
  37. Data Menus
  38. PageResult *PaginationResult
  39. }
  40. // MenuTree 菜单树
  41. type MenuTree struct {
  42. RecordID string `json:"record_id"`
  43. Name string `json:"name" binding:"required" `
  44. Path string `json:"path"` // 路径
  45. Component string `json:"component"` // 组件
  46. Meta MenuMeta `json:"meta"` // 元数据
  47. ParentId string `json:"parent_id"` // 父级id
  48. Children MenuTrees `json:"children"` // 子级
  49. }
  50. // MenuTrees 菜单树列表
  51. type MenuTrees []*MenuTree
  52. func ToTree(List MenuTrees, pid string) MenuTrees {
  53. var newArr MenuTrees
  54. for _, v := range List {
  55. if v.ParentId == pid {
  56. v.Children = ToTree(List, v.RecordID)
  57. newArr = append(newArr, v)
  58. }
  59. }
  60. return newArr
  61. }
  62. // ToTrees 转换为菜单列表
  63. func (a Menus) ToTrees() MenuTrees {
  64. list := make(MenuTrees, len(a))
  65. for i, item := range a {
  66. list[i] = &MenuTree{
  67. RecordID: item.RecordID,
  68. Name: item.Name,
  69. Path: item.Path,
  70. Component: item.Component,
  71. Meta: item.Meta,
  72. ParentId: item.ParentId,
  73. }
  74. }
  75. return list
  76. }
  77. // FillCreator 填充创建者信息
  78. func (a Menus) FillCreator(users Users) {
  79. for _, o := range a {
  80. for _, u := range users {
  81. if o.CreatorId == u.RecordID {
  82. o.CreatorName = u.RealName
  83. continue
  84. }
  85. }
  86. }
  87. }