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.

105 lines
3.2 KiB

  1. package sysModel
  2. import (
  3. "fmt"
  4. "gin-vue-admin/controller/servers"
  5. "gin-vue-admin/init/qmsql"
  6. "gin-vue-admin/model/modelInterface"
  7. "github.com/jinzhu/gorm"
  8. "github.com/pkg/errors"
  9. )
  10. type SysBaseMenu struct {
  11. gorm.Model
  12. MenuLevel uint `json:"-"`
  13. ParentId string `json:"parentId"`
  14. Path string `json:"path"`
  15. Name string `json:"name"`
  16. Hidden bool `json:"hidden"`
  17. Component string `json:"component"`
  18. Sort string `json:"sort"`
  19. Meta `json:"meta"`
  20. NickName string `json:"nickName"`
  21. Children []SysBaseMenu `json:"children"`
  22. }
  23. //增加基础路由
  24. func (b *SysBaseMenu) AddBaseMenu() (err error) {
  25. findOne := qmsql.DEFAULTDB.Where("name = ?", b.Name).Find(&SysBaseMenu{}).Error
  26. if findOne != nil {
  27. b.NickName = b.Title
  28. err = qmsql.DEFAULTDB.Create(b).Error
  29. } else {
  30. err = errors.New("存在重复name,请修改name")
  31. }
  32. return err
  33. }
  34. //删除基础路由
  35. func (b *SysBaseMenu) DeleteBaseMenu(id float64) (err error) {
  36. err = qmsql.DEFAULTDB.Where("parent_id = ?", id).First(&SysBaseMenu{}).Error
  37. if err != nil {
  38. err = qmsql.DEFAULTDB.Where("id = ?", id).Delete(&b).Error
  39. err = qmsql.DEFAULTDB.Where("menu_id = ?", id).Unscoped().Delete(&SysMenu{}).Error
  40. } else {
  41. return errors.New("此菜单存在子菜单不可删除")
  42. }
  43. return err
  44. }
  45. //更新路由
  46. func (b *SysBaseMenu) UpdataBaseMenu() (err error) {
  47. upDataMap := make(map[string]interface{})
  48. upDataMap["parent_id"] = b.ParentId
  49. upDataMap["path"] = b.Path
  50. upDataMap["name"] = b.Name
  51. upDataMap["hidden"] = b.Hidden
  52. upDataMap["component"] = b.Component
  53. upDataMap["title"] = b.Title
  54. upDataMap["icon"] = b.Icon
  55. upDataMap["sort"] = b.Sort
  56. upDataMap["nick_name"] = b.Title
  57. err = qmsql.DEFAULTDB.Where("id = ?", b.ID).Find(&SysBaseMenu{}).Updates(upDataMap).Error
  58. err1 := qmsql.DEFAULTDB.Where("menu_id = ?", b.ID).Find(&[]SysMenu{}).Updates(upDataMap).Error
  59. fmt.Printf("菜单修改时候,关联菜单err1:%v,err:%v", err1, err)
  60. return err
  61. }
  62. //当前选中角色所拥有的路由
  63. func (b *SysBaseMenu) GetBaseMenuById(id float64) (err error, menu SysBaseMenu) {
  64. err = qmsql.DEFAULTDB.Where("id = ?", id).First(&menu).Error
  65. return
  66. }
  67. //获取路由分页
  68. func (b *SysBaseMenu) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
  69. // 封装分页方法 调用即可 传入 当前的结构体和分页信息
  70. err, db, total := servers.PagingServer(b, info)
  71. if err != nil {
  72. return
  73. } else {
  74. var menuList []SysBaseMenu
  75. err = db.Where("parent_id = 0").Order("sort", true).Find(&menuList).Error
  76. for i := 0; i < len(menuList); i++ {
  77. err = getBaseChildrenList(&menuList[i])
  78. }
  79. return err, menuList, total
  80. }
  81. }
  82. //获取基础路由树
  83. func (m *SysBaseMenu) GetBaseMenuTree() (err error, menus []SysBaseMenu) {
  84. err = qmsql.DEFAULTDB.Where(" parent_id = ?", 0).Order("sort", true).Find(&menus).Error
  85. for i := 0; i < len(menus); i++ {
  86. err = getBaseChildrenList(&menus[i])
  87. }
  88. return err, menus
  89. }
  90. func getBaseChildrenList(menu *SysBaseMenu) (err error) {
  91. err = qmsql.DEFAULTDB.Where("parent_id = ?", menu.ID).Order("sort", true).Find(&menu.Children).Error
  92. for i := 0; i < len(menu.Children); i++ {
  93. err = getBaseChildrenList(&menu.Children[i])
  94. }
  95. return err
  96. }