Golang示例(beego)

Golang动态生成初始化数据(Beego框架)

对应控制器 controllers/IndexController.go

  1. package controllers
  2. import (
  3. "BeegoAdmin/models"
  4. "github.com/astaxie/beego"
  5. )
  6. type IndexController struct {
  7. beego.Controller
  8. }
  9. // 初始化后台框架接口
  10. func (c *IndexController) SystemInit() {
  11. systemInit := new(models.SystemMenu).GetSystemInit()
  12. c.Data["json"] = systemInit
  13. c.ServeJSON()
  14. }

对应Model models/SystemMenu.go

  1. package models
  2. import (
  3. "github.com/astaxie/beego/orm"
  4. "time"
  5. )
  6. // 菜单
  7. type SystemMenu struct {
  8. Id int `json:"id"`
  9. Pid int `json:"pid"`
  10. Title string `json:"title"`
  11. Icon string `json:"icon"`
  12. Href string `json:"href"`
  13. Sort string `json:"sort"`
  14. Target string `json:"target"`
  15. Remark string `json:"remark"`
  16. Status int `json:"status"`
  17. CreateAt time.Time `json:"create_at";orm:"auto_now;type(datetime)"`
  18. }
  19. func (m *SystemMenu) TableName() string {
  20. return TableName("system_menu")
  21. }
  22. // 初始化结构体
  23. type SystemInit struct {
  24. HomeInfo struct {
  25. Title string `json:"title"`
  26. Href string `json:"href"`
  27. } `json:"homeInfo"`
  28. LogoInfo struct {
  29. Title string `json:"title"`
  30. Image string `json:"image"`
  31. } `json:"logoInfo"`
  32. MenuInfo []*MenuTreeList `json:"menuInfo"`
  33. }
  34. // 菜单结构体
  35. type MenuTreeList struct {
  36. Id int `json:"id"`
  37. Pid int `json:"pid"`
  38. Title string `json:"title"`
  39. Icon string `json:"icon"`
  40. Href string `json:"href"`
  41. Target string `json:"target"`
  42. Remark string `json:"remark"`
  43. Child []*MenuTreeList `json:"child"`
  44. }
  45. // 获取初始化数据
  46. func (m *SystemMenu) GetSystemInit() SystemInit {
  47. var systemInit SystemInit
  48. // 首页
  49. systemInit.HomeInfo.Title = "首页"
  50. systemInit.HomeInfo.Href = "page/welcome-1.html?t=1"
  51. // logo
  52. systemInit.LogoInfo.Title = "LAYUI MINI"
  53. systemInit.LogoInfo.Image = "images/logo.png"
  54. // 菜单
  55. systemInit.MenuInfo = m.GetMenuList()
  56. return systemInit
  57. }
  58. // 获取菜单列表
  59. func (m *SystemMenu) GetMenuList() []*MenuTreeList {
  60. o := orm.NewOrm()
  61. var menuList []SystemMenu
  62. _, _ = o.QueryTable(m.TableName()).Filter("status",1).OrderBy("-sort").All(&menuList)
  63. return m.buildMenuChild(0, menuList)
  64. }
  65. //递归获取子菜单
  66. func (m *SystemMenu) buildMenuChild(pid int, menuList []SystemMenu) []*MenuTreeList {
  67. var treeList []*MenuTreeList
  68. for _, v := range menuList {
  69. if pid == v.Pid {
  70. node := &MenuTreeList{
  71. Id: v.Id,
  72. Title: v.Title,
  73. Icon: v.Icon,
  74. Href: v.Href,
  75. Target: v.Target,
  76. Pid: v.Pid,
  77. }
  78. child := v.buildMenuChild(v.Id, menuList)
  79. if len(child) != 0 {
  80. node.Child = child
  81. }
  82. // todo 后续此处加上用户的权限判断
  83. treeList = append(treeList, node)
  84. }
  85. }
  86. return treeList
  87. }

© zhongshaofa all right reserved,powered by Gitbook文件修订时间: 2021-04-06 22:10:57

results matching ""

No results matching ""

results matching ""

No results matching ""