auth_rule.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package auth_rule
  2. import (
  3. "gfast/app/service/cache_service"
  4. "gfast/library/utils"
  5. "github.com/gogf/gf/frame/g"
  6. "github.com/gogf/gf/os/gtime"
  7. "github.com/gogf/gf/util/gconv"
  8. )
  9. //菜单对象
  10. type MenuReq struct {
  11. IsMenu int `p:"ismenu" c:"ismenu" v:"min:0|max:1#菜单类型最小值为:min|菜单类型最大值为:max"`
  12. Pid int `p:"pid" c:"pid" v:"min:0"`
  13. Name string `p:"name" c:"name" v:"required#请填写规则名称"`
  14. Title string `p:"title" c:"title" v:"required|length:1,100#请填写标题|标题长度在:min到:max位"`
  15. Icon string `p:"icon" c:"icon"`
  16. Weigh int `p:"weigh" c:"weigh"`
  17. Condition string `p:"condition" c:"condition"`
  18. Remark string `p:"remark" c:"remark"`
  19. Status int `p:"status" c:"status"`
  20. }
  21. //获取所有菜单
  22. func GetMenuList() (list []*Entity, err error) {
  23. cache := cache_service.New()
  24. //从缓存获取
  25. iList := cache.Get(cache_service.AdminAuthMenu)
  26. if iList != nil {
  27. list = iList.([]*Entity)
  28. return
  29. }
  30. //从数据库获取
  31. list, err = Model.Order("weigh desc,id asc").FindAll()
  32. if err != nil {
  33. return
  34. }
  35. //缓存菜单
  36. cache.Set(cache_service.AdminAuthMenu, list, 0, cache_service.AdminAuthTag)
  37. return
  38. }
  39. //检查菜单规则是否存在
  40. func CheckMenuNameUnique(name string, id int) bool {
  41. model := Model.Where("name=?", name)
  42. if id != 0 {
  43. model = model.And("id!=?", id)
  44. }
  45. c, err := model.Count()
  46. if err != nil {
  47. g.Log().Error(err)
  48. return false
  49. }
  50. return c == 0
  51. }
  52. // 添加菜单操作
  53. func Add(req *MenuReq) (err error, insertId int64) {
  54. menuMap := gconv.Map(req)
  55. now := gtime.Timestamp()
  56. menuMap["createtime"] = now
  57. menuMap["updatetime"] = now
  58. res, e := Model.Insert(menuMap)
  59. err = e
  60. insertId, _ = res.LastInsertId()
  61. return
  62. }
  63. //修改菜单操作
  64. func Edit(req *MenuReq, id int) (err error, rows int64) {
  65. menuMap := gconv.Map(req)
  66. now := gtime.Timestamp()
  67. menuMap["updatetime"] = now
  68. res, e := Model.Where("id=?", id).Update(menuMap)
  69. err = e
  70. rows, _ = res.RowsAffected()
  71. return
  72. }
  73. //删除菜单
  74. func DeleteByIds(ids []int) (err error) {
  75. //获取菜单数据
  76. menus, err := GetMenuList()
  77. if err != nil {
  78. return
  79. }
  80. menuList := gconv.SliceMap(menus)
  81. son := make(g.List, 0, len(menuList))
  82. for _, id := range ids {
  83. son = append(son, utils.FindSonByParentId(menuList, id, "pid", "id")...)
  84. }
  85. for _, v := range son {
  86. ids = append(ids, gconv.Int(v["id"]))
  87. }
  88. _, err = Model.Where("id in (?)", ids).Delete()
  89. return
  90. }