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({
core: {
apiFn: api.list,
apiParams: {
orderField: 'level',
orderType: 'desc'
},
columnsFactory: () => [
{ prop: 'id', label: 'table.columns.common.no', minWidth: 60, align: 'center' },
{ prop: 'name', label: 'page.table.roleName', minWidth: 120 },

View File

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

View File

@@ -189,12 +189,16 @@ class SystemRoleChannelService
if ($this->tableHasColumn('sa_system_role', 'dept_id')) {
$query->where('dept_id', 0);
}
$rows = $query->order('sort', 'desc')->select()->toArray();
if (count($rows) === count($codes)) {
return $rows;
$rows = $query->select()->toArray();
if (empty($rows)) {
return [];
}
// 按配置顺序返回,缺失的 code 跳过
if (count($rows) === count($codes)) {
return $this->sortRolesByLevelDesc($rows);
}
// 按配置 code 补齐,缺失的 code 跳过,最终按角色级别从大到小排序
$byCode = [];
foreach ($rows as $row) {
$byCode[(string) ($row['code'] ?? '')] = $row;
@@ -205,7 +209,25 @@ class SystemRoleChannelService
$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