From 6ad004da9b5831daa622810304763a6f34fde049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=AD=90=E9=BB=98?= <925456043@qq.com> Date: Fri, 7 Aug 2026 12:47:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8=E7=94=A8=E6=88=B7=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=B1=95=E7=A4=BA=E8=A7=92=E8=89=B2=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 分页批量补全用户关联角色名称,避免逐行查询 - 支持多角色标签、溢出数量和完整信息提示 --- .../system/SysAccountController.java | 46 ++++++- .../easyflow/system/entity/SysAccount.java | 24 ++++ .../system/sysAccount/SysAccountList.vue | 121 ++++++++++++++++++ 3 files changed, 190 insertions(+), 1 deletion(-) diff --git a/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/system/SysAccountController.java b/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/system/SysAccountController.java index 81cc2438..b3f63e9a 100644 --- a/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/system/SysAccountController.java +++ b/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/system/SysAccountController.java @@ -139,7 +139,51 @@ public class SysAccountController extends BaseCurdController queryPage(Page page, QueryWrapper queryWrapper) { - return service.getMapper().paginateWithRelations(page, queryWrapper); + Page result = service.getMapper().paginateWithRelations(page, queryWrapper); + fillRoleNames(result.getRecords()); + return result; + } + + /** + * 按当前分页内的角色 ID 批量补全角色名称。 + * + * @param accounts 当前页账号 + */ + private void fillRoleNames(List accounts) { + if (accounts == null || accounts.isEmpty()) { + return; + } + Set roleIds = accounts.stream() + .map(SysAccount::getRoleIds) + .filter(java.util.Objects::nonNull) + .flatMap(Collection::stream) + .filter(java.util.Objects::nonNull) + .collect(Collectors.toCollection(LinkedHashSet::new)); + if (roleIds.isEmpty()) { + accounts.forEach(account -> account.setRoleNames(List.of())); + return; + } + + Map roleNameMap = sysRoleService.listByIds(roleIds).stream() + .filter(role -> role.getId() != null && StringUtil.hasText(role.getRoleName())) + .collect(Collectors.toMap( + SysRole::getId, + SysRole::getRoleName, + (first, ignored) -> first + )); + accounts.forEach(account -> { + List accountRoleIds = account.getRoleIds(); + if (accountRoleIds == null || accountRoleIds.isEmpty()) { + account.setRoleNames(List.of()); + return; + } + List roleNames = accountRoleIds.stream() + .map(roleNameMap::get) + .filter(StringUtil::hasText) + .distinct() + .collect(Collectors.toList()); + account.setRoleNames(roleNames); + }); } @Override diff --git a/easyflow-modules/easyflow-module-system/src/main/java/tech/easyflow/system/entity/SysAccount.java b/easyflow-modules/easyflow-module-system/src/main/java/tech/easyflow/system/entity/SysAccount.java index b2b1703f..9812d39f 100644 --- a/easyflow-modules/easyflow-module-system/src/main/java/tech/easyflow/system/entity/SysAccount.java +++ b/easyflow-modules/easyflow-module-system/src/main/java/tech/easyflow/system/entity/SysAccount.java @@ -31,6 +31,12 @@ public class SysAccount extends SysAccountBase { ) private List roleIds; + /** + * 账号关联的角色名称,仅用于列表展示。 + */ + @Column(ignore = true) + private List roleNames; + @RelationManyToMany(joinTable = "tb_sys_account_position" , joinSelfColumn = "account_id" , joinTargetColumn = "position_id" @@ -51,6 +57,24 @@ public class SysAccount extends SysAccountBase { this.roleIds = roleIds; } + /** + * 获取账号关联的角色名称。 + * + * @return 角色名称列表 + */ + public List getRoleNames() { + return roleNames; + } + + /** + * 设置账号关联的角色名称。 + * + * @param roleNames 角色名称列表 + */ + public void setRoleNames(List roleNames) { + this.roleNames = roleNames; + } + public List getPositionIds() { return positionIds; } diff --git a/easyflow-ui-admin/app/src/views/system/sysAccount/SysAccountList.vue b/easyflow-ui-admin/app/src/views/system/sysAccount/SysAccountList.vue index 0fe53be1..528c6e5c 100644 --- a/easyflow-ui-admin/app/src/views/system/sysAccount/SysAccountList.vue +++ b/easyflow-ui-admin/app/src/views/system/sysAccount/SysAccountList.vue @@ -20,6 +20,8 @@ import { ElMessageBox, ElTable, ElTableColumn, + ElTag, + ElTooltip, } from 'element-plus'; import { api } from '#/api/request'; @@ -47,6 +49,7 @@ const selectedRows = ref([]); const batchActionLoading = ref(false); const dictStore = useDictStore(); const selectedCount = computed(() => selectedRows.value.length); +const maxVisibleRoleCount = 2; const headerButtons = [ { key: 'create', @@ -68,6 +71,30 @@ const headerButtons = [ function initDict() { dictStore.fetchDictionary('dataStatus'); } +function getRoleNames(row: any): string[] { + if (!Array.isArray(row?.roleNames)) { + return []; + } + const roleNames: unknown[] = row.roleNames; + return [ + ...new Set( + roleNames + .filter( + (roleName: unknown) => + roleName !== null && + roleName !== undefined && + String(roleName).trim().length > 0, + ) + .map(String), + ), + ]; +} +function getVisibleRoleNames(row: any) { + return getRoleNames(row).slice(0, maxVisibleRoleCount); +} +function getHiddenRoleCount(row: any) { + return Math.max(getRoleNames(row).length - maxVisibleRoleCount, 0); +} const handleSearch = (params: string) => { pageDataRef.value.setQuery({ keyword: params.trim() }); }; @@ -353,6 +380,58 @@ function isAdmin(data: any) { {{ row.nickname }} + + +