修改角色bug

This commit is contained in:
chenxing
2026-08-17 18:54:00 +08:00
parent 0fa4b0f38a
commit 2321571060
3 changed files with 54 additions and 73 deletions
@@ -16,6 +16,8 @@ const currentId = ref<number | null>(null);
const detailData = ref<Record<string, any>>({});
// 所在组(只读):用户实际所属的多个组(ID + 完整路径名)
const userGroups = ref<{ id: number; name: string }[]>([]);
// 所在组(只读):默认选中的组 ID(来自 getGroups,失败则兜底当前组),仅用于展示,不提交
const userGroupIdsArr = ref<number[]>([]);
// 角色属性下拉:来自角色授权页的全量角色
const roleOptions = ref<{ id: number; name: string }[]>([]);
// 角色属性是否成功加载(控制编辑后是否回写 saveUserRoles,避免加载失败时误清空)
@@ -62,6 +64,7 @@ async function open(row: any, groups: any[], curGroupId?: number | null, onSucce
roleOptionsLoaded.value = false;
userRolesLoaded.value = false;
userGroups.value = [];
userGroupIdsArr.value = [];
roleOptions.value = [];
detailData.value = {};
visible.value = true;
@@ -79,11 +82,13 @@ async function open(row: any, groups: any[], curGroupId?: number | null, onSucce
groups_ = [{ id: curGroupId, name: groupMap.get(curGroupId)?.name ?? ('组#' + curGroupId) }];
}
userGroups.value = groups_;
userGroupIdsArr.value = groups_.map(g => g.id);
} catch (e: any) {
// getGroups 不可用(如后端未部署该接口)时,至少展示当前点击的组,避免空白
if (curGroupId != null) {
const groupMap = new Map<number, any>((groupList as any[]).map(g => [g.id, g]));
userGroups.value = [{ id: curGroupId, name: groupMap.get(curGroupId)?.name ?? ('组#' + curGroupId) }];
userGroupIdsArr.value = [curGroupId];
}
console.warn('[EditUserContainer] getGroups 失败,已用当前组兜底:', e?.message || e);
}
@@ -105,16 +110,16 @@ async function open(row: any, groups: any[], curGroupId?: number | null, onSucce
.map((r: any) => r.id)
.filter((x: any): x is number => x != null);
userRolesLoaded.value = true;
detailData.value = buildInitForm(row, userRoleIds);
detailData.value = buildInitForm(row, userRoleIds, userGroupIdsArr.value);
} catch (e: any) {
// 已绑定角色加载失败:仍回填其它字段,但标记未加载成功(提交时不回写,避免误清空)
detailData.value = buildInitForm(row, []);
detailData.value = buildInitForm(row, [], userGroupIdsArr.value);
Message.error('加载用户已绑定角色失败:' + (e?.message || '未知错误'));
}
}
/* ---------- 把行数据映射成表单回填数据 ---------- */
function buildInitForm(row: any, userRoleIds: number[]) {
function buildInitForm(row: any, userRoleIds: number[], userGroupIds: number[] = []) {
const roles: number[] = Array.isArray(row.roles) ? row.roles : [];
// 用户身份(业务身份)= 1..7;角色属性已移除(超管手动入库、管理员走勾选)
const userType = roles.filter(r => r >= 1 && r <= 7);
@@ -130,6 +135,7 @@ function buildInitForm(row: any, userRoleIds: number[]) {
gender: row.gender ?? 0,
userType,
appRoles: userRoleIds || [],
userGroupIds: userGroupIds || [],
};
}