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,41 @@
/**
* features/admin/modules/departments/DepartmentsPage.vue
* 用途:部门管理独立页面——全宽表格列表 + 列排序 + 创建/编辑居中弹窗。
* 依赖:useDepartmentsAdmin、SortableTableHeader、DepartmentFormModal、DeleteConfirmDialog。
* 逻辑:路由 /admin/departments;表头排序复用 shared SortableTableHeader + useTableSort。
* 用途:部门管理列表。一张卡片里放标题、添加按钮、搜索、表格。
* 依赖:AdminListPage、useDepartmentsAdmin、useElementProvider、element-plus、lucide-vue-next、部门表单和员工名单弹窗。
* 逻辑:路由 /admin/departments。骨架用 AdminListPage。筛选、列和两个弹窗留在本页。
* 名称、编码、公司用表头排序,排的是当前过滤结果。删除用 Element 确认框。
* 表格用 Element stripe 斑马纹。员工、编辑、删除保持图标按钮,悬停文案走 i18n title。
*/
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { computed, onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { Edit, Plus, Search, Trash2, Users } from 'lucide-vue-next';
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
import SortableTableHeader from '@/shared/components/SortableTableHeader.vue';
import { Plus } from '@element-plus/icons-vue';
import { Edit, Trash2, Users } from 'lucide-vue-next';
import {
ElButton,
ElConfigProvider,
ElInput,
ElLoading,
ElTable,
ElTableColumn,
} 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 { useElementProvider } from '@/features/admin/composables/useElementProvider';
import type { AdminDepartmentMembersResponse, AdminDepartmentRow } from '@/features/admin/types/admin.types';
import DepartmentFormModal from './components/DepartmentFormModal.vue';
import DepartmentMembersModal from './components/DepartmentMembersModal.vue';
import { useDepartmentsAdmin } from './composables/useDepartmentsAdmin';
import { useDepartmentsAdmin, type DeptSortKey } from './composables/useDepartmentsAdmin';
import { listAdminDepartmentMembers } from '@/features/admin/api/admin.api';
import type { AdminDepartmentMembersResponse } from '@/features/admin/types/admin.types';
import { useMessage } from '@/shared/composables/useMessage';
const vLoading = ElLoading.directive;
const { t } = useI18n();
const msg = useMessage();
const { elementLocale } = useElementProvider();
const showMembersModal = ref(false);
const departmentMembers = ref<AdminDepartmentMembersResponse | null>(null);
@@ -29,169 +46,152 @@ const {
sortKey,
sortDir,
isLoading,
isListLoading,
isSaving,
showFormModal,
showDeleteConfirmDialog,
deleteConfirmMessage,
loadAll,
toggleSort,
applySort,
openCreateModal,
openEditModal,
closeFormModal,
saveDepartment,
deleteDepartment,
closeDeleteConfirm,
confirmDelete,
} = useDepartmentsAdmin();
onMounted(() => {
void loadAll();
});
const defaultSort = computed(() => ({
prop: sortKey.value || 'name',
order: sortDir.value === 'desc' ? 'descending' as const : 'ascending' as const,
}));
/** 加载并展示指定部门的员工名单。 */
const openDepartmentMembers = async (departmentId: number): Promise<void> => {
const emptyText = computed(() => (
searchQuery.value.trim() ? t('admin.dept.noSearchResults') : t('admin.dept.noDepts')
));
/**
* 把 Element 表头排序交给本地列表。
* 核心逻辑:只接受名称、编码、公司;第三次点击 order 为空时清空排序。
* @param payload.prop - 列字段
* @param payload.order - ascending、descending 或空
*/
function onSortChange(payload: { prop: string | null; order: string | null }): void {
const field = payload.prop;
const sortable = field === 'name' || field === 'code' || field === 'company';
if (sortable && payload.order) {
applySort(field as DeptSortKey, payload.order === 'descending' ? 'desc' : 'asc');
return;
}
applySort('', '');
}
/**
* 打开编辑弹窗。表格插槽行类型较宽,这里收成部门行。
* @param row - 当前表格行
*/
function editDepartment(row: AdminDepartmentRow): void {
openEditModal(row);
}
/**
* 加载并展示指定部门的员工名单。
* 核心逻辑:请求成功才打开弹窗;失败只提示,不打开空名单。
* @param departmentId - 部门主键
*/
async function openDepartmentMembers(departmentId: number): Promise<void> {
try {
departmentMembers.value = await listAdminDepartmentMembers(departmentId) as AdminDepartmentMembersResponse;
showMembersModal.value = true;
} catch (error) {
msg.error((error as Error).message || t('admin.msg.requestFailed'));
}
};
}
onMounted(() => {
void loadAll();
});
</script>
<template>
<section class="admin-table-section 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>
<ElConfigProvider :locale="elementLocale">
<AdminListPage :loading="isLoading">
<template #title>
<h2 class="section-title">{{ t('admin.dept.title') }}</h2>
<p class="section-desc mt-1">{{ t('admin.dept.subtitle') }}</p>
</template>
<template #actions>
<ElButton type="primary" :icon="Plus" @click="openCreateModal">
{{ t('admin.dept.add') }}
</ElButton>
</template>
<template #filters>
<ElInput
v-model="searchQuery"
clearable
class="dept-filter-search"
:placeholder="t('admin.dept.searchPlaceholder')"
/>
</template>
<template v-else>
<div class="admin-table-header admin-table-header--stacked">
<div>
<h2 class="section-title">{{ t('admin.dept.title') }}</h2>
<p class="section-desc mt-1">{{ t('admin.dept.subtitle') }}</p>
</div>
<button
type="button"
class="flex items-center gap-2 px-4 py-2 bg-brand-600 text-white rounded-xl hover:bg-brand-700 transition-all shadow-lg shadow-brand-200 active:scale-95 text-sm font-medium"
@click="openCreateModal"
>
<Plus class="w-4 h-4" />
<span>{{ t('admin.dept.add') }}</span>
</button>
</div>
<div class="admin-table-filters--inline gap-3">
<div class="relative flex-1 min-w-[200px] max-w-sm">
<Search class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted pointer-events-none" />
<input
v-model="searchQuery"
type="search"
class="admin-table-filter-input w-full pl-9"
:placeholder="t('admin.dept.searchPlaceholder')"
/>
</div>
</div>
<div class="admin-table-wrap">
<!-- 列宽:名称 / 编码 / 公司 / 员工 / 操作 -->
<table class="admin-table table-fixed">
<colgroup>
<col class="w-[25%]" />
<col class="w-[20%]" />
<col class="w-[30%]" />
<col class="w-[12%]" />
<col class="w-[13%]" />
</colgroup>
<thead>
<tr>
<th class="px-6 py-4 text-left text-xs">
<SortableTableHeader
column-key="name"
:label="t('admin.dept.headers.name')"
:active-key="sortKey"
:direction="sortDir"
@sort="toggleSort"
/>
</th>
<th class="px-6 py-4 text-left text-xs">
<SortableTableHeader
column-key="code"
:label="t('admin.dept.headers.code')"
:active-key="sortKey"
:direction="sortDir"
@sort="toggleSort"
/>
</th>
<th class="px-6 py-4 text-left text-xs">
<SortableTableHeader
column-key="company"
:label="t('admin.dept.headers.company')"
:active-key="sortKey"
:direction="sortDir"
@sort="toggleSort"
/>
</th>
<th class="px-6 py-4 text-center text-xs">{{ t('admin.dept.headers.members') }}</th>
<th class="px-6 py-4 text-right text-xs">{{ t('admin.dept.headers.actions') }}</th>
</tr>
</thead>
<tbody>
<tr v-if="filteredDepartments.length === 0">
<td colspan="5" class="px-6 admin-table-empty">
{{ searchQuery.trim() ? t('admin.dept.noSearchResults') : t('admin.dept.noDepts') }}
</td>
</tr>
<tr v-for="d in filteredDepartments" :key="d.id" class="group">
<td class="px-6 py-4">
<div class="font-medium text-primary text-sm truncate" :title="d.name">{{ d.name }}</div>
<div
v-if="d.description"
class="text-xs text-muted truncate mt-0.5"
:title="d.description"
>
{{ d.description }}
</div>
</td>
<td class="px-6 py-4 text-sm text-muted font-mono whitespace-nowrap truncate" :title="d.code || '-'">
{{ d.code || '-' }}
</td>
<td class="px-6 py-4 text-sm text-muted truncate" :title="d.company || '-'">
{{ d.company || '-' }}
</td>
<td class="px-6 py-4 text-center">
<button
type="button"
class="inline-flex items-center justify-center rounded-lg bg-blue-50 p-2 text-blue-600 hover:bg-blue-100"
:title="t('admin.dept.actions.viewMembers')"
@click="openDepartmentMembers(d.id)"
><Users class="w-5 h-5" /><span class="ml-1 text-xs">{{ d.member_count || 0 }}</span></button>
</td>
<td class="px-6 py-4">
<div class="flex justify-end gap-1">
<button
type="button"
class="p-1.5 text-indigo-600 hover:bg-indigo-50 rounded-lg transition-colors"
:title="t('admin.dept.actions.edit')"
@click="openEditModal(d)"
>
<Edit class="w-4 h-4" />
</button>
<button
type="button"
class="p-1.5 text-red-600 hover:bg-red-50 rounded-lg transition-colors"
:title="t('admin.dept.actions.delete')"
@click="deleteDepartment(d.id)"
>
<Trash2 class="w-4 h-4" />
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<ElTable
v-loading="isListLoading"
class="hx-admin-table"
:data="filteredDepartments"
row-key="id"
stripe
:default-sort="defaultSort"
:empty-text="emptyText"
@sort-change="onSortChange"
>
<ElTableColumn prop="name" :label="t('admin.dept.headers.name')" sortable="custom" min-width="200">
<template #default="{ row }">
<div :title="row.name">{{ row.name }}</div>
<p v-if="row.description" class="hx-element-hint dept-desc" :title="row.description">
{{ row.description }}
</p>
</template>
</ElTableColumn>
<ElTableColumn prop="code" :label="t('admin.dept.headers.code')" sortable="custom" min-width="140">
<template #default="{ row }">{{ row.code || '-' }}</template>
</ElTableColumn>
<ElTableColumn prop="company" :label="t('admin.dept.headers.company')" sortable="custom" min-width="180">
<template #default="{ row }">{{ row.company || '-' }}</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.dept.headers.members')" width="120" align="center">
<template #default="{ row }">
<button
type="button"
class="dept-member-btn"
:title="t('admin.dept.actions.viewMembers')"
@click="openDepartmentMembers((row as AdminDepartmentRow).id)"
>
<Users class="dept-member-icon" />
<span>{{ row.member_count || 0 }}</span>
</button>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.dept.headers.actions')" width="120" align="right">
<template #default="{ row }">
<div class="dept-row-actions">
<button
type="button"
class="dept-icon-btn dept-icon-btn--edit"
:title="t('admin.dept.actions.edit')"
@click="editDepartment(row as AdminDepartmentRow)"
>
<Edit class="dept-action-icon" />
</button>
<button
type="button"
class="dept-icon-btn dept-icon-btn--delete"
:title="t('admin.dept.actions.delete')"
@click="deleteDepartment((row as AdminDepartmentRow).id)"
>
<Trash2 class="dept-action-icon" />
</button>
</div>
</template>
</ElTableColumn>
</ElTable>
</AdminListPage>
<DepartmentFormModal
:visible="showFormModal"
@@ -205,12 +205,83 @@ const openDepartmentMembers = async (departmentId: number): Promise<void> => {
:data="departmentMembers"
@close="showMembersModal = false"
/>
<DeleteConfirmDialog
v-model="showDeleteConfirmDialog"
:message="deleteConfirmMessage"
@cancel="closeDeleteConfirm"
@confirm="confirmDelete"
/>
</section>
</ElConfigProvider>
</template>
<style scoped>
:deep(.dept-filter-search) {
width: 16rem;
}
.dept-desc {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.dept-member-btn,
.dept-icon-btn {
border: 0;
cursor: pointer;
line-height: 0;
}
.dept-member-btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 8px;
border-radius: 8px;
background: #eff6ff;
color: #2563eb;
}
.dept-member-btn:hover {
background: #dbeafe;
}
.dept-member-icon {
width: 20px;
height: 20px;
}
.dept-member-btn span {
margin-left: 4px;
font-size: 12px;
line-height: 1;
}
.dept-row-actions {
display: inline-flex;
justify-content: flex-end;
gap: 4px;
}
.dept-icon-btn {
padding: 6px;
border-radius: 8px;
background: transparent;
transition: background-color 0.15s ease;
}
.dept-action-icon {
width: 16px;
height: 16px;
}
.dept-icon-btn--edit {
color: #4f46e5;
}
.dept-icon-btn--edit:hover {
background: #eef2ff;
}
.dept-icon-btn--delete {
color: #dc2626;
}
.dept-icon-btn--delete:hover {
background: #fef2f2;
}
</style>