fix(admin): admin管理页面优化,增加排序过滤等

This commit is contained in:
sophie committed 2026-07-10 14:10:49 +08:00
1 parent 0939aeb0d8
commit fc843905d1
31 files changed
+1090 -671

No files matched your search

@@ -1,25 +1,36 @@
/**
* features/admin/modules/departments/DepartmentsPage.vue
* 部门管理独立页面。路由:/admin/departments
* 用途:部门管理独立页面——全宽表格列表 + 列排序 + 创建/编辑居中弹窗。
* 依赖:useDepartmentsAdmin、SortableTableHeader、DepartmentFormModal、DeleteConfirmDialog。
* 逻辑:路由 /admin/departments;表头排序复用 shared SortableTableHeader + useTableSort。
*/
<script setup lang="ts">
import { onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { Edit, Trash2 } from 'lucide-vue-next';
import { Edit, Plus, Search, Trash2 } from 'lucide-vue-next';
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
import SortableTableHeader from '@/shared/components/SortableTableHeader.vue';
import DepartmentFormModal from './components/DepartmentFormModal.vue';
import { useDepartmentsAdmin } from './composables/useDepartmentsAdmin';
const { t } = useI18n();
const {
departments,
editingDept,
filteredDepartments,
form,
searchQuery,
sortKey,
sortDir,
isLoading,
isSaving,
showFormModal,
showDeleteConfirmDialog,
deleteConfirmMessage,
loadAll,
resetForm,
editDepartment,
toggleSort,
openCreateModal,
openEditModal,
closeFormModal,
saveDepartment,
deleteDepartment,
closeDeleteConfirm,
@@ -32,127 +43,137 @@ onMounted(() => {
</script>
<template>
<section class="section-gap animate-fade-in-up">
<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>
<template v-else>
<div class="flex items-center justify-between">
<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="grid grid-cols-1 lg:grid-cols-3 gap-6">
<div class="lg:col-span-1 card p-6 shadow-sm h-fit">
<h3 class="font-bold text-primary mb-4">
{{ editingDept.id ? t('admin.dept.edit') : t('admin.dept.add') }}
</h3>
<div class="form-stack">
<div class="space-y-1.5">
<label class="label-field block">{{ t('admin.dept.fields.name') }}</label>
<input
v-model="editingDept.name"
class="input-field text-sm"
:placeholder="t('admin.dept.fields.namePlaceholder')"
/>
</div>
<div class="space-y-1.5">
<label class="label-field">{{ t('admin.dept.fields.code') }}</label>
<input
v-model="editingDept.code"
class="input-field text-sm"
:placeholder="t('admin.dept.fields.codePlaceholder')"
/>
</div>
<div class="space-y-1.5">
<label class="label-field">{{ t('admin.dept.fields.company') }}</label>
<input
v-model="editingDept.company"
class="input-field text-sm"
:placeholder="t('admin.dept.fields.companyPlaceholder')"
/>
</div>
<div class="space-y-1.5">
<label class="label-field">{{ t('admin.dept.fields.description') }}</label>
<textarea
v-model="editingDept.description"
rows="3"
class="input-field text-sm"
:placeholder="t('admin.dept.fields.descriptionPlaceholder')"
/>
</div>
<div class="flex justify-end gap-2 pt-2">
<button
v-if="editingDept.id"
type="button"
class="btn-ghost btn-sm"
@click="resetForm"
>
{{ t('admin.cancel') }}
</button>
<button type="button" class="btn-primary btn-sm" @click="saveDepartment">
{{ editingDept.id ? t('admin.update') : t('admin.add') }}
</button>
</div>
</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="lg:col-span-2 admin-table-section">
<div v-if="departments.length === 0" class="p-8 text-center text-muted">
{{ t('admin.dept.noDepts') }}
</div>
<table v-else class="admin-table">
<thead>
<tr>
<th class="px-6 py-3 text-xs font-semibold text-muted uppercase">
{{ t('admin.dept.headers.name') }}
</th>
<th class="px-6 py-3 text-xs font-semibold text-muted uppercase">
{{ t('admin.dept.headers.code') }}
</th>
<th class="px-6 py-3 text-xs font-semibold text-muted uppercase">
{{ t('admin.dept.headers.company') }}
</th>
<th class="px-6 py-3 text-xs font-semibold text-muted uppercase text-right">
{{ t('admin.dept.headers.actions') }}
</th>
</tr>
</thead>
<tbody class="divide-y divide-subtle">
<tr v-for="d in departments" :key="d.id">
<td class="px-6 py-4">
<div class="font-medium text-primary text-sm">{{ d.name }}</div>
<div class="text-xs text-muted truncate max-w-xs mt-0.5">{{ d.description }}</div>
</td>
<td class="px-6 py-4 text-sm text-muted font-mono">{{ d.code || '-' }}</td>
<td class="px-6 py-4 text-sm text-muted">{{ d.company || '-' }}</td>
<td class="px-6 py-4 text-right flex justify-end gap-2">
<div class="admin-table-wrap">
<!-- 列宽:名称 / 编码 / 公司 / 操作 -->
<table class="admin-table table-fixed">
<colgroup>
<col class="w-[25%]" />
<col class="w-[20%]" />
<col class="w-[35%]" />
<col class="w-[15%]" />
</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-right text-xs">{{ t('admin.dept.headers.actions') }}</th>
</tr>
</thead>
<tbody>
<tr v-if="filteredDepartments.length === 0">
<td colspan="4" 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">
<div class="flex justify-end gap-1">
<button
type="button"
class="p-1.5 text-blue-600 hover:bg-blue-50 rounded-lg transition-colors"
@click="editDepartment(d)"
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>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<DepartmentFormModal
:visible="showFormModal"
:form="form"
:is-saving="isSaving"
@close="closeFormModal"
@save="saveDepartment"
/>
<DeleteConfirmDialog
v-model="showDeleteConfirmDialog"
:message="deleteConfirmMessage"
@@ -0,0 +1,94 @@
/**
* features/admin/modules/departments/components/DepartmentFormModal.vue
* 用途:部门创建/编辑居中弹窗。
* 依赖:DepartmentFormState、admin 通用 modal 样式。
* 逻辑:DepartmentsPage 打开本组件;提交时 emit save,由 useDepartmentsAdmin 调 API。
*/
<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import type { DepartmentFormState } from '@/features/admin/types/department.types';
defineProps<{
visible: boolean;
form: DepartmentFormState;
isSaving: boolean;
}>();
const emit = defineEmits<{
close: [];
save: [];
}>();
const { t } = useI18n();
</script>
<template>
<div v-if="visible" class="admin-modal-overlay animate-fade-in items-center">
<div class="admin-modal-panel max-w-lg max-h-[92vh] scale-100">
<div class="px-5 sm:px-8 pt-5 sm:pt-7 pb-4 border-b border-subtle">
<h3 class="section-title">
{{ form.id ? t('admin.dept.edit') : t('admin.dept.add') }}
</h3>
</div>
<div class="px-5 sm:px-8 py-5 space-y-4 overflow-y-auto">
<div class="space-y-1.5">
<label class="label-field">{{ t('admin.dept.fields.name') }}</label>
<input
v-model="form.name"
type="text"
class="input-field text-sm"
:placeholder="t('admin.dept.fields.namePlaceholder')"
/>
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div class="space-y-1.5">
<label class="label-field">{{ t('admin.dept.fields.code') }}</label>
<input
v-model="form.code"
type="text"
class="input-field text-sm"
:placeholder="t('admin.dept.fields.codePlaceholder')"
/>
</div>
<div class="space-y-1.5">
<label class="label-field">{{ t('admin.dept.fields.company') }}</label>
<input
v-model="form.company"
type="text"
class="input-field text-sm"
:placeholder="t('admin.dept.fields.companyPlaceholder')"
/>
</div>
</div>
<div class="space-y-1.5">
<label class="label-field">{{ t('admin.dept.fields.description') }}</label>
<textarea
v-model="form.description"
rows="3"
class="input-field text-sm resize-y"
:placeholder="t('admin.dept.fields.descriptionPlaceholder')"
/>
</div>
</div>
<div class="admin-modal-footer">
<button type="button" class="btn-ghost" @click="emit('close')">
{{ t('admin.cancel') }}
</button>
<button
type="button"
class="btn-primary disabled:opacity-70"
:disabled="isSaving"
@click="emit('save')"
>
{{
isSaving
? t('admin.save') + '...'
: form.id
? t('admin.update')
: t('admin.add')
}}
</button>
</div>
</div>
</div>
</template>
@@ -1,8 +1,10 @@
/**
* features/admin/modules/departments/composables/useDepartmentsAdmin.ts
* 部门管理:列表、创建、编辑、删除。
* 用途:部门管理列表、搜索过滤、列排序、创建/编辑弹窗、删除。
* 依赖:admin.api、department.types、useMessage、useTableSort。
* 逻辑:DepartmentsPage 调用本 composable;排序状态委托 shared useTableSort。
*/
import { ref } from 'vue';
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import {
createAdminDepartment,
@@ -11,24 +13,63 @@ import {
updateAdminDepartment,
} from '@/features/admin/api/admin.api';
import { useMessage } from '@/shared/composables/useMessage';
import { useTableSort } from '@/shared/composables/useTableSort';
import type { AdminDepartmentRow } from '@/features/admin/types/admin.types';
import {
createEmptyDepartmentForm,
departmentRowToForm,
type DepartmentFormState,
} from '@/features/admin/types/department.types';
/** 部门表可排序列 */
export type DeptSortKey = 'name' | 'code' | 'company';
export function useDepartmentsAdmin() {
const { t } = useI18n();
const msg = useMessage();
const departments = ref<AdminDepartmentRow[]>([]);
const editingDept = ref<DepartmentFormState>(createEmptyDepartmentForm());
const form = ref<DepartmentFormState>(createEmptyDepartmentForm());
const searchQuery = ref('');
const { sortKey, sortDir, toggleSort, sortItems } = useTableSort<DeptSortKey>({
initialKey: 'name',
initialDir: 'asc',
});
const isLoading = ref(true);
const isSaving = ref(false);
const showFormModal = ref(false);
const showDeleteConfirmDialog = ref(false);
const deleteConfirmMessage = ref('');
let deleteConfirmAction: (() => Promise<void> | void) | null = null;
/**
* 先按关键词过滤,再按当前排序列排序。
* 核心逻辑:模糊匹配四字段 → useTableSort.sortItems。
*/
const filteredDepartments = computed(() => {
const q = searchQuery.value.trim().toLowerCase();
const filtered = q
? departments.value.filter((d) => {
const haystack = [d.name, d.code, d.company, d.description]
.filter(Boolean)
.join(' ')
.toLowerCase();
return haystack.includes(q);
})
: departments.value;
return sortItems(
filtered,
(row, key) => row[key],
(a, b) => a.id - b.id,
);
});
/**
* 拉取部门列表并写入 departments。
* @returns Promise<void>
*/
const loadDepartments = async (): Promise<void> => {
try {
departments.value = await listAdminDepartments();
@@ -38,23 +79,37 @@ export function useDepartmentsAdmin() {
}
};
/**
* 页面初始化:加载部门列表并结束 loading。
* @returns Promise<void>
*/
const loadAll = async (): Promise<void> => {
isLoading.value = true;
await loadDepartments();
isLoading.value = false;
};
/**
* 打开删除确认对话框并绑定确认后的删除动作。
* @param message - 确认文案
* @param action - 用户确认后执行的异步删除逻辑
*/
const openDeleteConfirm = (message: string, action: () => Promise<void> | void): void => {
deleteConfirmMessage.value = message;
deleteConfirmAction = action;
showDeleteConfirmDialog.value = true;
};
/** 关闭删除确认对话框并清空待执行动作。 */
const closeDeleteConfirm = (): void => {
showDeleteConfirmDialog.value = false;
deleteConfirmAction = null;
};
/**
* 执行删除确认对话框中绑定的删除动作。
* @returns Promise<void>
*/
const confirmDelete = async (): Promise<void> => {
if (!deleteConfirmAction) return;
try {
@@ -64,44 +119,63 @@ export function useDepartmentsAdmin() {
}
};
const resetForm = (): void => {
editingDept.value = createEmptyDepartmentForm();
/** 打开「添加部门」弹窗并重置表单。 */
const openCreateModal = (): void => {
form.value = createEmptyDepartmentForm();
showFormModal.value = true;
};
const editDepartment = (dept: AdminDepartmentRow): void => {
editingDept.value = {
id: dept.id,
name: dept.name,
code: dept.code || '',
company: dept.company || '',
description: dept.description || '',
};
/**
* 打开「编辑部门」弹窗并回填选中行数据。
* @param dept - 待编辑的部门行
*/
const openEditModal = (dept: AdminDepartmentRow): void => {
form.value = departmentRowToForm(dept);
showFormModal.value = true;
};
/** 关闭创建/编辑弹窗并重置表单。 */
const closeFormModal = (): void => {
showFormModal.value = false;
form.value = createEmptyDepartmentForm();
};
/**
* 保存部门:有 id 则更新,否则新建。
* 核心逻辑:校验名称 → 调 create/update API → 刷新列表并关闭弹窗。
* @returns Promise<void>
*/
const saveDepartment = async (): Promise<void> => {
if (!editingDept.value.name?.trim()) {
if (!form.value.name?.trim()) {
msg.warning(t('admin.dept.msg.nameRequired'));
return;
}
isSaving.value = true;
try {
const payload = {
name: editingDept.value.name,
code: editingDept.value.code,
company: editingDept.value.company,
description: editingDept.value.description,
name: form.value.name.trim(),
code: form.value.code?.trim() || '',
company: form.value.company?.trim() || '',
description: form.value.description?.trim() || '',
};
if (editingDept.value.id) {
await updateAdminDepartment(editingDept.value.id, payload);
if (form.value.id) {
await updateAdminDepartment(form.value.id, payload);
} else {
await createAdminDepartment(payload);
}
closeFormModal();
await loadDepartments();
resetForm();
} catch (e) {
msg.error((e as Error).message || t('admin.msg.failed'));
} finally {
isSaving.value = false;
}
};
/**
* 删除指定部门,删除前弹出确认框。
* @param id - 部门主键
*/
const deleteDepartment = (id: number): void => {
openDeleteConfirm(t('admin.dept.confirmDelete'), async () => {
try {
@@ -116,13 +190,21 @@ export function useDepartmentsAdmin() {
return {
departments,
editingDept,
filteredDepartments,
form,
searchQuery,
sortKey,
sortDir,
isLoading,
isSaving,
showFormModal,
showDeleteConfirmDialog,
deleteConfirmMessage,
loadAll,
resetForm,
editDepartment,
toggleSort,
openCreateModal,
openEditModal,
closeFormModal,
saveDepartment,
deleteDepartment,
closeDeleteConfirm,