diff --git a/mayfly_go_web/src/components/pagetable/PageTable.vue b/mayfly_go_web/src/components/pagetable/PageTable.vue index 3a551026..6887c2ca 100644 --- a/mayfly_go_web/src/components/pagetable/PageTable.vue +++ b/mayfly_go_web/src/components/pagetable/PageTable.vue @@ -148,7 +148,7 @@ - + diff --git a/mayfly_go_web/src/views/system/role/RoleList.vue b/mayfly_go_web/src/views/system/role/RoleList.vue index baa51b43..e030c70d 100755 --- a/mayfly_go_web/src/views/system/role/RoleList.vue +++ b/mayfly_go_web/src/views/system/role/RoleList.vue @@ -20,6 +20,14 @@ 编辑 权限详情 权限分配 + 用户管理 @@ -31,6 +39,9 @@ :defaultCheckedKeys="resourceDialog.defaultCheckedKeys" @cancel="cancelEditResources()" /> + + + @@ -47,12 +58,14 @@ import { TableColumn } from '@/components/pagetable'; import { hasPerms } from '@/components/auth/auth'; import { RoleStatusEnum } from '../enums'; import { SearchItem } from '@/components/SearchForm'; +import AccountAllocation from './AccountAllocation.vue'; const perms = { addRole: 'role:add', delRole: 'role:del', updateRole: 'role:update', saveRoleResource: 'role:saveResources', + saveAccountRole: 'account:saveRoles', }; const searchItems = [SearchItem.input('name', '角色名')]; @@ -67,8 +80,8 @@ const columns = ref([ TableColumn.new('updateTime', '更新时间').isTime(), ]); -const actionBtns = hasPerms([perms.updateRole, perms.saveRoleResource]); -const actionColumn = TableColumn.new('action', '操作').isSlot().setMinWidth(260).fixedRight().alignCenter(); +const actionBtns = hasPerms([perms.updateRole, perms.saveRoleResource, perms.saveAccountRole]); +const actionColumn = TableColumn.new('action', '操作').isSlot().setMinWidth(290).fixedRight().alignCenter(); const pageTableRef: Ref = ref(null); const state = reactive({ @@ -94,9 +107,13 @@ const state = reactive({ resources: [], title: '', }, + accountAllocationDialog: { + visible: false, + role: {}, + }, }); -const { query, selectionData, resourceDialog, roleEditDialog, showResourceDialog } = toRefs(state); +const { query, selectionData, resourceDialog, roleEditDialog, showResourceDialog, accountAllocationDialog } = toRefs(state); onMounted(() => { if (Object.keys(actionBtns).length > 0) { @@ -176,6 +193,11 @@ const editResource = async (row: any) => { state.resourceDialog.role = row; }; +const showAccountAllocation = (data: any) => { + state.accountAllocationDialog.role = data; + state.accountAllocationDialog.visible = true; +}; + /** * 获取所有菜单树的叶子节点 * @param {Object} trees 菜单树列表 diff --git a/server/internal/sys/api/account.go b/server/internal/sys/api/account.go index 5120d724..3c958c90 100644 --- a/server/internal/sys/api/account.go +++ b/server/internal/sys/api/account.go @@ -6,6 +6,7 @@ import ( "mayfly-go/internal/sys/api/form" "mayfly-go/internal/sys/api/vo" "mayfly-go/internal/sys/application" + "mayfly-go/internal/sys/consts" "mayfly-go/internal/sys/domain/entity" "mayfly-go/pkg/biz" "mayfly-go/pkg/contextx" @@ -91,10 +92,7 @@ func (a *Account) ChangePassword(rc *req.Ctx) { func (a *Account) AccountInfo(rc *req.Ctx) { ap := new(vo.AccountPersonVO) // 角色信息 - roles := new([]vo.AccountRoleVO) - a.RoleApp.GetAccountRoles(rc.GetLoginAccount().Id, roles) - - ap.Roles = *roles + ap.Roles = a.getAccountRoles(rc.GetLoginAccount().Id) rc.ResData = ap } @@ -173,16 +171,47 @@ func (a *Account) DeleteAccount(rc *req.Ctx) { } } -// 获取账号角色id列表,用户回显角色分配 -func (a *Account) AccountRoleIds(rc *req.Ctx) { - rc.ResData = a.RoleApp.GetAccountRoleIds(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) +// 获取账号角色信息列表 +func (a *Account) AccountRoles(rc *req.Ctx) { + rc.ResData = a.getAccountRoles(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } -// 获取账号角色id列表,用户回显角色分配 -func (a *Account) AccountRoles(rc *req.Ctx) { - vos := new([]vo.AccountRoleVO) - a.RoleApp.GetAccountRoles(uint64(ginx.PathParamInt(rc.GinCtx, "id")), vos) - rc.ResData = vos +func (a *Account) getAccountRoles(accountId uint64) []*vo.AccountRoleVO { + vos := make([]*vo.AccountRoleVO, 0) + + accountRoles, err := a.RoleApp.GetAccountRoles(accountId) + biz.ErrIsNil(err) + + if len(accountRoles) == 0 { + return vos + } + + // 获取角色信息进行组装 + roleIds := collx.ArrayMap[*entity.AccountRole, uint64](accountRoles, func(val *entity.AccountRole) uint64 { + return val.RoleId + }) + roles, err := a.RoleApp.ListByQuery(&entity.RoleQuery{Ids: roleIds}) + biz.ErrIsNil(err) + roleId2Role := collx.ArrayToMap[*entity.Role, uint64](roles, func(val *entity.Role) uint64 { + return val.Id + }) + + for _, ac := range accountRoles { + role := roleId2Role[ac.RoleId] + if role == nil { + continue + } + vos = append(vos, &vo.AccountRoleVO{ + RoleId: ac.RoleId, + RoleName: role.Name, + Code: role.Code, + Status: role.Status, + CreateTime: ac.CreateTime, // 分配时间 + Creator: ac.Creator, // 分配者 + }) + } + + return vos } func (a *Account) AccountResources(rc *req.Ctx) { @@ -192,19 +221,13 @@ func (a *Account) AccountResources(rc *req.Ctx) { rc.ResData = resources.ToTrees(0) } -// 保存账号角色信息 -func (a *Account) SaveRoles(rc *req.Ctx) { +// 关联账号角色 +func (a *Account) RelateRole(rc *req.Ctx) { var form form.AccountRoleForm ginx.BindJsonAndValid(rc.GinCtx, &form) rc.ReqParam = form - // 将,拼接的字符串进行切割并转换 - newIds := collx.ArrayMap[string, uint64](strings.Split(form.RoleIds, ","), func(val string) uint64 { - id, _ := strconv.Atoi(val) - return uint64(id) - }) - - a.RoleApp.SaveAccountRole(rc.MetaCtx, form.Id, newIds) + biz.ErrIsNil(a.RoleApp.RelateAccountRole(rc.MetaCtx, form.Id, form.RoleId, consts.AccountRoleRelateType(form.RelateType))) } // 重置otp秘钥 diff --git a/server/internal/sys/api/form/role.go b/server/internal/sys/api/form/role.go index e6498600..20142b20 100644 --- a/server/internal/sys/api/form/role.go +++ b/server/internal/sys/api/form/role.go @@ -17,6 +17,7 @@ type RoleForm struct { // 账号分配角色表单 type AccountRoleForm struct { - Id uint64 `json:"id" binding:"required"` - RoleIds string `json:"roleIds"` + Id uint64 `json:"id" binding:"required"` + RoleId uint64 `json:"roleId" binding:"required"` + RelateType int `json:"relateType" binding:"required"` } diff --git a/server/internal/sys/api/role.go b/server/internal/sys/api/role.go index de86130f..1d044e21 100644 --- a/server/internal/sys/api/role.go +++ b/server/internal/sys/api/role.go @@ -8,6 +8,7 @@ import ( "mayfly-go/pkg/biz" "mayfly-go/pkg/ginx" "mayfly-go/pkg/req" + "mayfly-go/pkg/utils/anyx" "mayfly-go/pkg/utils/collx" "strconv" "strings" @@ -20,8 +21,16 @@ type Role struct { func (r *Role) Roles(rc *req.Ctx) { g := rc.GinCtx - condition := &entity.Role{Name: g.Query("name")} - res, err := r.RoleApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Role)) + cond, pageParam := ginx.BindQueryAndPage(g, new(entity.RoleQuery)) + + notIdsStr := g.Query("notIds") + if notIdsStr != "" { + cond.NotIds = collx.ArrayMap[string, uint64](strings.Split(notIdsStr, ","), func(val string) uint64 { + return uint64(anyx.ConvInt(val)) + }) + } + + res, err := r.RoleApp.GetPageList(cond, pageParam, new([]entity.Role)) biz.ErrIsNil(err) rc.ResData = res } @@ -77,3 +86,14 @@ func (r *Role) SaveResource(rc *req.Ctx) { r.RoleApp.SaveRoleResource(rc.MetaCtx, form.Id, newIds) } + +// 查看角色关联的用户 +func (r *Role) RoleAccount(rc *req.Ctx) { + g := rc.GinCtx + cond, pageParam := ginx.BindQueryAndPage[*entity.RoleAccountQuery](g, new(entity.RoleAccountQuery)) + cond.RoleId = uint64(ginx.PathParamInt(g, "id")) + var accounts []*vo.AccountRoleVO + res, err := r.RoleApp.GetRoleAccountPage(cond, pageParam, &accounts) + biz.ErrIsNil(err) + rc.ResData = res +} diff --git a/server/internal/sys/api/vo/account.go b/server/internal/sys/api/vo/account.go index fcc34cbf..e2e3548c 100644 --- a/server/internal/sys/api/vo/account.go +++ b/server/internal/sys/api/vo/account.go @@ -16,13 +16,19 @@ type AccountManageVO struct { // 账号角色信息 type AccountRoleVO struct { - Name *string `json:"name"` - Status int `json:"status"` - CreateTime *time.Time `json:"createTime"` - Creator string `json:"creator"` + RoleId uint64 `json:"roleId"` + RoleName string `json:"roleName"` + Code string `json:"code"` + Status int `json:"status"` + AccountId uint64 `json:"accountId" gorm:"column:accountId"` + AccountName string `json:"accountName" gorm:"column:accountName"` + Username string `json:"username"` + AccountStatus int `json:"accountStatus" gorm:"column:accountStatus"` + CreateTime *time.Time `json:"createTime"` + Creator string `json:"creator"` } // 账号个人信息 type AccountPersonVO struct { - Roles []AccountRoleVO `json:"roles"` // 角色信息 + Roles []*AccountRoleVO `json:"roles"` // 角色信息 } diff --git a/server/internal/sys/application/application.go b/server/internal/sys/application/application.go index 08694abe..ef483919 100644 --- a/server/internal/sys/application/application.go +++ b/server/internal/sys/application/application.go @@ -8,7 +8,7 @@ var ( accountApp = newAccountApp(persistence.GetAccountRepo()) configApp = newConfigApp(persistence.GetConfigRepo()) resourceApp = newResourceApp(persistence.GetResourceRepo()) - roleApp = newRoleApp(persistence.GetRoleRepo()) + roleApp = newRoleApp(persistence.GetRoleRepo(), persistence.GetAccountRoleRepo()) syslogApp = newSyslogApp(persistence.GetSyslogRepo()) ) diff --git a/server/internal/sys/application/role.go b/server/internal/sys/application/role.go index ba81eb1f..9cac86e0 100644 --- a/server/internal/sys/application/role.go +++ b/server/internal/sys/application/role.go @@ -2,20 +2,23 @@ package application import ( "context" + "mayfly-go/internal/sys/consts" "mayfly-go/internal/sys/domain/entity" "mayfly-go/internal/sys/domain/repository" "mayfly-go/pkg/contextx" + "mayfly-go/pkg/errorx" "mayfly-go/pkg/gormx" "mayfly-go/pkg/model" "mayfly-go/pkg/utils/collx" - "strings" "time" "gorm.io/gorm" ) type Role interface { - GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) + GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) + + ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) SaveRole(ctx context.Context, role *entity.Role) error @@ -28,36 +31,37 @@ type Role interface { // 保存角色资源关联记录 SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64) - // 删除角色资源关联记录 - DeleteRoleResource(ctx context.Context, roleId uint64, resourceId uint64) + // 关联账号角色 + RelateAccountRole(ctx context.Context, accountId, roleId uint64, relateType consts.AccountRoleRelateType) error - // 获取账号角色id列表 - GetAccountRoleIds(accountId uint64) []uint64 + // 获取账号关联角色 + GetAccountRoles(accountId uint64) ([]*entity.AccountRole, error) - // 保存账号角色关联信息 - SaveAccountRole(ctx context.Context, accountId uint64, roleIds []uint64) - - DeleteAccountRole(ctx context.Context, accountId, roleId uint64) - - GetAccountRoles(accountId uint64, toEntity any) + // 获取角色关联的用户信息 + GetRoleAccountPage(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) } -func newRoleApp(roleRepo repository.Role) Role { +func newRoleApp(roleRepo repository.Role, accountRoleRepo repository.AccountRole) Role { return &roleAppImpl{ - roleRepo: roleRepo, + roleRepo: roleRepo, + accountRoleRepo: accountRoleRepo, } } type roleAppImpl struct { - roleRepo repository.Role + roleRepo repository.Role + accountRoleRepo repository.AccountRole } -func (m *roleAppImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) { +func (m *roleAppImpl) GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) { return m.roleRepo.GetPageList(condition, pageParam, toEntity, orderBy...) } +func (m *roleAppImpl) ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) { + return m.roleRepo.ListByQuery(condition) +} + func (m *roleAppImpl) SaveRole(ctx context.Context, role *entity.Role) error { - role.Code = strings.ToUpper(role.Code) if role.Id != 0 { // code不可更改,防止误传 role.Code = "" @@ -65,11 +69,11 @@ func (m *roleAppImpl) SaveRole(ctx context.Context, role *entity.Role) error { } role.Status = 1 - return gormx.Insert(role) + return m.roleRepo.Insert(ctx, role) } func (m *roleAppImpl) DeleteRole(ctx context.Context, id uint64) error { - // 删除角色与资源的关联关系 + // 删除角色与资源账号的关联关系 return gormx.Tx( func(db *gorm.DB) error { return m.roleRepo.DeleteByIdWithDb(ctx, db, id) @@ -77,6 +81,9 @@ func (m *roleAppImpl) DeleteRole(ctx context.Context, id uint64) error { func(db *gorm.DB) error { return gormx.DeleteByWithDb(db, &entity.RoleResource{RoleId: id}) }, + func(db *gorm.DB) error { + return gormx.DeleteByWithDb(db, &entity.AccountRole{RoleId: id}) + }, ) } @@ -110,44 +117,35 @@ func (m *roleAppImpl) SaveRoleResource(ctx context.Context, roleId uint64, resou m.roleRepo.SaveRoleResource(addVals) for _, v := range delIds { - m.DeleteRoleResource(ctx, roleId, v) + m.roleRepo.DeleteRoleResource(roleId, v) } } -func (m *roleAppImpl) DeleteRoleResource(ctx context.Context, roleId uint64, resourceId uint64) { - m.roleRepo.DeleteRoleResource(roleId, resourceId) -} +func (m *roleAppImpl) RelateAccountRole(ctx context.Context, accountId, roleId uint64, relateType consts.AccountRoleRelateType) error { + accountRole := &entity.AccountRole{AccountId: accountId, RoleId: roleId} + if relateType == consts.AccountRoleUnbind { + return m.accountRoleRepo.DeleteByCond(ctx, accountRole) + } -func (m *roleAppImpl) GetAccountRoleIds(accountId uint64) []uint64 { - return m.roleRepo.GetAccountRoleIds(accountId) -} - -// 保存账号角色关联信息 -func (m *roleAppImpl) SaveAccountRole(ctx context.Context, accountId uint64, roleIds []uint64) { - oIds := m.GetAccountRoleIds(accountId) - - addIds, delIds, _ := collx.ArrayCompare(roleIds, oIds, func(i1, i2 uint64) bool { - return i1 == i2 - }) + err := m.accountRoleRepo.GetBy(accountRole) + if err == nil { + return errorx.NewBiz("该用户已拥有该权限") + } la := contextx.GetLoginAccount(ctx) - createTime := time.Now() - creator := la.Username - creatorId := la.Id - for _, v := range addIds { - rr := &entity.AccountRole{AccountId: accountId, RoleId: v, CreateTime: &createTime, CreatorId: creatorId, Creator: creator} - m.roleRepo.SaveAccountRole(rr) - } - for _, v := range delIds { - m.DeleteAccountRole(ctx, accountId, v) - } + accountRole.Creator = la.Username + accountRole.CreatorId = la.Id + accountRole.CreateTime = &createTime + return m.accountRoleRepo.Insert(ctx, accountRole) } -func (m *roleAppImpl) DeleteAccountRole(ctx context.Context, accountId, roleId uint64) { - m.roleRepo.DeleteAccountRole(accountId, roleId) +func (m *roleAppImpl) GetAccountRoles(accountId uint64) ([]*entity.AccountRole, error) { + var res []*entity.AccountRole + err := m.accountRoleRepo.ListByCond(&entity.AccountRole{AccountId: accountId}, &res) + return res, err } -func (m *roleAppImpl) GetAccountRoles(accountId uint64, toEntity any) { - m.roleRepo.GetAccountRoles(accountId, toEntity) +func (m *roleAppImpl) GetRoleAccountPage(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) { + return m.accountRoleRepo.GetPageList(condition, pageParam, toEntity, orderBy...) } diff --git a/server/internal/sys/consts/consts.go b/server/internal/sys/consts/consts.go new file mode 100644 index 00000000..69250885 --- /dev/null +++ b/server/internal/sys/consts/consts.go @@ -0,0 +1,8 @@ +package consts + +type AccountRoleRelateType int + +const ( + AccountRoleBind AccountRoleRelateType = 1 + AccountRoleUnbind AccountRoleRelateType = -1 +) diff --git a/server/internal/sys/domain/entity/query.go b/server/internal/sys/domain/entity/query.go index ddb58b9d..13b87864 100644 --- a/server/internal/sys/domain/entity/query.go +++ b/server/internal/sys/domain/entity/query.go @@ -5,3 +5,16 @@ type SysLogQuery struct { Type int8 `json:"type" form:"type"` Description string `json:"description" form:"description"` } + +type RoleQuery struct { + Ids []uint64 `json:"ids"` + Name string `json:"name" form:"name"` + Code string `json:"code" form:"code"` + NotIds []uint64 `json:"notIds"` +} + +type RoleAccountQuery struct { + RoleId uint64 `json:"roleId" ` + Name string `json:"name" form:"name"` + Username string `json:"username" form:"username"` +} diff --git a/server/internal/sys/domain/repository/role.go b/server/internal/sys/domain/repository/role.go index 42672211..cda0c29b 100644 --- a/server/internal/sys/domain/repository/role.go +++ b/server/internal/sys/domain/repository/role.go @@ -9,7 +9,9 @@ import ( type Role interface { base.Repo[*entity.Role] - GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) + GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) + + ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) // 获取角色拥有的资源id数组,从role_resource表获取 GetRoleResourceIds(roleId uint64) []uint64 @@ -19,14 +21,10 @@ type Role interface { SaveRoleResource(rr []*entity.RoleResource) DeleteRoleResource(roleId uint64, resourceId uint64) - - // 获取账号拥有的角色id数组,从account_role表获取 - GetAccountRoleIds(accountId uint64) []uint64 - - SaveAccountRole(ar *entity.AccountRole) - - DeleteAccountRole(accountId, roleId uint64) - - // 获取账号角色信息列表 - GetAccountRoles(accountId uint64, toEntity any) +} + +type AccountRole interface { + base.Repo[*entity.AccountRole] + + GetPageList(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) } diff --git a/server/internal/sys/infrastructure/persistence/account_role.go b/server/internal/sys/infrastructure/persistence/account_role.go new file mode 100644 index 00000000..2b39a537 --- /dev/null +++ b/server/internal/sys/infrastructure/persistence/account_role.go @@ -0,0 +1,30 @@ +package persistence + +import ( + "mayfly-go/internal/sys/domain/entity" + "mayfly-go/internal/sys/domain/repository" + "mayfly-go/pkg/base" + "mayfly-go/pkg/gormx" + "mayfly-go/pkg/model" +) + +type accountRoleRepoImpl struct { + base.RepoImpl[*entity.AccountRole] +} + +func newAccountRoleRepo() repository.AccountRole { + return &accountRoleRepoImpl{base.RepoImpl[*entity.AccountRole]{M: new(entity.AccountRole)}} +} + +func (m *accountRoleRepoImpl) GetPageList(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) { + qd := gormx.NewQueryWithTableName("t_sys_account_role t"). + Select("t.creator, t.create_time, a.username, a.name accountName, a.status accountStatus, a.id accountId"). + Joins("JOIN t_sys_account a ON t.account_id = a.id AND a.status = 1"). + Eq0("a.is_deleted", model.ModelUndeleted). + Eq0("t.is_deleted", model.ModelUndeleted). + RLike("a.username", condition.Username). + RLike("a.name", condition.Name). + Eq("t.role_id", condition.RoleId). + OrderByDesc("t.id") + return gormx.PageQuery(qd, pageParam, toEntity) +} diff --git a/server/internal/sys/infrastructure/persistence/persistence.go b/server/internal/sys/infrastructure/persistence/persistence.go index ca95c5d2..3c8292ce 100644 --- a/server/internal/sys/infrastructure/persistence/persistence.go +++ b/server/internal/sys/infrastructure/persistence/persistence.go @@ -3,11 +3,12 @@ package persistence import "mayfly-go/internal/sys/domain/repository" var ( - accountRepo = newAccountRepo() - configRepo = newConfigRepo() - resourceRepo = newResourceRepo() - roleRepo = newRoleRepo() - syslogRepo = newSyslogRepo() + accountRepo = newAccountRepo() + configRepo = newConfigRepo() + resourceRepo = newResourceRepo() + roleRepo = newRoleRepo() + accountRoleRepo = newAccountRoleRepo() + syslogRepo = newSyslogRepo() ) func GetAccountRepo() repository.Account { @@ -26,6 +27,10 @@ func GetRoleRepo() repository.Role { return roleRepo } +func GetAccountRoleRepo() repository.AccountRole { + return accountRoleRepo +} + func GetSyslogRepo() repository.Syslog { return syslogRepo } diff --git a/server/internal/sys/infrastructure/persistence/role.go b/server/internal/sys/infrastructure/persistence/role.go index 3f0eb570..e477c155 100644 --- a/server/internal/sys/infrastructure/persistence/role.go +++ b/server/internal/sys/infrastructure/persistence/role.go @@ -16,11 +16,28 @@ func newRoleRepo() repository.Role { return &roleRepoImpl{base.RepoImpl[*entity.Role]{M: new(entity.Role)}} } -func (m *roleRepoImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) { - qd := gormx.NewQuery(condition).WithCondModel(condition).WithOrderBy(orderBy...) +func (m *roleRepoImpl) GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) { + qd := gormx.NewQuery(new(entity.Role)). + Like("name", condition.Name). + Like("code", condition.Code). + In("id", condition.Ids). + NotIn("id", condition.NotIds). + WithOrderBy(orderBy...) return gormx.PageQuery(qd, pageParam, toEntity) } +func (m *roleRepoImpl) ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) { + var res []*entity.Role + qd := gormx.NewQuery(new(entity.Role)). + Like("name", condition.Name). + Like("code", condition.Code). + In("id", condition.Ids). + NotIn("id", condition.NotIds). + OrderByDesc("id") + err := gormx.ListByQueryCond(qd, &res) + return res, err +} + // 获取角色拥有的资源id数组,从role_resource表获取 func (m *roleRepoImpl) GetRoleResourceIds(roleId uint64) []uint64 { var rrs []entity.RoleResource @@ -51,32 +68,3 @@ func (m *roleRepoImpl) SaveRoleResource(rr []*entity.RoleResource) { func (m *roleRepoImpl) DeleteRoleResource(roleId uint64, resourceId uint64) { gormx.DeleteBy(&entity.RoleResource{RoleId: roleId, ResourceId: resourceId}) } - -func (m *roleRepoImpl) GetAccountRoleIds(accountId uint64) []uint64 { - var rrs []entity.AccountRole - - condtion := &entity.AccountRole{AccountId: accountId} - gormx.ListBy(condtion, &rrs, "RoleId") - - var rids []uint64 - for _, v := range rrs { - rids = append(rids, v.RoleId) - } - return rids -} - -func (m *roleRepoImpl) SaveAccountRole(ar *entity.AccountRole) { - gormx.Insert(ar) -} - -func (m *roleRepoImpl) DeleteAccountRole(accountId, roleId uint64) { - gormx.DeleteBy(&entity.AccountRole{RoleId: roleId, AccountId: accountId}) -} - -// 获取账号角色信息列表 -func (m *roleRepoImpl) GetAccountRoles(accountId uint64, toEntity any) { - sql := "SELECT r.status, r.name, ar.create_time AS CreateTime, ar.creator AS creator " + - "FROM t_sys_role r JOIN t_sys_account_role ar ON r.id = ar.role_id AND ar.account_id = ? AND r.is_deleted = 0 AND ar.is_deleted = 0 " + - "ORDER BY ar.create_time DESC" - gormx.GetListBySql2Model(sql, toEntity, accountId) -} diff --git a/server/internal/sys/router/account.go b/server/internal/sys/router/account.go index ef2f7631..0e364351 100644 --- a/server/internal/sys/router/account.go +++ b/server/internal/sys/router/account.go @@ -47,11 +47,8 @@ func InitAccountRouter(router *gin.RouterGroup) { req.NewDelete(":id", a.DeleteAccount).Log(req.NewLogSave("删除账号")).RequiredPermissionCode("account:del"), - // 获取所有用户角色id列表 - req.NewGet(":id/roleIds", a.AccountRoleIds), - - // 保存用户角色 - req.NewPost("/roles", a.SaveRoles).Log(req.NewLogSave("保存用户角色")).RequiredPermissionCode("account:saveRoles"), + // 关联用户角色 + req.NewPost("/roles", a.RelateRole).Log(req.NewLogSave("关联用户角色")).RequiredPermissionCode("account:saveRoles"), // 获取用户角色 req.NewGet(":id/roles", a.AccountRoles), diff --git a/server/internal/sys/router/role.go b/server/internal/sys/router/role.go index 5b624135..0ee92490 100644 --- a/server/internal/sys/router/role.go +++ b/server/internal/sys/router/role.go @@ -27,6 +27,8 @@ func InitRoleRouter(router *gin.RouterGroup) { req.NewGet(":id/resources", r.RoleResource), req.NewPost(":id/resources", r.SaveResource).Log(req.NewLogSave("保存角色资源")).RequiredPermissionCode("role:saveResources"), + + req.NewGet(":id/accounts", r.RoleAccount), } req.BatchSetGroup(rg, reqs[:]) diff --git a/server/pkg/consts/gormx.go b/server/pkg/consts/gormx.go index 9345580d..82496039 100644 --- a/server/pkg/consts/gormx.go +++ b/server/pkg/consts/gormx.go @@ -11,6 +11,7 @@ const ( And = "AND" Or = "OR" In = "IN" + NotIn = "NOT IN" Not = "NOT" Like = "LIKE" Eq = "=" diff --git a/server/pkg/gormx/gormx.go b/server/pkg/gormx/gormx.go index e6479756..9670a496 100644 --- a/server/pkg/gormx/gormx.go +++ b/server/pkg/gormx/gormx.go @@ -72,6 +72,13 @@ func PageQuery[T any](q *QueryCond, pageParam *model.PageParam, toModels T) (*mo return &model.PageResult[T]{Total: count, List: toModels}, nil } +// 根据查询条件查询列表信息 +func ListByQueryCond(q *QueryCond, list any) error { + q.Undeleted() + gdb := q.GenGdb() + return gdb.Find(list).Error +} + // 获取满足model中不为空的字段值条件的所有数据. // // @param list为数组类型 如 var users *[]User,可指定为非model结构体,即只包含需要返回的字段结构体 diff --git a/server/pkg/gormx/query.go b/server/pkg/gormx/query.go index db48eb60..1e334548 100644 --- a/server/pkg/gormx/query.go +++ b/server/pkg/gormx/query.go @@ -127,6 +127,10 @@ func (q *QueryCond) In(column string, val any) *QueryCond { return q.Cond(consts.In, column, val, true) } +func (q *QueryCond) NotIn(column string, val any) *QueryCond { + return q.Cond(consts.NotIn, column, val, true) +} + // // Ne 不等于 != func (q *QueryCond) Ne(column string, val any) *QueryCond { q.Cond(consts.Ne, column, val, true)