1.优化角色管理页面/system/role中角色级别排序

This commit is contained in:
2026-06-01 09:56:17 +08:00
parent 906539995d
commit d77e390fa3
3 changed files with 35 additions and 6 deletions

View File

@@ -125,6 +125,10 @@
} = useTable({ } = useTable({
core: { core: {
apiFn: api.list, apiFn: api.list,
apiParams: {
orderField: 'level',
orderType: 'desc'
},
columnsFactory: () => [ columnsFactory: () => [
{ prop: 'id', label: 'table.columns.common.no', minWidth: 60, align: 'center' }, { prop: 'id', label: 'table.columns.common.no', minWidth: 60, align: 'center' },
{ prop: 'name', label: 'page.table.roleName', minWidth: 120 }, { prop: 'name', label: 'page.table.roleName', minWidth: 120 },

View File

@@ -20,6 +20,10 @@ use support\think\Db;
*/ */
class SystemRoleLogic extends BaseLogic class SystemRoleLogic extends BaseLogic
{ {
protected string $orderField = 'level';
protected string $orderType = 'desc';
public function __construct() public function __construct()
{ {
$this->model = new SystemRole(); $this->model = new SystemRole();
@@ -110,7 +114,6 @@ class SystemRoleLogic extends BaseLogic
$query->where('level', '<', $maxLevel); $query->where('level', '<', $maxLevel);
} }
$query->where('id', '<>', SystemRoleChannelService::SUPER_ADMIN_ROLE_ID); $query->where('id', '<>', SystemRoleChannelService::SUPER_ADMIN_ROLE_ID);
$query->order('sort', 'desc');
return $this->getAll($query); return $this->getAll($query);
} }

View File

@@ -189,12 +189,16 @@ class SystemRoleChannelService
if ($this->tableHasColumn('sa_system_role', 'dept_id')) { if ($this->tableHasColumn('sa_system_role', 'dept_id')) {
$query->where('dept_id', 0); $query->where('dept_id', 0);
} }
$rows = $query->order('sort', 'desc')->select()->toArray(); $rows = $query->select()->toArray();
if (count($rows) === count($codes)) { if (empty($rows)) {
return $rows; return [];
} }
// 按配置顺序返回,缺失的 code 跳过 if (count($rows) === count($codes)) {
return $this->sortRolesByLevelDesc($rows);
}
// 按配置 code 补齐,缺失的 code 跳过,最终按角色级别从大到小排序
$byCode = []; $byCode = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$byCode[(string) ($row['code'] ?? '')] = $row; $byCode[(string) ($row['code'] ?? '')] = $row;
@@ -205,7 +209,25 @@ class SystemRoleChannelService
$ordered[] = $byCode[$code]; $ordered[] = $byCode[$code];
} }
} }
return $ordered; return $this->sortRolesByLevelDesc($ordered);
}
/**
* 按角色级别从大到小排序(同级别按 sort 降序)
*
* @param array<int, array<string, mixed>> $rows
* @return array<int, array<string, mixed>>
*/
private function sortRolesByLevelDesc(array $rows): array
{
usort($rows, static function (array $a, array $b): int {
$levelCompare = (int) ($b['level'] ?? 0) <=> (int) ($a['level'] ?? 0);
if ($levelCompare !== 0) {
return $levelCompare;
}
return (int) ($b['sort'] ?? 0) <=> (int) ($a['sort'] ?? 0);
});
return $rows;
} }
private function insertRoleFromTemplate(array $template, int $deptId): int private function insertRoleFromTemplate(array $template, int $deptId): int