sys_role.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/database/gdb"
  11. "github.com/gogf/gf/v2/frame/g"
  12. "github.com/gogf/gf/v2/util/gconv"
  13. "github.com/tiger1103/gfast/v3/api/v1/system"
  14. commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
  15. "github.com/tiger1103/gfast/v3/internal/app/system/consts"
  16. "github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
  17. "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao"
  18. "github.com/tiger1103/gfast/v3/library/liberr"
  19. )
  20. type IRole interface {
  21. GetRoleList(ctx context.Context) (list []*entity.SysRole, err error)
  22. GetRoleListSearch(ctx context.Context, req *system.RoleListReq) (res *system.RoleListRes, err error)
  23. AddRole(ctx context.Context, req *system.RoleAddReq) (err error)
  24. }
  25. type roleImpl struct {
  26. }
  27. var role = roleImpl{}
  28. func Role() IRole {
  29. return IRole(&role)
  30. }
  31. func (s *roleImpl) GetRoleListSearch(ctx context.Context, req *system.RoleListReq) (res *system.RoleListRes, err error) {
  32. res = new(system.RoleListRes)
  33. g.Try(func() {
  34. model := dao.SysRole.Ctx(ctx)
  35. if req.RoleName != "" {
  36. model = model.Where("name like ?", "%"+req.RoleName+"%")
  37. }
  38. if req.Status != "" {
  39. model = model.Where("status", gconv.Int(req.Status))
  40. }
  41. res.Total, err = model.Count()
  42. liberr.ErrIsNil(ctx, err, "获取角色数据失败")
  43. if req.PageNum == 0 {
  44. req.PageNum = 1
  45. }
  46. res.CurrentPage = req.PageNum
  47. if req.PageSize == 0 {
  48. req.PageSize = consts.PageSize
  49. }
  50. err = model.Page(res.CurrentPage, req.PageSize).Order("id asc").Scan(&res.List)
  51. liberr.ErrIsNil(ctx, err, "获取数据失败")
  52. })
  53. return
  54. }
  55. // GetRoleList 获取角色列表
  56. func (s *roleImpl) GetRoleList(ctx context.Context) (list []*entity.SysRole, err error) {
  57. cache := commonService.Cache()
  58. //从缓存获取
  59. iList := cache.GetOrSetFuncLock(ctx, consts.CacheSysRole, s.getRoleListFromDb, 0, consts.CacheSysAuthTag)
  60. if iList != nil {
  61. err = gconv.Struct(iList, &list)
  62. }
  63. return
  64. }
  65. // 从数据库获取所有角色
  66. func (s *roleImpl) getRoleListFromDb(ctx context.Context) (value interface{}, err error) {
  67. err = g.Try(func() {
  68. var v []*entity.SysRole
  69. //从数据库获取
  70. err = dao.SysRole.Ctx(ctx).
  71. Order(dao.SysRole.Columns().ListOrder + " asc," + dao.SysRole.Columns().Id + " asc").
  72. Scan(&v)
  73. liberr.ErrIsNil(ctx, err, "获取角色数据失败")
  74. value = v
  75. })
  76. return
  77. }
  78. // AddRoleRule 添加角色权限
  79. func (s *roleImpl) AddRoleRule(ctx context.Context, ruleIds []uint, roleId int64) (err error) {
  80. err = g.Try(func() {
  81. enforcer, e := commonService.CasbinEnforcer(ctx)
  82. liberr.ErrIsNil(ctx, e)
  83. ruleIdsStr := gconv.Strings(ruleIds)
  84. for _, v := range ruleIdsStr {
  85. _, err = enforcer.AddPolicy(gconv.String(roleId), v, "All")
  86. liberr.ErrIsNil(ctx, err)
  87. }
  88. })
  89. return
  90. }
  91. func (s *roleImpl) AddRole(ctx context.Context, req *system.RoleAddReq) (err error) {
  92. err = g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  93. err = g.Try(func() {
  94. roleId, e := dao.SysRole.Ctx(ctx).TX(tx).InsertAndGetId(req)
  95. liberr.ErrIsNil(ctx, e, "添加角色失败")
  96. //添加角色权限
  97. e = s.AddRoleRule(ctx, req.MenuIds, roleId)
  98. liberr.ErrIsNil(ctx, e)
  99. //清除TAG缓存
  100. commonService.Cache().RemoveByTag(ctx, consts.CacheSysAuthTag)
  101. })
  102. return err
  103. })
  104. return
  105. }