sys_role.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/errors/gerror"
  11. "github.com/gogf/gf/v2/frame/g"
  12. "github.com/gogf/gf/v2/util/gconv"
  13. commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
  14. "github.com/tiger1103/gfast/v3/internal/app/system/consts"
  15. "github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
  16. "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao"
  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.SysRole, func(ctx context.Context) (value interface{}, err error) {
  32. var v []*entity.SysRole
  33. //从数据库获取
  34. err = dao.SysRole.Ctx(ctx).
  35. Order(dao.SysRole.Columns().ListOrder + " asc," + dao.SysRole.Columns().Id + " asc").
  36. Scan(&v)
  37. if err != nil {
  38. g.Log().Error(ctx, err)
  39. err = gerror.New("获取角色数据失败")
  40. }
  41. value = v
  42. return
  43. }, 0, consts.SysAuthTag)
  44. if iList != nil {
  45. err = gconv.Struct(iList, &list)
  46. }
  47. return
  48. }