refactor file tree
This commit is contained in:
@@ -0,0 +1,277 @@
|
||||
<template>
|
||||
<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="可配置的转授权限 D" theme="card">
|
||||
<t-cell
|
||||
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" />
|
||||
<t-button
|
||||
v-if="canEditActiveRole"
|
||||
v-can="rolePermissionWriteAccess"
|
||||
class="save"
|
||||
theme="primary"
|
||||
@click="savePermissionList"
|
||||
>
|
||||
保存 D
|
||||
</t-button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import type { DropdownOption, DropdownValue } from "tdesign-mobile-vue";
|
||||
import { Toast } from "tdesign-mobile-vue";
|
||||
import { type Permission, useUserStore } from "timi-web";
|
||||
import { type Role, usePermissionSettingsStore } from "@/store/permissionSettingsStore";
|
||||
import { corePermissionCodes, coreRoleCodes } from "@/utils/permission";
|
||||
|
||||
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 rolePermissionWriteAccess = {
|
||||
value: [
|
||||
corePermissionCodes.rolePermission.create,
|
||||
corePermissionCodes.rolePermission.delete
|
||||
],
|
||||
mode: "and"
|
||||
} as const;
|
||||
|
||||
// ---------- 权限状态 ----------
|
||||
|
||||
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
|
||||
])));
|
||||
Toast.success("D 已保存");
|
||||
await loadActiveRolePermissions();
|
||||
} catch (error) {
|
||||
clearPermissionState();
|
||||
try {
|
||||
await loadAuthorizationScope();
|
||||
await loadActiveRolePermissions();
|
||||
} finally {
|
||||
Toast.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> {
|
||||
const roleList = isSystemAdmin.value ? permissionStore.roleList : await permissionStore.loadManageableRoles();
|
||||
candidateRoleList.value = roleList;
|
||||
}
|
||||
|
||||
function getErrorMessage(error: unknown, fallback: string): string {
|
||||
if (error instanceof Error && error.message) {
|
||||
return error.message;
|
||||
}
|
||||
if (typeof error === "string" && error) {
|
||||
return error;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
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(--app-nav-height) + 4.5rem) 0 6rem 0;
|
||||
flex-direction: column;
|
||||
|
||||
.header {
|
||||
top: var(--app-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;
|
||||
}
|
||||
|
||||
.save {
|
||||
left: 1rem;
|
||||
right: 1rem;
|
||||
bottom: calc(.75rem + env(safe-area-inset-bottom, 0));
|
||||
position: fixed;
|
||||
}
|
||||
}
|
||||
|
||||
:global(html.theme-dark) {
|
||||
.role-delegation-permission-assign {
|
||||
.header {
|
||||
background: #0f172a;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user