feat: 移除mock并接入真实权限控制
This commit is contained in:
@@ -0,0 +1,533 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { ElMessage } from "element-plus";
|
||||
import {
|
||||
getPermissionDefinitions,
|
||||
getPermissionPolicies,
|
||||
updateRolePermissions,
|
||||
type PermissionDefinitionGroup,
|
||||
type PermissionPolicy
|
||||
} from "@/api/access";
|
||||
import { useUserStoreHook } from "@/store/modules/user";
|
||||
import { hasMenuAction } from "@/utils/auth";
|
||||
|
||||
import Check from "~icons/ep/check";
|
||||
import Edit from "~icons/ep/edit";
|
||||
import Refresh from "~icons/ep/refresh";
|
||||
|
||||
defineOptions({
|
||||
name: "PermissionPolicies"
|
||||
});
|
||||
|
||||
const tableLoading = ref(false);
|
||||
const saving = ref(false);
|
||||
const drawerVisible = ref(false);
|
||||
const policies = ref<PermissionPolicy[]>([]);
|
||||
const definitionGroups = ref<PermissionDefinitionGroup[]>([]);
|
||||
const editingPolicy = ref<PermissionPolicy | null>(null);
|
||||
const checkedPermissions = ref<string[]>([]);
|
||||
|
||||
const canUpdatePermissions = computed(() =>
|
||||
hasMenuAction("permissions", "update")
|
||||
);
|
||||
const editableRoleCount = computed(
|
||||
() => policies.value.filter(item => item.editable).length
|
||||
);
|
||||
const menuTotal = computed(() =>
|
||||
policies.value.reduce((total, item) => total + item.menus.length, 0)
|
||||
);
|
||||
const permissionTotal = computed(() =>
|
||||
policies.value.reduce(
|
||||
(total, item) =>
|
||||
total + item.permissions.filter(permission => permission !== "*").length,
|
||||
0
|
||||
)
|
||||
);
|
||||
const drawerTitle = computed(() =>
|
||||
editingPolicy.value ? `分配权限:${editingPolicy.value.roleName}` : "分配权限"
|
||||
);
|
||||
|
||||
function getErrorMessage(error: unknown, fallback: string) {
|
||||
const message = (
|
||||
error as { response?: { data?: { error?: { message?: string } } } }
|
||||
)?.response?.data?.error?.message;
|
||||
|
||||
if (message) return message;
|
||||
if (error instanceof Error && error.message) return error.message;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function formatActions(actions: string[]) {
|
||||
const actionMap: Record<string, string> = {
|
||||
view: "查看",
|
||||
create: "新增",
|
||||
update: "编辑",
|
||||
delete: "删除"
|
||||
};
|
||||
|
||||
return actions.map(action => actionMap[action] ?? action).join(" / ");
|
||||
}
|
||||
|
||||
function formatPermissionTitle(code: string) {
|
||||
for (const group of definitionGroups.value) {
|
||||
const found = group.permissions.find(permission => permission.code === code);
|
||||
if (found) return found.title;
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
function isGroupChecked(group: PermissionDefinitionGroup) {
|
||||
const codes = group.permissions.map(permission => permission.code);
|
||||
return codes.every(code => checkedPermissions.value.includes(code));
|
||||
}
|
||||
|
||||
function isGroupIndeterminate(group: PermissionDefinitionGroup) {
|
||||
const codes = group.permissions.map(permission => permission.code);
|
||||
const checkedCount = codes.filter(code =>
|
||||
checkedPermissions.value.includes(code)
|
||||
).length;
|
||||
|
||||
return checkedCount > 0 && checkedCount < codes.length;
|
||||
}
|
||||
|
||||
function toggleGroup(group: PermissionDefinitionGroup, checked: boolean) {
|
||||
const nextPermissions = new Set(checkedPermissions.value);
|
||||
|
||||
for (const permission of group.permissions) {
|
||||
if (checked) {
|
||||
nextPermissions.add(permission.code);
|
||||
} else {
|
||||
nextPermissions.delete(permission.code);
|
||||
}
|
||||
}
|
||||
|
||||
checkedPermissions.value = [...nextPermissions];
|
||||
}
|
||||
|
||||
function openEditor(policy: PermissionPolicy) {
|
||||
if (!policy.editable || !canUpdatePermissions.value) return;
|
||||
|
||||
editingPolicy.value = policy;
|
||||
checkedPermissions.value = policy.permissions.filter(
|
||||
permission => permission !== "*"
|
||||
);
|
||||
drawerVisible.value = true;
|
||||
}
|
||||
|
||||
async function fetchPermissionData() {
|
||||
tableLoading.value = true;
|
||||
try {
|
||||
const [policyResult, definitionResult] = await Promise.all([
|
||||
getPermissionPolicies(),
|
||||
getPermissionDefinitions()
|
||||
]);
|
||||
|
||||
policies.value = policyResult.data;
|
||||
definitionGroups.value = definitionResult.data.groups;
|
||||
} catch (error) {
|
||||
ElMessage.error(getErrorMessage(error, "加载权限数据失败"));
|
||||
} finally {
|
||||
tableLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function savePermissions() {
|
||||
if (!editingPolicy.value) return;
|
||||
|
||||
saving.value = true;
|
||||
try {
|
||||
await updateRolePermissions(
|
||||
editingPolicy.value.roleId,
|
||||
checkedPermissions.value
|
||||
);
|
||||
await fetchPermissionData();
|
||||
await useUserStoreHook().loadAuthContext(true);
|
||||
drawerVisible.value = false;
|
||||
ElMessage.success("权限已更新");
|
||||
} catch (error) {
|
||||
ElMessage.error(getErrorMessage(error, "保存权限失败"));
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(fetchPermissionData);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="permission-page">
|
||||
<div class="page-heading">
|
||||
<div>
|
||||
<p class="eyebrow">动态权限分配</p>
|
||||
<h1>权限策略</h1>
|
||||
</div>
|
||||
<el-button :icon="Refresh" @click="fetchPermissionData">刷新</el-button>
|
||||
</div>
|
||||
|
||||
<div class="summary-strip">
|
||||
<div class="summary-item">
|
||||
<span>角色策略</span>
|
||||
<strong>{{ policies.length }}</strong>
|
||||
</div>
|
||||
<div class="summary-item">
|
||||
<span>可分配角色</span>
|
||||
<strong>{{ editableRoleCount }}</strong>
|
||||
</div>
|
||||
<div class="summary-item">
|
||||
<span>权限码</span>
|
||||
<strong>{{ permissionTotal }}</strong>
|
||||
</div>
|
||||
<div class="summary-item">
|
||||
<span>菜单授权</span>
|
||||
<strong>{{ menuTotal }}</strong>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="table-shell">
|
||||
<el-table
|
||||
v-loading="tableLoading"
|
||||
:data="policies"
|
||||
row-key="roleCode"
|
||||
stripe
|
||||
class="permission-table"
|
||||
>
|
||||
<el-table-column label="角色" min-width="190">
|
||||
<template #default="{ row }">
|
||||
<div class="role-cell">
|
||||
<strong>{{ row.roleName }}</strong>
|
||||
<span>{{ row.roleCode }}</span>
|
||||
</div>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="scope" label="作用范围" min-width="140" />
|
||||
<el-table-column label="权限码" min-width="300">
|
||||
<template #default="{ row }">
|
||||
<div class="tag-list">
|
||||
<el-tag
|
||||
v-for="permission in row.permissions"
|
||||
:key="permission"
|
||||
effect="plain"
|
||||
size="small"
|
||||
>
|
||||
{{ permission === "*" ? "全部权限" : formatPermissionTitle(permission) }}
|
||||
</el-tag>
|
||||
<span v-if="row.permissions.length === 0" class="muted-text">
|
||||
未分配
|
||||
</span>
|
||||
</div>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="菜单与动作" min-width="360">
|
||||
<template #default="{ row }">
|
||||
<div class="menu-list">
|
||||
<div v-for="menu in row.menus" :key="menu.key" class="menu-item">
|
||||
<strong>{{ menu.title }}</strong>
|
||||
<span>{{ formatActions(menu.actions) }}</span>
|
||||
</div>
|
||||
<span v-if="row.menus.length === 0" class="muted-text">
|
||||
无后台菜单
|
||||
</span>
|
||||
</div>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="操作" width="132" fixed="right">
|
||||
<template #default="{ row }">
|
||||
<el-tooltip
|
||||
:content="row.editable ? '分配权限' : '系统最高权限不可编辑'"
|
||||
placement="top"
|
||||
>
|
||||
<span>
|
||||
<el-button
|
||||
:icon="Edit"
|
||||
:disabled="!row.editable || !canUpdatePermissions"
|
||||
text
|
||||
type="primary"
|
||||
@click="openEditor(row)"
|
||||
>
|
||||
分配
|
||||
</el-button>
|
||||
</span>
|
||||
</el-tooltip>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
|
||||
<el-drawer
|
||||
v-model="drawerVisible"
|
||||
:title="drawerTitle"
|
||||
size="520px"
|
||||
destroy-on-close
|
||||
>
|
||||
<div class="drawer-body">
|
||||
<section
|
||||
v-for="group in definitionGroups"
|
||||
:key="group.key"
|
||||
class="permission-group"
|
||||
>
|
||||
<div class="group-heading">
|
||||
<strong>{{ group.title }}</strong>
|
||||
<el-checkbox
|
||||
:model-value="isGroupChecked(group)"
|
||||
:indeterminate="isGroupIndeterminate(group)"
|
||||
@change="value => toggleGroup(group, Boolean(value))"
|
||||
>
|
||||
全选
|
||||
</el-checkbox>
|
||||
</div>
|
||||
<div class="permission-options">
|
||||
<el-checkbox-group v-model="checkedPermissions">
|
||||
<el-checkbox
|
||||
v-for="permission in group.permissions"
|
||||
:key="permission.code"
|
||||
:label="permission.code"
|
||||
border
|
||||
>
|
||||
<span class="permission-option">
|
||||
<strong>{{ permission.title }}</strong>
|
||||
<small>{{ permission.code }}</small>
|
||||
</span>
|
||||
</el-checkbox>
|
||||
</el-checkbox-group>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<template #footer>
|
||||
<div class="drawer-footer">
|
||||
<el-button @click="drawerVisible = false">取消</el-button>
|
||||
<el-button
|
||||
:icon="Check"
|
||||
:loading="saving"
|
||||
type="primary"
|
||||
@click="savePermissions"
|
||||
>
|
||||
保存
|
||||
</el-button>
|
||||
</div>
|
||||
</template>
|
||||
</el-drawer>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.permission-page {
|
||||
min-height: 100%;
|
||||
padding: 20px;
|
||||
background: #f6f8fb;
|
||||
}
|
||||
|
||||
.page-heading {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 16px;
|
||||
|
||||
h1 {
|
||||
margin: 4px 0 0;
|
||||
font-size: 24px;
|
||||
font-weight: 650;
|
||||
color: #111827;
|
||||
letter-spacing: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.eyebrow {
|
||||
margin: 0;
|
||||
font-size: 13px;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.summary-strip {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, minmax(0, 1fr));
|
||||
gap: 12px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.summary-item {
|
||||
padding: 14px 16px;
|
||||
background: #fff;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 8px;
|
||||
|
||||
span {
|
||||
display: block;
|
||||
margin-bottom: 8px;
|
||||
font-size: 13px;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
strong {
|
||||
font-size: 24px;
|
||||
font-weight: 700;
|
||||
color: #111827;
|
||||
letter-spacing: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.table-shell {
|
||||
padding: 8px 0 14px;
|
||||
overflow: hidden;
|
||||
background: #fff;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.permission-table {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.role-cell {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 3px;
|
||||
|
||||
strong {
|
||||
font-weight: 650;
|
||||
color: #111827;
|
||||
}
|
||||
|
||||
span {
|
||||
font-size: 12px;
|
||||
color: #64748b;
|
||||
}
|
||||
}
|
||||
|
||||
.tag-list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.menu-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.menu-item {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 8px 10px;
|
||||
background: #f8fafc;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 8px;
|
||||
|
||||
strong {
|
||||
font-weight: 650;
|
||||
color: #111827;
|
||||
}
|
||||
|
||||
span {
|
||||
font-size: 13px;
|
||||
color: #64748b;
|
||||
}
|
||||
}
|
||||
|
||||
.muted-text {
|
||||
font-size: 13px;
|
||||
color: #94a3b8;
|
||||
}
|
||||
|
||||
.drawer-body {
|
||||
padding-right: 4px;
|
||||
}
|
||||
|
||||
.permission-group {
|
||||
padding: 14px 0;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
}
|
||||
|
||||
.permission-group:first-child {
|
||||
padding-top: 0;
|
||||
}
|
||||
|
||||
.permission-group:last-child {
|
||||
border-bottom: 0;
|
||||
}
|
||||
|
||||
.group-heading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 10px;
|
||||
|
||||
strong {
|
||||
font-size: 15px;
|
||||
color: #111827;
|
||||
}
|
||||
}
|
||||
|
||||
.permission-options {
|
||||
:deep(.el-checkbox-group) {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
:deep(.el-checkbox.is-bordered) {
|
||||
height: auto;
|
||||
margin-right: 0;
|
||||
padding: 10px 12px;
|
||||
}
|
||||
|
||||
:deep(.el-checkbox__label) {
|
||||
min-width: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.permission-option {
|
||||
display: inline-flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
min-width: 0;
|
||||
|
||||
strong {
|
||||
font-size: 14px;
|
||||
font-weight: 650;
|
||||
color: #111827;
|
||||
}
|
||||
|
||||
small {
|
||||
overflow: hidden;
|
||||
font-size: 12px;
|
||||
color: #64748b;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
}
|
||||
|
||||
.drawer-footer {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
@media (max-width: 760px) {
|
||||
.permission-page {
|
||||
padding: 12px;
|
||||
}
|
||||
|
||||
.page-heading {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.summary-strip {
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
}
|
||||
|
||||
.permission-options :deep(.el-checkbox-group) {
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
}
|
||||
|
||||
.menu-item {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user