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.

110 lines
3.2 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 string `json:"sort"`
  17. Meta `json:"meta"`
  18. NickName string `json:"nickName"`
  19. SysAuthoritys []SysAuthority `json:"authoritys" gorm:"many2many:sys_authority_menus;"`
  20. Children []SysBaseMenu `json:"children"`
  21. }
  22. type Meta struct {
  23. Title string `json:"title"`
  24. Icon string `json:"icon"`
  25. }
  26. //增加基础路由
  27. func (b *SysBaseMenu) AddBaseMenu() (err error) {
  28. findOne := global.GVA_DB.Where("name = ?", b.Name).Find(&SysBaseMenu{}).Error
  29. if findOne != nil {
  30. b.NickName = b.Title
  31. err = global.GVA_DB.Create(b).Error
  32. } else {
  33. err = errors.New("存在重复name,请修改name")
  34. }
  35. return err
  36. }
  37. //删除基础路由
  38. func (b *SysBaseMenu) DeleteBaseMenu(id float64) (err error) {
  39. err = global.GVA_DB.Where("parent_id = ?", id).First(&SysBaseMenu{}).Error
  40. if err != nil {
  41. err = global.GVA_DB.Where("id = ?", id).Delete(&b).Error
  42. err = global.GVA_DB.Where("menu_id = ?", id).Unscoped().Delete(&SysMenu{}).Error
  43. } else {
  44. return errors.New("此菜单存在子菜单不可删除")
  45. }
  46. return err
  47. }
  48. //更新路由
  49. func (b *SysBaseMenu) UpdateBaseMenu() (err error) {
  50. upDateMap := make(map[string]interface{})
  51. upDateMap["parent_id"] = b.ParentId
  52. upDateMap["path"] = b.Path
  53. upDateMap["name"] = b.Name
  54. upDateMap["hidden"] = b.Hidden
  55. upDateMap["component"] = b.Component
  56. upDateMap["title"] = b.Title
  57. upDateMap["icon"] = b.Icon
  58. upDateMap["sort"] = b.Sort
  59. upDateMap["nick_name"] = b.Title
  60. err = global.GVA_DB.Where("id = ?", b.ID).Find(&SysBaseMenu{}).Updates(upDateMap).Error
  61. err1 := global.GVA_DB.Where("menu_id = ?", b.ID).Find(&[]SysMenu{}).Updates(upDateMap).Error
  62. fmt.Printf("菜单修改时候,关联菜单err1:%v,err:%v", err1, err)
  63. return err
  64. }
  65. //当前选中角色所拥有的路由
  66. func (b *SysBaseMenu) GetBaseMenuById(id float64) (err error, menu SysBaseMenu) {
  67. err = global.GVA_DB.Where("id = ?", id).First(&menu).Error
  68. return
  69. }
  70. //获取路由分页
  71. func (b *SysBaseMenu) GetInfoList(info PageInfo) (err error, list interface{}, total int) {
  72. limit := info.PageSize
  73. offset := info.PageSize * (info.Page - 1)
  74. db := global.GVA_DB
  75. if err != nil {
  76. return
  77. } else {
  78. var menuList []SysBaseMenu
  79. err = db.Limit(limit).Offset(offset).Where("parent_id = 0").Order("sort", true).Find(&menuList).Error
  80. for i := 0; i < len(menuList); i++ {
  81. err = getBaseChildrenList(&menuList[i])
  82. }
  83. return err, menuList, total
  84. }
  85. }
  86. //获取基础路由树
  87. func (m *SysBaseMenu) GetBaseMenuTree() (err error, menus []SysBaseMenu) {
  88. err = global.GVA_DB.Where(" parent_id = ?", 0).Order("sort", true).Find(&menus).Error
  89. for i := 0; i < len(menus); i++ {
  90. err = getBaseChildrenList(&menus[i])
  91. }
  92. return err, menus
  93. }
  94. func getBaseChildrenList(menu *SysBaseMenu) (err error) {
  95. err = global.GVA_DB.Where("parent_id = ?", menu.ID).Order("sort", true).Find(&menu.Children).Error
  96. for i := 0; i < len(menu.Children); i++ {
  97. err = getBaseChildrenList(&menu.Children[i])
  98. }
  99. return err
  100. }