feat: 更新超级管理员登录逻辑,添加路由守卫和用户统计功能

This commit is contained in:
2025-12-23 16:28:21 +08:00
parent 3db41f4b0c
commit d09a9374ee
9 changed files with 508 additions and 180 deletions

View File

@@ -1,22 +1,106 @@
<script setup>
import BestSellingWidget from '@/components/dashboard/BestSellingWidget.vue';
import NotificationsWidget from '@/components/dashboard/NotificationsWidget.vue';
import RecentSalesWidget from '@/components/dashboard/RecentSalesWidget.vue';
import RevenueStreamWidget from '@/components/dashboard/RevenueStreamWidget.vue';
import StatsWidget from '@/components/dashboard/StatsWidget.vue';
import StatisticsStrip from '@/components/StatisticsStrip.vue';
import { TenantService } from '@/service/TenantService';
import { UserService } from '@/service/UserService';
import { useToast } from 'primevue/usetoast';
import { computed, onMounted, ref } from 'vue';
const toast = useToast();
const tenantTotal = ref(null);
const tenantLoading = ref(false);
const statistics = ref([]);
const statisticsLoading = ref(false);
const statisticsItems = computed(() => {
const total = (statistics.value || []).reduce((sum, row) => sum + (Number(row?.count) || 0), 0);
const statusIcon = (status) => {
switch (status) {
case 'verified':
return 'pi-check-circle';
case 'pending_verify':
return 'pi-clock';
case 'banned':
return 'pi-ban';
default:
return 'pi-tag';
}
};
const valueClass = (status) => {
switch (status) {
case 'banned':
return 'text-red-500';
case 'pending_verify':
return 'text-orange-500';
default:
return '';
}
};
return [
{ key: 'users-total', label: '用户总数:', value: statisticsLoading.value ? '-' : total, icon: 'pi-users' },
...(statistics.value || []).map((row) => ({
key: row?.status ?? row?.status_description,
label: `${row?.status_description || row?.status || '-'}`,
value: statisticsLoading.value ? '-' : (row?.count ?? 0),
icon: statusIcon(row?.status),
valueClass: valueClass(row?.status)
}))
];
});
const tenantItems = computed(() => [
{
key: 'tenants-total',
label: '租户总数:',
value: tenantLoading.value ? '-' : (tenantTotal.value ?? '-'),
icon: 'pi-building'
}
]);
async function loadTenantTotal() {
tenantLoading.value = true;
try {
const result = await TenantService.listTenants({ page: 1, limit: 10 });
tenantTotal.value = result?.total ?? 0;
} catch (error) {
toast.add({ severity: 'error', summary: '加载失败', detail: error?.message || '无法加载租户信息', life: 4000 });
} finally {
tenantLoading.value = false;
}
}
async function loadStatistics() {
statisticsLoading.value = true;
try {
statistics.value = await UserService.getUserStatistics();
} catch (error) {
toast.add({ severity: 'error', summary: '加载失败', detail: error?.message || '无法加载用户统计信息', life: 4000 });
} finally {
statisticsLoading.value = false;
}
}
onMounted(() => {
loadTenantTotal();
loadStatistics();
});
</script>
<template>
<div class="grid grid-cols-12 gap-8">
<StatsWidget />
<div>
<StatisticsStrip :items="tenantItems" containerClass="card mb-4" />
<StatisticsStrip :items="statisticsItems" containerClass="card mb-4" />
<div class="col-span-12 xl:col-span-6">
<RecentSalesWidget />
<BestSellingWidget />
</div>
<div class="col-span-12 xl:col-span-6">
<RevenueStreamWidget />
<NotificationsWidget />
<div class="card">
<div class="flex items-center justify-between mb-4">
<h4 class="m-0">快捷入口</h4>
</div>
<div class="flex flex-wrap gap-2">
<Button label="租户管理" icon="pi pi-building" as="router-link" to="/superadmin/tenants" />
<Button label="用户管理" icon="pi pi-users" as="router-link" to="/superadmin/users" severity="secondary" />
</div>
</div>
</div>
</template>

View File

@@ -1,10 +1,49 @@
<script setup>
import FloatingConfigurator from '@/components/FloatingConfigurator.vue';
import { ref } from 'vue';
import { superLogin, hasSuperAuthToken } from '@/service/auth';
import { useToast } from 'primevue/usetoast';
import { computed, onMounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
const email = ref('');
const toast = useToast();
const router = useRouter();
const route = useRoute();
const username = ref('');
const password = ref('');
const checked = ref(false);
const loading = ref(false);
const redirectPath = computed(() => {
const raw = route.query?.redirect;
return typeof raw === 'string' && raw.startsWith('/') ? raw : '/';
});
async function onSubmit() {
if (!username.value || !password.value) {
toast.add({ severity: 'warn', summary: '请输入账号密码', life: 2500 });
return;
}
loading.value = true;
try {
await superLogin({ username: username.value, password: password.value });
await router.replace(redirectPath.value);
} catch (error) {
toast.add({
severity: 'error',
summary: '登录失败',
detail: error?.message || '无法登录',
life: 4000
});
} finally {
loading.value = false;
}
}
onMounted(() => {
if (hasSuperAuthToken()) router.replace(redirectPath.value);
});
</script>
<template>
@@ -35,21 +74,21 @@ const checked = ref(false);
<span class="text-muted-color font-medium">Sign in to continue</span>
</div>
<div>
<label for="email1" class="block text-surface-900 dark:text-surface-0 text-xl font-medium mb-2">Email</label>
<InputText id="email1" type="text" placeholder="Email address" class="w-full md:w-[30rem] mb-8" v-model="email" />
<div @keyup.enter="onSubmit">
<label for="username" class="block text-surface-900 dark:text-surface-0 text-xl font-medium mb-2">Username</label>
<InputText id="username" type="text" placeholder="Username" class="w-full md:w-[30rem] mb-8" v-model="username" :disabled="loading" />
<label for="password1" class="block text-surface-900 dark:text-surface-0 font-medium text-xl mb-2">Password</label>
<Password id="password1" v-model="password" placeholder="Password" :toggleMask="true" class="mb-4" fluid :feedback="false"></Password>
<Password id="password1" v-model="password" placeholder="Password" :toggleMask="true" class="mb-4" fluid :feedback="false" :disabled="loading"></Password>
<div class="flex items-center justify-between mt-2 mb-8 gap-8">
<div class="flex items-center">
<Checkbox v-model="checked" id="rememberme1" binary class="mr-2"></Checkbox>
<Checkbox v-model="checked" id="rememberme1" binary class="mr-2" :disabled="loading"></Checkbox>
<label for="rememberme1">Remember me</label>
</div>
<span class="font-medium no-underline ml-2 text-right cursor-pointer text-primary">Forgot password?</span>
<span class="font-medium no-underline ml-2 text-right text-muted-color">Super Admin</span>
</div>
<Button label="Sign In" class="w-full" as="router-link" to="/"></Button>
<Button label="Sign In" class="w-full" :loading="loading" :disabled="loading" @click="onSubmit"></Button>
</div>
</div>
</div>