269 lines
7.2 KiB
Vue
269 lines
7.2 KiB
Vue
<template>
|
|
<t-navbar
|
|
fixed
|
|
:z-index="20"
|
|
title="角色可转授权限 D"
|
|
left-arrow
|
|
@left-click="$router.back()"
|
|
>
|
|
<template #right>
|
|
<t-button
|
|
v-if="canEditActiveRole && canUpdateRolePermission"
|
|
@click="savePermissionList"
|
|
theme="primary"
|
|
variant="text"
|
|
size="small"
|
|
content="保存"
|
|
/>
|
|
</template>
|
|
</t-navbar>
|
|
<div
|
|
v-can="{ value: [corePermissionCodes.role.read, corePermissionCodes.permission.read, corePermissionCodes.rolePermission.read], mode: 'and' }"
|
|
class="role-delegation-permission-assign"
|
|
>
|
|
<div class="header bg-white">
|
|
<t-dropdown-menu class="role-selector">
|
|
<t-dropdown-item
|
|
:label="activeRoleLabel"
|
|
:options="roleOptions"
|
|
:value="activeRoleId"
|
|
borderless
|
|
@change="handleRoleChange"
|
|
/>
|
|
</t-dropdown-menu>
|
|
</div>
|
|
<t-cell-group
|
|
v-if="activeRole && grantablePermissionList.length"
|
|
title="允许该角色可继续往下级角色转授权限"
|
|
theme="card"
|
|
>
|
|
<t-cell
|
|
v-entrance-transition="{ step: 30 }"
|
|
v-for="item in grantablePermissionList"
|
|
:key="item.id"
|
|
:title="item.name || item.code"
|
|
>
|
|
<template #description>
|
|
<span class="code" v-text="item.code"></span>
|
|
</template>
|
|
<template #right-icon>
|
|
<t-switch
|
|
:disabled="!canUpdateRolePermission || !canEditActiveRole"
|
|
:model-value="isPermissionChecked(item.id)"
|
|
@change="value => setPermissionChecked(item.id, value === true)"
|
|
/>
|
|
</template>
|
|
</t-cell>
|
|
</t-cell-group>
|
|
<t-empty v-else-if="!permissionStore.loading" description="暂无可配置的转授权限 D" />
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import type { DropdownOption, DropdownValue } from "tdesign-mobile-vue";
|
|
import { Message } from "@/utils/message";
|
|
import { type Permission, useUserStore } from "timi-web-api";
|
|
import { getErrorMessage } from "@/utils/error";
|
|
import { corePermissionCodes, coreRoleCodes } from "@/utils/permission";
|
|
|
|
import { type Role, usePermissionSettingsStore } from "@/store/permissionSettingsStore";
|
|
|
|
defineOptions({
|
|
name: "RoleDelegationPermissionAssign"
|
|
});
|
|
|
|
const permissionStore = usePermissionSettingsStore();
|
|
const userStore = useUserStore();
|
|
const route = useRoute();
|
|
|
|
// ---------- 角色选择 ----------
|
|
|
|
const activeRoleId = ref(String(route.query.roleId || route.params.id || ""));
|
|
const checkedPermissionIds = ref<string[]>([]);
|
|
const unmanagedPermissionIds = ref<string[]>([]);
|
|
const grantablePermissionList = ref<Permission[]>([]);
|
|
const candidateRoleList = ref<Role[]>([]);
|
|
let permissionSyncSeq = 0;
|
|
|
|
const activeRole = computed(() => permissionStore.getRoleById(activeRoleId.value));
|
|
const activeRoleLabel = computed(() => activeRole.value ? `当前角色:${activeRole.value.name}` : "选择角色");
|
|
const canUpdateRolePermission = computed(() => userStore.hasPermissions([
|
|
corePermissionCodes.rolePermission.create,
|
|
corePermissionCodes.rolePermission.delete
|
|
], "and"));
|
|
const isSystemAdmin = computed(() => userStore.hasRoles(coreRoleCodes.admin));
|
|
const canEditActiveRole = computed(() => Boolean(activeRole.value)
|
|
&& candidateRoleList.value.some(item => item.id === activeRoleId.value)
|
|
&& (isSystemAdmin.value || activeRole.value?.protectedRole !== true));
|
|
const roleOptions = computed<DropdownOption[]>(() => candidateRoleList.value
|
|
.filter(item => !!item.id)
|
|
.map(item => ({
|
|
label: item.name || item.code,
|
|
value: item.id,
|
|
disabled: false
|
|
})));
|
|
|
|
// ---------- 权限状态 ----------
|
|
|
|
const checkedPermissionIdSet = computed(() => new Set(checkedPermissionIds.value));
|
|
|
|
// ---------- 角色切换 ----------
|
|
|
|
async function handleRoleChange(value: DropdownValue): Promise<void> {
|
|
activeRoleId.value = Array.isArray(value) ? String(value[0] || "") : String(value);
|
|
await loadActiveRolePermissions();
|
|
}
|
|
|
|
async function loadActiveRolePermissions(): Promise<void> {
|
|
const roleId = activeRoleId.value;
|
|
if (!roleId) {
|
|
clearPermissionState();
|
|
return;
|
|
}
|
|
|
|
const currentSeq = permissionSyncSeq + 1;
|
|
permissionSyncSeq = currentSeq;
|
|
const [role, permissionList] = await Promise.all([
|
|
permissionStore.loadRoleDetail(roleId),
|
|
permissionStore.loadGrantableDelegationPermissions(roleId)
|
|
]);
|
|
if (currentSeq !== permissionSyncSeq || activeRoleId.value !== roleId) {
|
|
return;
|
|
}
|
|
|
|
grantablePermissionList.value = permissionList;
|
|
const grantablePermissionIdSet = new Set(grantablePermissionList.value.map(item => item.id));
|
|
checkedPermissionIds.value = role.delegationPermissionIdList.filter(id => grantablePermissionIdSet.has(id));
|
|
unmanagedPermissionIds.value = role.delegationPermissionIdList.filter(id => !grantablePermissionIdSet.has(id));
|
|
}
|
|
|
|
// ---------- 保存 ----------
|
|
|
|
async function savePermissionList(): Promise<void> {
|
|
if (!activeRoleId.value || !canEditActiveRole.value || !canUpdateRolePermission.value) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await permissionStore.setRoleDelegationPermissionList(activeRoleId.value, Array.from(new Set([
|
|
...checkedPermissionIds.value,
|
|
...unmanagedPermissionIds.value
|
|
])));
|
|
Message.success("D 已保存");
|
|
await loadActiveRolePermissions();
|
|
} catch (error) {
|
|
clearPermissionState();
|
|
try {
|
|
await loadAuthorizationScope();
|
|
await loadActiveRolePermissions();
|
|
} finally {
|
|
Message.error(getErrorMessage(error, "D 保存失败"));
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------- 权限勾选 ----------
|
|
|
|
function isPermissionChecked(permissionId: string): boolean {
|
|
return checkedPermissionIdSet.value.has(permissionId);
|
|
}
|
|
|
|
function setPermissionChecked(permissionId: string, value: boolean): void {
|
|
if (!canUpdateRolePermission.value || !canEditActiveRole.value) {
|
|
return;
|
|
}
|
|
|
|
if (value && !checkedPermissionIds.value.includes(permissionId)) {
|
|
checkedPermissionIds.value.push(permissionId);
|
|
return;
|
|
}
|
|
|
|
if (!value) {
|
|
checkedPermissionIds.value = checkedPermissionIds.value.filter(item => item !== permissionId);
|
|
}
|
|
}
|
|
|
|
function clearPermissionState(): void {
|
|
checkedPermissionIds.value = [];
|
|
unmanagedPermissionIds.value = [];
|
|
grantablePermissionList.value = [];
|
|
}
|
|
|
|
// ---------- 授权范围 ----------
|
|
|
|
async function loadAuthorizationScope(): Promise<void> {
|
|
candidateRoleList.value = permissionStore.roleList;
|
|
}
|
|
|
|
onMounted(async () => {
|
|
await permissionStore.loadAll();
|
|
await loadAuthorizationScope();
|
|
if (!activeRoleId.value) {
|
|
activeRoleId.value = candidateRoleList.value[0]?.id || "";
|
|
}
|
|
await loadActiveRolePermissions();
|
|
});
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.role-delegation-permission-assign {
|
|
gap: 1rem;
|
|
display: flex;
|
|
padding: calc(var(--tui-nav-height) + 3.5rem) 0 6rem 0;
|
|
flex-direction: column;
|
|
|
|
.header {
|
|
top: var(--tui-nav-height);
|
|
left: 0;
|
|
right: 0;
|
|
z-index: 20;
|
|
display: flex;
|
|
position: fixed;
|
|
background: #fff;
|
|
box-shadow: var(--td-shadow-1);
|
|
align-items: center;
|
|
justify-content: center;
|
|
|
|
.role-selector::after {
|
|
display: none;
|
|
}
|
|
}
|
|
|
|
:deep(.t-cell__title) {
|
|
min-width: 0;
|
|
}
|
|
|
|
:deep(.t-cell__title-text) {
|
|
overflow: hidden;
|
|
white-space: nowrap;
|
|
text-overflow: ellipsis;
|
|
}
|
|
|
|
:deep(.t-cell__description) {
|
|
min-width: 0;
|
|
}
|
|
|
|
:deep(.t-cell__right) {
|
|
flex: 0 0 auto;
|
|
margin-left: .75rem;
|
|
}
|
|
|
|
.code {
|
|
width: 100%;
|
|
display: block;
|
|
overflow: hidden;
|
|
white-space: nowrap;
|
|
text-overflow: ellipsis;
|
|
}
|
|
}
|
|
|
|
:global(html.theme-dark) {
|
|
.role-delegation-permission-assign {
|
|
.header {
|
|
background: #0f172a;
|
|
}
|
|
}
|
|
}
|
|
|
|
</style>
|