sys_role.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * @desc:角色处理
  3. * @company:云南奇讯科技有限公司
  4. * @Author: yixiaohu
  5. * @Date: 2022/3/9 10:31
  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. commonService "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/entity"
  15. "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao"
  16. "github.com/tiger1103/gfast/v3/library/liberr"
  17. )
  18. type IRole interface {
  19. GetRoleList(ctx context.Context) (list []*entity.SysRole, err error)
  20. }
  21. type roleImpl struct {
  22. }
  23. var role = roleImpl{}
  24. func Role() IRole {
  25. return IRole(&role)
  26. }
  27. // GetRoleList 获取角色列表
  28. func (s *roleImpl) GetRoleList(ctx context.Context) (list []*entity.SysRole, err error) {
  29. cache := commonService.Cache(ctx)
  30. //从缓存获取
  31. iList := cache.GetOrSetFuncLock(ctx, consts.CacheSysRole, s.getRoleListFromDb, 0, consts.CacheSysAuthTag)
  32. if iList != nil {
  33. err = gconv.Struct(iList, &list)
  34. }
  35. return
  36. }
  37. // 从数据库获取所有角色
  38. func (s *roleImpl) getRoleListFromDb(ctx context.Context) (value interface{}, err error) {
  39. err = g.Try(func() {
  40. var v []*entity.SysRole
  41. //从数据库获取
  42. err = dao.SysRole.Ctx(ctx).
  43. Order(dao.SysRole.Columns().ListOrder + " asc," + dao.SysRole.Columns().Id + " asc").
  44. Scan(&v)
  45. liberr.ErrIsNil(ctx, err, "获取角色数据失败")
  46. value = v
  47. })
  48. return
  49. }