refactor: 重构前端骨架,及admin模块子路由和懒加载

refactor: 优化多次请求,并使用pinia状态管理
This commit is contained in:
sophie committed 2026-06-03 10:30:16 +08:00
1 parent f877f61efd
commit 61693d014d
74 files changed
+9493 -6418

No files matched your search

@@ -0,0 +1,163 @@
/**
* features/admin/modules/departments/DepartmentsPage.vue
* 部门管理独立页面。路由:/admin/departments
*/
<script setup lang="ts">
import { onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { Edit, Trash2 } from 'lucide-vue-next';
import DeleteConfirmDialog from '@/components/DeleteConfirmDialog.vue';
import { useDepartmentsAdmin } from './composables/useDepartmentsAdmin';
const { t } = useI18n();
const {
departments,
editingDept,
isLoading,
showDeleteConfirmDialog,
deleteConfirmMessage,
loadAll,
resetForm,
editDepartment,
saveDepartment,
deleteDepartment,
closeDeleteConfirm,
confirmDelete,
} = useDepartmentsAdmin();
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 items-center justify-between">
<div>
<h2 class="section-title">{{ t('admin.dept.title') }}</h2>
<p class="section-desc mt-1">{{ t('admin.dept.subtitle') }}</p>
</div>
</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>
<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">
<button
type="button"
class="p-1.5 text-blue-600 hover:bg-blue-50 rounded-lg transition-colors"
@click="editDepartment(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"
@click="deleteDepartment(d.id)"
>
<Trash2 class="w-4 h-4" />
</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<DeleteConfirmDialog
v-model="showDeleteConfirmDialog"
:message="deleteConfirmMessage"
@cancel="closeDeleteConfirm"
@confirm="confirmDelete"
/>
</section>
</template>
@@ -0,0 +1,131 @@
/**
* features/admin/modules/departments/composables/useDepartmentsAdmin.ts
* 部门管理:列表、创建、编辑、删除。
*/
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import {
createAdminDepartment,
deleteAdminDepartment,
listAdminDepartments,
updateAdminDepartment,
} from '@/api/admin';
import { useMessage } from '@/composables/useMessage';
import type { AdminDepartmentRow } from '@/features/admin/types/admin.types';
import {
createEmptyDepartmentForm,
type DepartmentFormState,
} from '@/features/admin/types/department.types';
export function useDepartmentsAdmin() {
const { t } = useI18n();
const msg = useMessage();
const departments = ref<AdminDepartmentRow[]>([]);
const editingDept = ref<DepartmentFormState>(createEmptyDepartmentForm());
const isLoading = ref(true);
const showDeleteConfirmDialog = ref(false);
const deleteConfirmMessage = ref('');
let deleteConfirmAction: (() => Promise<void> | void) | null = null;
const loadDepartments = async (): Promise<void> => {
try {
departments.value = await listAdminDepartments();
} catch (e) {
console.error('Failed to load departments', e);
departments.value = [];
}
};
const loadAll = async (): Promise<void> => {
isLoading.value = true;
await loadDepartments();
isLoading.value = false;
};
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;
};
const confirmDelete = async (): Promise<void> => {
if (!deleteConfirmAction) return;
try {
await deleteConfirmAction();
} finally {
closeDeleteConfirm();
}
};
const resetForm = (): void => {
editingDept.value = createEmptyDepartmentForm();
};
const editDepartment = (dept: AdminDepartmentRow): void => {
editingDept.value = {
id: dept.id,
name: dept.name,
code: dept.code || '',
company: dept.company || '',
description: dept.description || '',
};
};
const saveDepartment = async (): Promise<void> => {
if (!editingDept.value.name?.trim()) {
msg.warning(t('admin.dept.msg.nameRequired'));
return;
}
try {
const payload = {
name: editingDept.value.name,
code: editingDept.value.code,
company: editingDept.value.company,
description: editingDept.value.description,
};
if (editingDept.value.id) {
await updateAdminDepartment(editingDept.value.id, payload);
} else {
await createAdminDepartment(payload);
}
await loadDepartments();
resetForm();
} catch (e) {
msg.error((e as Error).message || t('admin.msg.failed'));
}
};
const deleteDepartment = (id: number): void => {
openDeleteConfirm(t('admin.dept.confirmDelete'), async () => {
try {
await deleteAdminDepartment(id);
await loadDepartments();
} catch (e) {
console.error(e);
msg.error((e as Error).message || t('admin.msg.requestFailed'));
}
});
};
return {
departments,
editingDept,
isLoading,
showDeleteConfirmDialog,
deleteConfirmMessage,
loadAll,
resetForm,
editDepartment,
saveDepartment,
deleteDepartment,
closeDeleteConfirm,
confirmDelete,
};
}