| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /*
- * @desc:角色处理
- * @company:云南奇讯科技有限公司
- * @Author: yixiaohu
- * @Date: 2022/3/9 10:31
- */
- package service
- import (
- "context"
- "github.com/gogf/gf/v2/database/gdb"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/util/gconv"
- "github.com/tiger1103/gfast/v3/api/v1/system"
- commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
- "github.com/tiger1103/gfast/v3/internal/app/system/consts"
- "github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
- "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao"
- "github.com/tiger1103/gfast/v3/library/liberr"
- )
- type IRole interface {
- GetRoleList(ctx context.Context) (list []*entity.SysRole, err error)
- GetRoleListSearch(ctx context.Context, req *system.RoleListReq) (res *system.RoleListRes, err error)
- AddRole(ctx context.Context, req *system.RoleAddReq) (err error)
- }
- type roleImpl struct {
- }
- var role = roleImpl{}
- func Role() IRole {
- return IRole(&role)
- }
- func (s *roleImpl) GetRoleListSearch(ctx context.Context, req *system.RoleListReq) (res *system.RoleListRes, err error) {
- res = new(system.RoleListRes)
- g.Try(func() {
- model := dao.SysRole.Ctx(ctx)
- if req.RoleName != "" {
- model = model.Where("name like ?", "%"+req.RoleName+"%")
- }
- if req.Status != "" {
- model = model.Where("status", gconv.Int(req.Status))
- }
- res.Total, err = model.Count()
- liberr.ErrIsNil(ctx, err, "获取角色数据失败")
- if req.PageNum == 0 {
- req.PageNum = 1
- }
- res.CurrentPage = req.PageNum
- if req.PageSize == 0 {
- req.PageSize = consts.PageSize
- }
- err = model.Page(res.CurrentPage, req.PageSize).Order("id asc").Scan(&res.List)
- liberr.ErrIsNil(ctx, err, "获取数据失败")
- })
- return
- }
- // GetRoleList 获取角色列表
- func (s *roleImpl) GetRoleList(ctx context.Context) (list []*entity.SysRole, err error) {
- cache := commonService.Cache()
- //从缓存获取
- iList := cache.GetOrSetFuncLock(ctx, consts.CacheSysRole, s.getRoleListFromDb, 0, consts.CacheSysAuthTag)
- if iList != nil {
- err = gconv.Struct(iList, &list)
- }
- return
- }
- // 从数据库获取所有角色
- func (s *roleImpl) getRoleListFromDb(ctx context.Context) (value interface{}, err error) {
- err = g.Try(func() {
- var v []*entity.SysRole
- //从数据库获取
- err = dao.SysRole.Ctx(ctx).
- Order(dao.SysRole.Columns().ListOrder + " asc," + dao.SysRole.Columns().Id + " asc").
- Scan(&v)
- liberr.ErrIsNil(ctx, err, "获取角色数据失败")
- value = v
- })
- return
- }
- // AddRoleRule 添加角色权限
- func (s *roleImpl) AddRoleRule(ctx context.Context, ruleIds []uint, roleId int64) (err error) {
- err = g.Try(func() {
- enforcer, e := commonService.CasbinEnforcer(ctx)
- liberr.ErrIsNil(ctx, e)
- ruleIdsStr := gconv.Strings(ruleIds)
- for _, v := range ruleIdsStr {
- _, err = enforcer.AddPolicy(gconv.String(roleId), v, "All")
- liberr.ErrIsNil(ctx, err)
- }
- })
- return
- }
- func (s *roleImpl) AddRole(ctx context.Context, req *system.RoleAddReq) (err error) {
- err = g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- err = g.Try(func() {
- roleId, e := dao.SysRole.Ctx(ctx).TX(tx).InsertAndGetId(req)
- liberr.ErrIsNil(ctx, e, "添加角色失败")
- //添加角色权限
- e = s.AddRoleRule(ctx, req.MenuIds, roleId)
- liberr.ErrIsNil(ctx, e)
- //清除TAG缓存
- commonService.Cache().RemoveByTag(ctx, consts.CacheSysAuthTag)
- })
- return err
- })
- return
- }
|