fix(admin): 管理控制台界面使用element plus

This commit is contained in:
sophie committed 2026-10-20 17:01:24 +08:00
1 parent 6c47d0639c
commit cdc8610ec1
45 files changed
+6258 -4639

No files matched your search

@@ -1,24 +1,49 @@
/**
* features/admin/modules/permissions/PermissionsPage.vue
* 用途:应用权限页。已种子权限组时以角色包为主、矩阵只读;否则保留历史按人勾选。
* 依赖:usePermissionsAdmin、RoleEditModal、UserPickerDialog、PaginationBar、roleLabel、getUserAvatarLabel。
* 逻辑:路由 /admin/permissions;点人数打开选人弹窗,确定后必须确认增减才写库。
* 依赖:AdminListPage、AdminListTabs、usePermissionsAdmin、useElementProvider、element-plus、RoleEditModal、RoleMembersConfirmDialog、UserPickerDialog、BatchPermissionModal。
* 逻辑:路由 /admin/permissions。骨架用 AdminListPage。角色/矩阵用 AdminListTabs。
* 点人数打开选人弹窗,确定后必须确认增减才写库。删除和重写入口确认用 ElMessageBox。
*/
<script setup lang="ts">
import { onMounted } from 'vue';
import { computed, onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { Download, FileText, Plus, Shield, Users } from 'lucide-vue-next';
import { Download, Plus, Upload, User } from '@element-plus/icons-vue';
import {
ElButton,
ElCheckbox,
ElConfigProvider,
ElInput,
ElLoading,
ElOption,
ElPagination,
ElSelect,
ElSpace,
ElTable,
ElTableColumn,
ElTag,
} from 'element-plus';
import 'element-plus/dist/index.css';
import 'element-plus/theme-chalk/dark/css-vars.css';
import '@/features/admin/styles/element-trial.css';
import AdminListPage from '@/features/admin/components/AdminListPage.vue';
import AdminListTabs from '@/features/admin/components/AdminListTabs.vue';
import { useElementProvider } from '@/features/admin/composables/useElementProvider';
import BatchPermissionModal from '@/features/admin/modules/users/components/BatchPermissionModal.vue';
import PaginationBar from '@/shared/components/PaginationBar.vue';
import { getUserAvatarLabel } from '@/shared/utils/user-avatar';
import UserPickerDialog from '@/shared/components/UserPickerDialog.vue';
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
import type { AdminUserRow } from '@/features/admin/types/admin.types';
import type { PermissionRoleRow } from '@/features/admin/types/permissionRole.types';
import { adminRoleLabel, LEGACY_ROLE_OPTIONS } from '@/features/admin/utils/roleLabel';
import RoleEditModal from './components/RoleEditModal.vue';
import RoleMembersConfirmDialog from './components/RoleMembersConfirmDialog.vue';
import { usePermissionsAdmin } from './composables/usePermissionsAdmin';
const vLoading = ElLoading.directive;
const { t } = useI18n();
const { elementLocale } = useElementProvider();
const csvInputRef = ref<HTMLInputElement | null>(null);
const {
agents,
@@ -28,13 +53,13 @@ const {
permissionFilters,
selectedUserIds,
paginatedUsers,
allUsersSelectedOnPage,
permissionPage,
permissionPageSize,
permissionPageSizeOptions,
permissionTotalRows,
permissionTotalPages,
allUsersSelectedOnPage,
isLoading,
isListLoading,
isSaving,
activeTab,
rolesConfigured,
@@ -43,8 +68,6 @@ const {
showRoleEditor,
editingExisting,
roleDraft,
editingMemberCount,
showRoleSaveConfirm,
pickerRole,
showPicker,
pickerSelectedIds,
@@ -64,7 +87,6 @@ const {
openEditRole,
closeRoleEditor,
requestSaveRole,
persistRole,
removeRole,
openMembersPicker,
handlePickerConfirm,
@@ -72,285 +94,273 @@ const {
applyMembers,
} = usePermissionsAdmin();
/** 角色表只在已配置权限组、并且停在角色页时显示。 */
const showRoles = computed(() => rolesConfigured.value && activeTab.value === 'roles');
/** 未配置权限组时直接看矩阵;已配置时跟页签走。 */
const showMatrix = computed(() => !rolesConfigured.value || activeTab.value === 'matrix');
/** 角色表和权限矩阵两个页签。没有权限组时不渲染这一条。 */
const permissionTabs = computed(() => [
{ name: 'roles', label: t('admin.permRoles.tabs.roles') },
{ name: 'matrix', label: t('admin.permRoles.tabs.matrix') },
]);
/**
* 公共页签回传字符串,这里收成角色或矩阵。
* @param name - AdminListTabs 当前 name
*/
function onPermissionTab(name: string): void {
if (name === 'roles' || name === 'matrix') activeTab.value = name;
}
/**
* 角色筛选。清空时写回空字符串,矩阵仍按「所有角色」过滤。
* @param value - 选中的角色编码;清空不是字符串
*/
function onRoleFilterChange(value: unknown): void {
permissionFilters.value.role = typeof value === 'string' ? value : '';
}
/**
* 打开隐藏的 CSV 文件框,选中后仍走 importPermissions。
*/
function openCsvImport(): void {
csvInputRef.value?.click();
}
onMounted(() => {
void loadAll();
});
</script>
<template>
<section class="section-gap animate-fade-in-up">
<div v-if="isLoading" class="flex items-center justify-center py-20">
<div class="animate-spin rounded-full h-10 w-10 border-b-2 border-brand-600" />
</div>
<template v-else>
<div class="flex flex-col md:flex-row items-start md:items-center justify-between gap-4">
<div>
<h2 class="section-title">{{ t('admin.perm.title') }}</h2>
<p class="section-desc mt-1">
{{ rolesConfigured ? t('admin.permRoles.subtitle') : t('admin.perm.subtitle') }}
</p>
</div>
<div class="flex flex-wrap gap-3">
<button
<ElConfigProvider :locale="elementLocale">
<AdminListPage :loading="isLoading">
<template #title>
<h2 class="section-title">{{ t('admin.perm.title') }}</h2>
<p class="section-desc mt-1">
{{ rolesConfigured ? t('admin.permRoles.subtitle') : t('admin.perm.subtitle') }}
</p>
</template>
<template #actions>
<div class="perm-actions">
<ElSpace wrap>
<ElButton
v-if="!rolesConfigured && selectedUserIds.length > 0"
type="button"
class="flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-xl hover:bg-indigo-700 transition-all shadow-lg shadow-indigo-200 active:scale-95"
type="primary"
:icon="User"
@click="openBatchPermissionModal"
>
<Users class="w-4 h-4" />
<span class="font-medium">{{ t('admin.perm.batchAssign') }}</span>
</button>
<label
v-if="!rolesConfigured"
class="flex items-center gap-2 px-4 py-2 btn-secondary cursor-pointer text-sm font-medium transition-all shadow-sm"
>
<input type="file" accept=".csv" class="hidden" @change="importPermissions" />
<FileText class="w-4 h-4" />
{{ t('admin.perm.batchAssign') }}
</ElButton>
<ElButton v-if="!rolesConfigured" :icon="Upload" @click="openCsvImport">
{{ t('admin.perm.importCsv') }}
</label>
<button
type="button"
class="flex items-center gap-2 px-4 py-2 btn-secondary text-sm font-medium transition-all shadow-sm"
@click="exportPermissions"
>
<Download class="w-4 h-4" />
</ElButton>
<ElButton :icon="Download" @click="exportPermissions">
{{ t('admin.perm.exportCsv') }}
</button>
<button
v-if="rolesConfigured"
type="button"
class="flex items-center gap-2 px-4 py-2 bg-brand-600 text-white rounded-xl hover:bg-brand-700 text-sm font-medium"
@click="openCreateRole"
>
<Plus class="w-4 h-4" />
</ElButton>
<ElButton v-if="rolesConfigured" type="primary" :icon="Plus" @click="openCreateRole">
{{ t('admin.permRoles.create') }}
</button>
</ElButton>
</ElSpace>
<input
ref="csvInputRef"
type="file"
accept=".csv"
class="perm-csv-input"
@change="importPermissions"
/>
</div>
</div>
<div v-if="rolesConfigured" class="flex gap-1 border-b border-default">
<button
type="button"
class="px-4 py-2 text-sm font-medium"
:class="activeTab === 'roles' ? 'border-b-2 border-brand-600 text-brand-700' : 'text-muted'"
@click="activeTab = 'roles'"
</template>
<template v-if="rolesConfigured" #tabs>
<AdminListTabs :model-value="activeTab" :items="permissionTabs" @update:model-value="onPermissionTab" />
</template>
<template v-if="showMatrix" #filters>
<ElInput
v-model="permissionFilters.username"
clearable
class="perm-filter-search"
:placeholder="t('admin.perm.filterName')"
/>
<ElInput
v-model="permissionFilters.department"
clearable
class="perm-filter-search"
:placeholder="t('admin.perm.filterDept')"
/>
<ElSelect
:model-value="permissionFilters.role || undefined"
class="perm-filter-role"
clearable
:placeholder="t('admin.perm.allRoles')"
@update:model-value="onRoleFilterChange"
>
{{ t('admin.permRoles.tabs.roles') }}
</button>
<button
type="button"
class="px-4 py-2 text-sm font-medium"
:class="activeTab === 'matrix' ? 'border-b-2 border-brand-600 text-brand-700' : 'text-muted'"
@click="activeTab = 'matrix'"
<ElOption v-for="role in roles" :key="role.code" :label="role.name" :value="role.code" />
<template v-if="!roles.length">
<ElOption
v-for="option in LEGACY_ROLE_OPTIONS"
:key="option.code"
:label="t(option.i18nKey)"
:value="option.code"
/>
</template>
</ElSelect>
<span v-if="rolesConfigured" class="hx-element-hint perm-matrix-hint">
{{ t('admin.permRoles.matrixHint') }}
</span>
</template>
<ElTable
v-if="showRoles"
v-loading="isListLoading"
class="hx-admin-table"
:data="roles"
row-key="code"
:empty-text="t('admin.wecom.noData')"
>
<ElTableColumn :label="t('admin.permRoles.headers.name')" min-width="180">
<template #default="{ row }">
<div>{{ row.name }}</div>
<div v-if="row.is_default || row.is_platform_admin" class="perm-role-badges">
<ElTag v-if="row.is_default" effect="light">{{ t('admin.permRoles.badge.default') }}</ElTag>
<ElTag v-if="row.is_platform_admin" type="warning" effect="light">
{{ t('admin.permRoles.badge.admin') }}
</ElTag>
</div>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.permRoles.headers.description')" min-width="200">
<template #default="{ row }">
<span class="perm-secondary">{{ row.description || '-' }}</span>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.permRoles.headers.members')" width="120">
<template #default="{ row }">
<ElButton
v-if="!row.is_platform_admin"
link
type="primary"
@click="openMembersPicker(row as PermissionRoleRow)"
>
{{ t('admin.permRoles.memberCount', { count: row.member_count }) }}
</ElButton>
<span v-else class="perm-secondary">
{{ t('admin.permRoles.memberCount', { count: row.member_count }) }}
</span>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.permRoles.headers.apps')" min-width="220">
<template #default="{ row }">
<span class="perm-secondary">
{{
row.is_platform_admin
? t('admin.permRoles.allApps')
: (row.agent_names || []).join('、') || '-'
}}
</span>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.user.headers.actions')" width="160" align="right">
<template #default="{ row }">
<ElButton link type="primary" @click="openEditRole(row as PermissionRoleRow)">
{{ t('admin.edit') }}
</ElButton>
<ElButton
v-if="!row.is_system && !row.is_platform_admin"
link
type="danger"
@click="removeRole(row as PermissionRoleRow)"
>
{{ t('admin.delete') }}
</ElButton>
</template>
</ElTableColumn>
</ElTable>
<ElTable
v-if="showMatrix"
v-loading="isListLoading"
class="hx-admin-table"
:data="paginatedUsers"
row-key="id"
:empty-text="t('admin.user.noUsers')"
>
<ElTableColumn v-if="!rolesConfigured" width="64" align="center" fixed="left">
<template #header>
<ElCheckbox
class="perm-check"
:model-value="allUsersSelectedOnPage"
@change="toggleAllUsers"
/>
</template>
<template #default="{ row }">
<ElCheckbox
class="perm-check"
:model-value="selectedUserIds.includes(row.id)"
@change="toggleUserSelection(row.id)"
/>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.perm.headers.user')" min-width="200" fixed="left">
<template #default="{ row }">
<span class="perm-user">
<span class="perm-avatar">{{ getUserAvatarLabel(row) }}</span>
<span class="perm-ellipsis" :title="row.username">{{ row.username }}</span>
</span>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.perm.headers.realName')" min-width="140">
<template #default="{ row }">
<span class="perm-ellipsis" :title="row.real_name || '-'">{{ row.real_name || '-' }}</span>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.perm.headers.dept')" min-width="160">
<template #default="{ row }">
<span class="perm-secondary perm-ellipsis" :title="row.department || '-'">
{{ row.department || '-' }}
</span>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.user.headers.role')" min-width="140">
<template #default="{ row }">
{{ adminRoleLabel(t, row.role, row.role_name) }}
</template>
</ElTableColumn>
<ElTableColumn
v-for="agent in agents"
:key="agent.id"
:label="agent.name"
min-width="120"
align="center"
>
{{ t('admin.permRoles.tabs.matrix') }}
</button>
</div>
<template #header>
<div class="perm-agent-head">
<span>{{ agent.name }}</span>
<ElTag v-if="agent.type" effect="light">{{ agent.type }}</ElTag>
</div>
</template>
<template #default="{ row }">
<ElCheckbox
class="perm-check"
:model-value="userHasAgent(row as AdminUserRow, agent.id)"
:disabled="rolesConfigured"
@change="toggleUserAgentPermission(row as AdminUserRow, agent.id)"
/>
</template>
</ElTableColumn>
</ElTable>
<div v-if="rolesConfigured && activeTab === 'roles'" class="space-y-4">
<template v-if="rolesConfigured">
<div class="admin-table-section admin-table-wrap">
<table class="admin-table">
<thead>
<tr>
<th class="px-6 py-4 text-left text-sm">{{ t('admin.permRoles.headers.name') }}</th>
<th class="px-6 py-4 text-left text-sm">{{ t('admin.permRoles.headers.description') }}</th>
<th class="px-6 py-4 text-left text-sm">{{ t('admin.permRoles.headers.members') }}</th>
<th class="px-6 py-4 text-left text-sm">{{ t('admin.permRoles.headers.apps') }}</th>
<th class="px-6 py-4 text-right text-sm">{{ t('admin.user.headers.actions') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="role in roles" :key="role.code">
<td class="px-6 py-4">
<div class="font-medium text-primary">{{ role.name }}</div>
<div class="mt-1 flex flex-wrap gap-1.5">
<span
v-if="role.is_default"
class="inline-flex rounded-full border border-brand-100 bg-brand-50 px-2 py-0.5 text-[11px] text-brand-700"
>
{{ t('admin.permRoles.badge.default') }}
</span>
<span
v-if="role.is_platform_admin"
class="inline-flex items-center gap-1 rounded-full border border-purple-100 bg-purple-50 px-2 py-0.5 text-[11px] text-purple-700"
>
<Shield class="w-3 h-3" />
{{ t('admin.permRoles.badge.admin') }}
</span>
</div>
</td>
<td class="px-6 py-4 text-sm text-muted max-w-xs">{{ role.description || '-' }}</td>
<td class="px-6 py-4">
<button
v-if="!role.is_platform_admin"
type="button"
class="text-brand-600 hover:text-brand-800 font-semibold"
@click="openMembersPicker(role)"
>
{{ t('admin.permRoles.memberCount', { count: role.member_count }) }}
</button>
<span v-else class="text-sm text-muted">
{{ t('admin.permRoles.memberCount', { count: role.member_count }) }}
</span>
</td>
<td class="px-6 py-4 text-sm text-muted">
{{
role.is_platform_admin
? t('admin.permRoles.allApps')
: (role.agent_names || []).join('、') || '-'
}}
</td>
<td class="px-6 py-4 text-right whitespace-nowrap">
<button
type="button"
class="text-brand-600 hover:text-brand-900 font-medium text-xs px-3 py-1.5 rounded-lg hover:bg-brand-50"
@click="openEditRole(role)"
>
{{ t('admin.edit') }}
</button>
<button
v-if="!role.is_system && !role.is_platform_admin"
type="button"
class="text-red-600 hover:text-red-800 font-medium text-xs px-3 py-1.5 rounded-lg hover:bg-red-50"
@click="removeRole(role)"
>
{{ t('admin.delete') }}
</button>
</td>
</tr>
</tbody>
</table>
</div>
</template>
</div>
<template v-if="!rolesConfigured || activeTab === 'matrix'">
<p v-if="rolesConfigured" class="text-sm text-muted">{{ t('admin.permRoles.matrixHint') }}</p>
<div class="admin-table-filters--inline">
<input
v-model="permissionFilters.username"
:placeholder="t('admin.perm.filterName')"
class="admin-table-filter-input w-48"
/>
<input
v-model="permissionFilters.department"
:placeholder="t('admin.perm.filterDept')"
class="admin-table-filter-input w-48"
/>
<select v-model="permissionFilters.role" class="admin-table-filter-input w-40">
<option value="">{{ t('admin.perm.allRoles') }}</option>
<option v-for="role in roles" :key="role.code" :value="role.code">{{ role.name }}</option>
<template v-if="!roles.length">
<option
v-for="option in LEGACY_ROLE_OPTIONS"
:key="option.code"
:value="option.code"
>
{{ t(option.i18nKey) }}
</option>
</template>
</select>
</div>
<div class="admin-table-section admin-table-wrap">
<table class="admin-table">
<thead>
<tr>
<th v-if="!rolesConfigured" class="px-4 py-4 w-12 text-center sticky left-0 admin-table-sticky-head z-20">
<input
type="checkbox"
class="w-4 h-4 rounded text-brand-600 focus:ring-brand-500"
:checked="allUsersSelectedOnPage"
@change="toggleAllUsers"
/>
</th>
<th class="px-6 py-4 text-left text-sm sticky left-0 admin-table-sticky-head z-20 w-56">
{{ t('admin.perm.headers.user') }}
</th>
<th class="px-6 py-4 text-left text-sm w-36">{{ t('admin.perm.headers.realName') }}</th>
<th class="px-6 py-4 text-left text-sm w-40 border-r border-subtle">
{{ t('admin.perm.headers.dept') }}
</th>
<th class="px-6 py-4 text-left text-sm w-32">{{ t('admin.user.headers.role') }}</th>
<th
v-for="agent in agents"
:key="agent.id"
class="px-4 py-4 text-center text-sm min-w-[100px]"
>
<div class="flex flex-col items-center gap-1">
<span>{{ agent.name }}</span>
<span class="text-[10px] text-muted font-normal border border-default px-1 rounded">
{{ agent.type }}
</span>
</div>
</th>
</tr>
</thead>
<tbody>
<tr v-for="user in paginatedUsers" :key="user.id">
<td v-if="!rolesConfigured" class="px-4 py-4 text-center sticky left-0 admin-table-sticky-cell z-20">
<input
type="checkbox"
class="w-4 h-4 rounded text-brand-600 focus:ring-brand-500"
:checked="selectedUserIds.includes(user.id)"
@change="toggleUserSelection(user.id)"
/>
</td>
<td class="px-6 py-4 sticky left-0 admin-table-sticky-cell z-20">
<div class="flex items-center gap-3">
<div
class="w-8 h-8 rounded-full bg-gradient-to-br from-brand-100 to-indigo-100 text-brand-600 flex items-center justify-center font-bold text-sm"
>
{{ getUserAvatarLabel(user) }}
</div>
<div class="font-medium text-primary text-sm truncate max-w-[120px]" :title="user.username">
{{ user.username }}
</div>
</div>
</td>
<td class="px-6 py-4">
<div class="text-sm text-primary truncate" :title="user.real_name || '-'">
{{ user.real_name || '-' }}
</div>
</td>
<td class="px-6 py-4 border-r border-subtle">
<div class="text-sm text-muted truncate" :title="user.department">
{{ user.department || '-' }}
</div>
</td>
<td class="px-6 py-4 text-sm text-primary">
{{ adminRoleLabel(t, user.role, user.role_name) }}
</td>
<td
v-for="agent in agents"
:key="agent.id"
class="px-4 py-4 text-center border-l border-dashed border-subtle"
>
<input
type="checkbox"
class="admin-checkbox-lg"
:checked="userHasAgent(user, agent.id)"
:disabled="rolesConfigured"
@change="toggleUserAgentPermission(user, agent.id)"
/>
</td>
</tr>
</tbody>
</table>
</div>
<PaginationBar
v-model:page="permissionPage"
<template v-if="showMatrix" #pagination>
<ElPagination
v-model:current-page="permissionPage"
v-model:page-size="permissionPageSize"
:total="permissionTotalRows"
:pages="permissionTotalPages"
:page-size-options="permissionPageSizeOptions"
class="mt-5"
:page-sizes="permissionPageSizeOptions"
layout="total, sizes, prev, pager, next"
background
/>
</template>
</template>
</AdminListPage>
<BatchPermissionModal
:visible="showBatchPermissionModal"
@@ -374,16 +384,6 @@ onMounted(() => {
@update:draft="roleDraft = $event"
/>
<DeleteConfirmDialog
v-model="showRoleSaveConfirm"
:title="t('admin.permRoles.saveConfirm.title')"
:message="t('admin.permRoles.saveConfirm.message', { count: editingMemberCount })"
:confirm-text="t('admin.save')"
confirm-tone="primary"
:confirm-loading="isSaving"
@confirm="persistRole"
/>
<UserPickerDialog
v-model="showPicker"
:users="pickerUsers"
@@ -400,5 +400,84 @@ onMounted(() => {
@cancel="cancelMembersConfirm"
@confirm="applyMembers"
/>
</section>
</ElConfigProvider>
</template>
<style scoped>
.perm-actions {
display: flex;
justify-content: flex-end;
}
.perm-csv-input {
display: none;
}
:deep(.perm-filter-search) {
width: 16rem;
}
:deep(.perm-filter-role) {
width: 11rem;
}
.perm-matrix-hint {
margin-top: 0;
flex: 1 1 12rem;
min-width: 12rem;
}
.perm-role-badges {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 4px;
}
.perm-secondary {
color: var(--el-text-color-secondary);
}
.perm-user {
display: inline-flex;
align-items: center;
gap: 12px;
max-width: 100%;
min-width: 0;
}
.perm-avatar {
display: flex;
height: 2.25rem;
width: 2.25rem;
flex: none;
align-items: center;
justify-content: center;
border-radius: 9999px;
background-image: linear-gradient(to bottom right, #0ea5e9, #4f46e5);
font-size: var(--el-font-size-extra-small);
font-weight: 700;
color: #fff;
}
.perm-ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
min-width: 0;
}
.perm-agent-head {
display: flex;
flex-direction: column;
align-items: center;
gap: 4px;
line-height: 1.3;
}
.perm-check :deep(.el-checkbox) {
height: auto;
margin-right: 0;
vertical-align: middle;
}
</style>