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,98 @@
|
||||
/**
|
||||
* features/admin/modules/approvals/composables/useApprovalsAdmin.ts
|
||||
* 用户注册审批:待审列表、通过、拒绝。
|
||||
*/
|
||||
import { computed, ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { approveAdminUser, listAdminUsers, rejectAdminUser } from '@/api/admin';
|
||||
import { useMessage } from '@/composables/useMessage';
|
||||
import { useAdminNavBadges } from '@/features/admin/composables/useAdminNavBadges';
|
||||
import type { AdminUserRow } from '@/features/admin/types/admin.types';
|
||||
|
||||
export function useApprovalsAdmin() {
|
||||
const { t } = useI18n();
|
||||
const msg = useMessage();
|
||||
const { refreshBadges } = useAdminNavBadges();
|
||||
|
||||
const users = ref<AdminUserRow[]>([]);
|
||||
const isLoading = ref(true);
|
||||
|
||||
const showConfirmDialog = ref(false);
|
||||
const confirmMessage = ref('');
|
||||
let confirmAction: (() => Promise<void> | void) | null = null;
|
||||
|
||||
const pendingUsers = computed(() => users.value.filter((u) => u.status === 'pending'));
|
||||
|
||||
const loadUsers = async (): Promise<void> => {
|
||||
try {
|
||||
users.value = await listAdminUsers();
|
||||
} catch (e) {
|
||||
console.error('Error loading users:', e);
|
||||
users.value = [];
|
||||
}
|
||||
};
|
||||
|
||||
const loadAll = async (): Promise<void> => {
|
||||
isLoading.value = true;
|
||||
await loadUsers();
|
||||
isLoading.value = false;
|
||||
};
|
||||
|
||||
const openConfirm = (message: string, action: () => Promise<void> | void): void => {
|
||||
confirmMessage.value = message;
|
||||
confirmAction = action;
|
||||
showConfirmDialog.value = true;
|
||||
};
|
||||
|
||||
const closeConfirm = (): void => {
|
||||
showConfirmDialog.value = false;
|
||||
confirmAction = null;
|
||||
};
|
||||
|
||||
const confirmActionHandler = async (): Promise<void> => {
|
||||
if (!confirmAction) return;
|
||||
try {
|
||||
await confirmAction();
|
||||
} finally {
|
||||
closeConfirm();
|
||||
}
|
||||
};
|
||||
|
||||
const approveUser = (userId: number): void => {
|
||||
openConfirm(t('admin.approval.confirmApprove'), async () => {
|
||||
try {
|
||||
await approveAdminUser(userId);
|
||||
await loadUsers();
|
||||
await refreshBadges();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.failed'));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const rejectUser = (userId: number): void => {
|
||||
openConfirm(t('admin.approval.confirmReject'), async () => {
|
||||
try {
|
||||
await rejectAdminUser(userId);
|
||||
await loadUsers();
|
||||
await refreshBadges();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.requestFailed'));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
pendingUsers,
|
||||
isLoading,
|
||||
showConfirmDialog,
|
||||
confirmMessage,
|
||||
loadAll,
|
||||
approveUser,
|
||||
rejectUser,
|
||||
closeConfirm,
|
||||
confirmActionHandler,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user