You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.5 KiB

  1. package model
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global"
  5. "github.com/jinzhu/gorm"
  6. "github.com/pkg/errors"
  7. )
  8. type SysBaseMenu struct {
  9. gorm.Model
  10. MenuLevel uint `json:"-"`
  11. ParentId string `json:"parentId"`
  12. Path string `json:"path"`
  13. Name string `json:"name"`
  14. Hidden bool `json:"hidden"`
  15. Component string `json:"component"`
  16. Sort int `json:"sort"`
  17. Meta `json:"meta"`
  18. SysAuthoritys []SysAuthority `json:"authoritys" gorm:"many2many:sys_authority_menus;"`
  19. Children []SysBaseMenu `json:"children"`
  20. }
  21. type Meta struct {
  22. Title string `json:"title"`
  23. Icon string `json:"icon"`
  24. }
  25. // @title AddBaseMenu
  26. // @description 函数的详细描述
  27. // @auth (2020/04/05 20:22 )
  28. // @param newPassword string
  29. // @return err error
  30. //增加基础路由
  31. func (b *SysBaseMenu) AddBaseMenu() (err error) {
  32. findOne := global.GVA_DB.Where("name = ?", b.Name).Find(&SysBaseMenu{}).Error
  33. if findOne != nil {
  34. err = global.GVA_DB.Create(b).Error
  35. } else {
  36. err = errors.New("存在重复name,请修改name")
  37. }
  38. return err
  39. }
  40. // @title DeleteBaseMenu
  41. // @description 删除基础路由
  42. // @auth (2020/04/05 20:22 )
  43. // @param newPassword string
  44. // @return err error
  45. func (b *SysBaseMenu) DeleteBaseMenu(id float64) (err error) {
  46. err = global.GVA_DB.Where("parent_id = ?", id).First(&SysBaseMenu{}).Error
  47. if err != nil {
  48. db := global.GVA_DB.Preload("SysAuthoritys").Where("id = ?", id).First(&b).Delete(&b)
  49. if len(b.SysAuthoritys) > 0 {
  50. err = db.Association("SysAuthoritys").Delete(b.SysAuthoritys).Error
  51. } else {
  52. err = db.Error
  53. }
  54. } else {
  55. return errors.New("此菜单存在子菜单不可删除")
  56. }
  57. return err
  58. }
  59. // @title UpdateBaseMenu
  60. // @description 更新路由
  61. // @auth (2020/04/05 20:22 )
  62. // @param newPassword string
  63. // @return err error
  64. func (b *SysBaseMenu) UpdateBaseMenu() (err error) {
  65. upDateMap := make(map[string]interface{})
  66. upDateMap["parent_id"] = b.ParentId
  67. upDateMap["path"] = b.Path
  68. upDateMap["name"] = b.Name
  69. upDateMap["hidden"] = b.Hidden
  70. upDateMap["component"] = b.Component
  71. upDateMap["title"] = b.Title
  72. upDateMap["icon"] = b.Icon
  73. upDateMap["sort"] = b.Sort
  74. err = global.GVA_DB.Where("id = ?", b.ID).Find(&SysBaseMenu{}).Updates(upDateMap).Error
  75. err1 := global.GVA_DB.Where("menu_id = ?", b.ID).Find(&[]SysMenu{}).Updates(upDateMap).Error
  76. fmt.Printf("菜单修改时候,关联菜单err1:%v,err:%v", err1, err)
  77. return err
  78. }
  79. // @title GetBaseMenuById
  80. // @description get current menus, 返回当前选中menu
  81. // @auth (2020/04/05 20:22 )
  82. // @param newPassword string
  83. // @return err error
  84. func (b *SysBaseMenu) GetBaseMenuById(id float64) (err error, menu SysBaseMenu) {
  85. err = global.GVA_DB.Where("id = ?", id).First(&menu).Error
  86. return
  87. }
  88. // @title GetInfoList
  89. // @description 获取路由分页
  90. // @auth (2020/04/05 20:22 )
  91. // @param newPassword string
  92. // @return err error
  93. func (b *SysBaseMenu) GetInfoList(info PageInfo) (err error, list interface{}, total int) {
  94. limit := info.PageSize
  95. offset := info.PageSize * (info.Page - 1)
  96. db := global.GVA_DB
  97. if err != nil {
  98. return
  99. } else {
  100. var menuList []SysBaseMenu
  101. err = db.Limit(limit).Offset(offset).Where("parent_id = 0").Order("sort", true).Find(&menuList).Error
  102. for i := 0; i < len(menuList); i++ {
  103. err = getBaseChildrenList(&menuList[i])
  104. }
  105. return err, menuList, total
  106. }
  107. }
  108. // @title GetBaseMenuTree
  109. // @description 获取基础路由树
  110. // @auth (2020/04/05 20:22 )
  111. // @return err error
  112. // @return menus []SysBaseMenu
  113. func (m *SysBaseMenu) GetBaseMenuTree() (err error, menus []SysBaseMenu) {
  114. err = global.GVA_DB.Where(" parent_id = ?", 0).Order("sort", true).Find(&menus).Error
  115. for i := 0; i < len(menus); i++ {
  116. err = getBaseChildrenList(&menus[i])
  117. }
  118. return err, menus
  119. }
  120. // @title getBaseChildrenList
  121. // @description get children of menu, 获取菜单的子菜单
  122. // @auth (2020/04/05 20:22 )
  123. // @param menu *SysBaseMenu
  124. // @return err error
  125. func getBaseChildrenList(menu *SysBaseMenu) (err error) {
  126. err = global.GVA_DB.Where("parent_id = ?", menu.ID).Order("sort", true).Find(&menu.Children).Error
  127. for i := 0; i < len(menu.Children); i++ {
  128. err = getBaseChildrenList(&menu.Children[i])
  129. }
  130. return err
  131. }