auth_rule.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package auth_rule
  2. import (
  3. "gfast/app/service/cache_service"
  4. "gfast/library/utils"
  5. "github.com/gogf/gf/errors/gerror"
  6. "github.com/gogf/gf/frame/g"
  7. "github.com/gogf/gf/os/gtime"
  8. "github.com/gogf/gf/util/gconv"
  9. )
  10. //菜单对象
  11. type MenuReq struct {
  12. MenuType uint `p:"menuType" v:"min:0|max:2#菜单类型最小值为:min|菜单类型最大值为:max"`
  13. Pid uint `p:"parentId" v:"min:0"`
  14. Name string `p:"component" v:"required#请填写规则名称"`
  15. Title string `p:"menuName" v:"required|length:1,100#请填写标题|标题长度在:min到:max位"`
  16. Icon string `p:"icon"`
  17. Weigh int `p:"orderNum" `
  18. Condition string `p:"condition" `
  19. Remark string `p:"remark" `
  20. Status uint `p:"status" `
  21. AlwaysShow uint `p:"visible"`
  22. Path string `p:"path"`
  23. IsFrame uint `p:"is_frame"`
  24. ModuleType string `p:"moduleType" v:required#所属模块不能为空`
  25. ModelId uint `p:"modelId"`
  26. }
  27. //获取所有菜单
  28. func GetMenuList() (list []*Entity, err error) {
  29. cache := cache_service.New()
  30. //从缓存获取
  31. iList := cache.Get(cache_service.AdminAuthMenu)
  32. if iList != nil {
  33. list = iList.([]*Entity)
  34. return
  35. }
  36. //从数据库获取
  37. list, err = Model.Order("weigh desc,id asc").FindAll()
  38. if err != nil {
  39. return
  40. }
  41. //缓存菜单
  42. cache.Set(cache_service.AdminAuthMenu, list, 0, cache_service.AdminAuthTag)
  43. return
  44. }
  45. //检查菜单规则是否存在
  46. func CheckMenuNameUnique(name string, id int) bool {
  47. model := Model.Where("name=?", name)
  48. if id != 0 {
  49. model = model.And("id!=?", id)
  50. }
  51. c, err := model.Count()
  52. if err != nil {
  53. g.Log().Error(err)
  54. return false
  55. }
  56. return c == 0
  57. }
  58. //检查菜单路由地址是否已经存在
  59. func CheckMenuPathUnique(path string, id int) bool {
  60. model := Model.Where("path=?", path).Where("menu_type<>?", 2)
  61. if id != 0 {
  62. model = model.And("id!=?", id)
  63. }
  64. c, err := model.Count()
  65. if err != nil {
  66. g.Log().Error(err)
  67. return false
  68. }
  69. return c == 0
  70. }
  71. // 添加菜单操作
  72. func Add(req *MenuReq) (err error, insertId int64) {
  73. if req == nil {
  74. err = gerror.New("参数错误")
  75. return
  76. }
  77. now := gtime.Timestamp()
  78. entity := new(Entity)
  79. entity.Title = req.Title
  80. entity.Status = req.Status
  81. entity.MenuType = req.MenuType
  82. entity.Path = req.Path
  83. entity.AlwaysShow = req.AlwaysShow
  84. entity.Icon = req.Icon
  85. entity.Name = req.Name
  86. entity.IsFrame = req.IsFrame
  87. entity.ModuleType = req.ModuleType
  88. entity.ModelId = req.ModelId
  89. entity.Pid = req.Pid
  90. entity.Createtime = gconv.Uint(now)
  91. entity.Updatetime = gconv.Uint(now)
  92. entity.Weigh = req.Weigh
  93. res, e := entity.Insert()
  94. err = e
  95. if err != nil {
  96. return
  97. }
  98. insertId, err = res.LastInsertId()
  99. return
  100. }
  101. //修改菜单操作
  102. func Edit(req *MenuReq, id int) (err error, rows int64) {
  103. var entity *Entity
  104. entity, err = Model.FindOne(id)
  105. if err != nil {
  106. return
  107. }
  108. now := gtime.Timestamp()
  109. entity.Updatetime = gconv.Uint(now)
  110. entity.Title = req.Title
  111. entity.Status = req.Status
  112. entity.MenuType = req.MenuType
  113. entity.Path = req.Path
  114. entity.AlwaysShow = req.AlwaysShow
  115. entity.Icon = req.Icon
  116. entity.Name = req.Name
  117. entity.IsFrame = req.IsFrame
  118. entity.ModuleType = req.ModuleType
  119. entity.ModelId = req.ModelId
  120. entity.Pid = req.Pid
  121. entity.Weigh = req.Weigh
  122. res, e := entity.Update()
  123. err = e
  124. if err != nil {
  125. return
  126. }
  127. rows, err = res.RowsAffected()
  128. return
  129. }
  130. //删除菜单
  131. func DeleteByIds(ids []int) (err error) {
  132. //获取菜单数据
  133. menus, err := GetMenuList()
  134. if err != nil {
  135. return
  136. }
  137. menuList := gconv.SliceMap(menus)
  138. son := make(g.List, 0, len(menuList))
  139. for _, id := range ids {
  140. son = append(son, utils.FindSonByParentId(menuList, id, "pid", "id")...)
  141. }
  142. for _, v := range son {
  143. ids = append(ids, gconv.Int(v["id"]))
  144. }
  145. _, err = Model.Where("id in (?)", ids).Delete()
  146. return
  147. }
  148. type ReqSearch struct {
  149. Status string `p:"status" `
  150. Title string `p:"menuName" `
  151. }