sys_auth_rule.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * @desc:菜单处理
  3. * @company:云南奇讯科技有限公司
  4. * @Author: yixiaohu
  5. * @Date: 2022/3/11 15:07
  6. */
  7. package service
  8. import (
  9. "context"
  10. "github.com/gogf/gf/v2/frame/g"
  11. "github.com/gogf/gf/v2/util/gconv"
  12. "github.com/tiger1103/gfast/v3/internal/app/common/service"
  13. "github.com/tiger1103/gfast/v3/internal/app/system/consts"
  14. "github.com/tiger1103/gfast/v3/internal/app/system/model"
  15. "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao"
  16. "github.com/tiger1103/gfast/v3/library/liberr"
  17. )
  18. type IRule interface {
  19. GetIsMenuStatusList(ctx context.Context) ([]*model.SysAuthRuleInfoRes, error)
  20. GetMenuList(ctx context.Context) (list []*model.SysAuthRuleInfoRes, err error)
  21. GetIsButtonStatusList(ctx context.Context) ([]*model.SysAuthRuleInfoRes, error)
  22. }
  23. type ruleImpl struct {
  24. }
  25. var rule = ruleImpl{}
  26. func Rule() IRule {
  27. return IRule(&rule)
  28. }
  29. // GetIsMenuStatusList 获取isMenu=0|1且status=1的菜单列表
  30. func (s *ruleImpl) GetIsMenuStatusList(ctx context.Context) ([]*model.SysAuthRuleInfoRes, error) {
  31. list, err := s.GetMenuList(ctx)
  32. if err != nil {
  33. return nil, err
  34. }
  35. var gList = make([]*model.SysAuthRuleInfoRes, 0, len(list))
  36. for _, v := range list {
  37. if (v.MenuType == 0 || v.MenuType == 1) && v.Status == 1 {
  38. gList = append(gList, v)
  39. }
  40. }
  41. return gList, nil
  42. }
  43. // GetMenuList 获取所有菜单
  44. func (s *ruleImpl) GetMenuList(ctx context.Context) (list []*model.SysAuthRuleInfoRes, err error) {
  45. cache := service.Cache(ctx)
  46. //从缓存获取
  47. iList := cache.GetOrSetFuncLock(ctx, consts.CacheSysAuthMenu, s.getMenuListFromDb, 0, consts.CacheSysAuthTag)
  48. if iList != nil {
  49. err = gconv.Struct(iList, &list)
  50. }
  51. return
  52. }
  53. // 从数据库获取所有菜单
  54. func (s *ruleImpl) getMenuListFromDb(ctx context.Context) (value interface{}, err error) {
  55. err = g.Try(func() {
  56. var v []*model.SysAuthRuleInfoRes
  57. //从数据库获取
  58. err = dao.SysAuthRule.Ctx(ctx).
  59. Fields(model.SysAuthRuleInfoRes{}).Order("weigh desc,id asc").Scan(&v)
  60. liberr.ErrIsNil(ctx, err, "获取菜单数据失败")
  61. value = v
  62. })
  63. return
  64. }
  65. // GetIsButtonStatusList 获取所有按钮isMenu=2 且status=1的菜单列表
  66. func (s *ruleImpl) GetIsButtonStatusList(ctx context.Context) ([]*model.SysAuthRuleInfoRes, error) {
  67. list, err := s.GetMenuList(ctx)
  68. if err != nil {
  69. return nil, err
  70. }
  71. var gList = make([]*model.SysAuthRuleInfoRes, 0, len(list))
  72. for _, v := range list {
  73. if v.MenuType == 2 && v.Status == 1 {
  74. gList = append(gList, v)
  75. }
  76. }
  77. return gList, nil
  78. }