refactor: 重构前端骨架,及admin模块子路由和懒加载
refactor: 优化多次请求,并使用pinia状态管理
This commit is contained in:
1 parent
f877f61efd
commit
61693d014d
74 files changed
+9493
-6418
No files matched your search
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* features/admin/modules/announcements/AnnouncementsPage.vue
|
||||
* 公告管理独立页面。路由:/admin/announcements
|
||||
*/
|
||||
<script setup lang="ts">
|
||||
import { onMounted } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { Trash2 } from 'lucide-vue-next';
|
||||
import DeleteConfirmDialog from '@/components/DeleteConfirmDialog.vue';
|
||||
import { useAnnouncementsAdmin } from './composables/useAnnouncementsAdmin';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const {
|
||||
announcements,
|
||||
newAnnouncement,
|
||||
isLoading,
|
||||
isSaving,
|
||||
showDeleteConfirmDialog,
|
||||
deleteConfirmMessage,
|
||||
loadAll,
|
||||
publishAnnouncement,
|
||||
deleteAnnouncementItem,
|
||||
closeDeleteConfirm,
|
||||
confirmDelete,
|
||||
} = useAnnouncementsAdmin();
|
||||
|
||||
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>
|
||||
<h2 class="section-title">{{ t('admin.announcement.title') }}</h2>
|
||||
<p class="section-desc mt-1">{{ t('admin.announcement.subtitle') }}</p>
|
||||
</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">{{ t('admin.announcement.add') }}</h3>
|
||||
<div class="space-y-4">
|
||||
<div class="space-y-1.5">
|
||||
<label class="label-field">{{ t('admin.announcement.fields.title') }}</label>
|
||||
<input v-model="newAnnouncement.title" class="input-field text-sm" />
|
||||
</div>
|
||||
<div class="space-y-1.5">
|
||||
<label class="label-field">{{ t('admin.announcement.fields.content') }}</label>
|
||||
<textarea v-model="newAnnouncement.content" rows="6" class="input-field text-sm" />
|
||||
</div>
|
||||
<div class="flex justify-end pt-2">
|
||||
<button
|
||||
type="button"
|
||||
class="btn-primary btn-sm disabled:opacity-50"
|
||||
:disabled="isSaving"
|
||||
@click="publishAnnouncement"
|
||||
>
|
||||
{{ isSaving ? t('admin.save') + '...' : t('admin.announcement.publish') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="lg:col-span-2 card shadow-sm overflow-hidden">
|
||||
<div v-if="announcements.length === 0" class="p-8 text-center text-muted">
|
||||
{{ t('admin.announcement.noData') }}
|
||||
</div>
|
||||
<div v-else class="divide-y divide-subtle">
|
||||
<div
|
||||
v-for="ann in announcements"
|
||||
:key="ann.id"
|
||||
class="p-5 hover-surface-soft transition-colors"
|
||||
>
|
||||
<div class="flex items-start justify-between gap-3">
|
||||
<div class="min-w-0">
|
||||
<h4 class="font-semibold text-primary truncate">{{ ann.title }}</h4>
|
||||
<p class="text-xs text-muted mt-1">{{ new Date(ann.created_at).toLocaleString() }}</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
class="p-1.5 text-red-600 hover:bg-red-50 rounded-lg transition-colors"
|
||||
@click="deleteAnnouncementItem(ann.id)"
|
||||
>
|
||||
<Trash2 class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
<p class="text-sm text-secondary mt-2 whitespace-pre-wrap">{{ ann.content }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<DeleteConfirmDialog
|
||||
v-model="showDeleteConfirmDialog"
|
||||
:message="deleteConfirmMessage"
|
||||
@cancel="closeDeleteConfirm"
|
||||
@confirm="confirmDelete"
|
||||
/>
|
||||
</section>
|
||||
</template>
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* features/admin/modules/announcements/composables/useAnnouncementsAdmin.ts
|
||||
* 公告管理:列表、发布、删除。
|
||||
*/
|
||||
import { ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { createAnnouncement, deleteAnnouncement, listAnnouncements } from '@/api/admin';
|
||||
import { useMessage } from '@/composables/useMessage';
|
||||
import type { AdminAnnouncementRow } from '@/features/admin/types/admin.types';
|
||||
import {
|
||||
createEmptyAnnouncementForm,
|
||||
type AnnouncementFormState,
|
||||
} from '@/features/admin/types/announcement.types';
|
||||
|
||||
export function useAnnouncementsAdmin() {
|
||||
const { t } = useI18n();
|
||||
const msg = useMessage();
|
||||
|
||||
const announcements = ref<AdminAnnouncementRow[]>([]);
|
||||
const newAnnouncement = ref<AnnouncementFormState>(createEmptyAnnouncementForm());
|
||||
const isLoading = ref(true);
|
||||
const isSaving = ref(false);
|
||||
|
||||
const showDeleteConfirmDialog = ref(false);
|
||||
const deleteConfirmMessage = ref('');
|
||||
let deleteConfirmAction: (() => Promise<void> | void) | null = null;
|
||||
|
||||
const loadAnnouncements = async (): Promise<void> => {
|
||||
try {
|
||||
announcements.value = await listAnnouncements();
|
||||
} catch (e) {
|
||||
console.error('Failed to load announcements', e);
|
||||
announcements.value = [];
|
||||
}
|
||||
};
|
||||
|
||||
const loadAll = async (): Promise<void> => {
|
||||
isLoading.value = true;
|
||||
await loadAnnouncements();
|
||||
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 publishAnnouncement = async (): Promise<void> => {
|
||||
if (!newAnnouncement.value.title.trim() || !newAnnouncement.value.content.trim()) {
|
||||
msg.warning(t('admin.announcement.msg.fillAll'));
|
||||
return;
|
||||
}
|
||||
isSaving.value = true;
|
||||
try {
|
||||
await createAnnouncement({
|
||||
title: newAnnouncement.value.title.trim(),
|
||||
content: newAnnouncement.value.content.trim(),
|
||||
});
|
||||
newAnnouncement.value = createEmptyAnnouncementForm();
|
||||
await loadAnnouncements();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.requestFailed'));
|
||||
} finally {
|
||||
isSaving.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const deleteAnnouncementItem = (id: number): void => {
|
||||
openDeleteConfirm(t('admin.announcement.confirmDelete'), async () => {
|
||||
try {
|
||||
await deleteAnnouncement(id);
|
||||
await loadAnnouncements();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.requestFailed'));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
announcements,
|
||||
newAnnouncement,
|
||||
isLoading,
|
||||
isSaving,
|
||||
showDeleteConfirmDialog,
|
||||
deleteConfirmMessage,
|
||||
loadAll,
|
||||
publishAnnouncement,
|
||||
deleteAnnouncementItem,
|
||||
closeDeleteConfirm,
|
||||
confirmDelete,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user