sys_auth_rule.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. }
  22. type ruleImpl struct {
  23. }
  24. var rule = ruleImpl{}
  25. func Rule() IRule {
  26. return IRule(&rule)
  27. }
  28. // GetIsMenuStatusList 获取isMenu=0|1且status=1的菜单列表
  29. func (s *ruleImpl) GetIsMenuStatusList(ctx context.Context) ([]*model.SysAuthRuleInfoRes, error) {
  30. list, err := s.GetMenuList(ctx)
  31. if err != nil {
  32. return nil, err
  33. }
  34. var gList = make([]*model.SysAuthRuleInfoRes, 0, len(list))
  35. for _, v := range list {
  36. if (v.MenuType == 0 || v.MenuType == 1) && v.Status == 1 {
  37. gList = append(gList, v)
  38. }
  39. }
  40. return gList, nil
  41. }
  42. // GetMenuList 获取所有菜单
  43. func (s *ruleImpl) GetMenuList(ctx context.Context) (list []*model.SysAuthRuleInfoRes, err error) {
  44. cache := service.Cache(ctx)
  45. //从缓存获取
  46. iList := cache.GetOrSetFuncLock(ctx, consts.CacheSysAuthMenu, s.getMenuListFromDb, 0, consts.CacheSysAuthTag)
  47. if iList != nil {
  48. err = gconv.Struct(iList, &list)
  49. }
  50. return
  51. }
  52. // 从数据库获取所有菜单
  53. func (s *ruleImpl) getMenuListFromDb(ctx context.Context) (value interface{}, err error) {
  54. err = g.Try(func() {
  55. var v []*model.SysAuthRuleInfoRes
  56. //从数据库获取
  57. err = dao.SysAuthRule.Ctx(ctx).
  58. Fields(model.SysAuthRuleInfoRes{}).Order("weigh desc,id asc").Scan(&v)
  59. liberr.ErrIsNil(ctx, err, "获取菜单数据失败")
  60. value = v
  61. })
  62. return
  63. }