Files
ClassHelper/packages/web-admin/src/layouts/AdminLayout.vue
T

397 lines
12 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { computed, reactive, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus';
import { authApi } from '@/api';
import { useAuthStore } from '@/stores/auth';
import { useRealtimeStore } from '@/stores/realtime';
import { useResponsive } from '@/composables/useResponsive';
import { APP_TITLE } from '@/config';
const route = useRoute();
const router = useRouter();
const auth = useAuthStore();
const realtime = useRealtimeStore();
const { isMobile } = useResponsive();
const menuItems = [
{ path: '/dashboard', title: '仪表盘', icon: 'Odometer' },
{ path: '/classes', title: '班级管理', icon: 'School' },
{ path: '/students', title: '学生管理', icon: 'User' },
{ path: '/schedules', title: '课表管理', icon: 'Calendar' },
{ path: '/homeworks', title: '作业发布', icon: 'Notebook' },
{ path: '/notifications', title: '通知发布', icon: 'Bell' },
{ path: '/grades', title: '成绩录入', icon: 'Trophy' },
];
/** 高亮当前菜单:直接比较路由路径,避免依赖路由名 */
const activeMenu = computed(() => route.path);
/** 顶栏显示的当前页面标题(小屏下替代侧边栏的"我在哪"提示,1Panel 同款做法) */
const currentTitle = computed(() => menuItems.find((item) => item.path === route.path)?.title ?? APP_TITLE);
/** 小屏:侧边栏收进抽屉,由顶栏汉堡按钮唤出 */
const drawerVisible = ref(false);
watch(
() => route.path,
() => {
drawerVisible.value = false;
},
);
watch(isMobile, (mobile) => {
if (!mobile) drawerVisible.value = false;
});
/**
* 菜单点击回调。
* 注意:el-menu 的 @select 抛出的是 el-menu-item 的 index(这里即路由路径),
* 不能当作路由名使用(router.push({ name }) 会因找不到同名路由而静默失败)。
*/
function handleMenuSelect(index: string): void {
void router.push(index);
drawerVisible.value = false;
}
/** 开发期自检:菜单路径必须存在于路由表中,防止再次出现"点了没反应" */
if (import.meta.env.DEV) {
const knownPaths = new Set(router.getRoutes().map((item) => item.path));
for (const item of menuItems) {
if (!knownPaths.has(item.path)) {
console.error(`[AdminLayout] 菜单项未注册对应路由:${item.path}`);
}
}
}
const connectionText = computed(() => {
if (realtime.connected) return '实时通道已连接';
if (realtime.connecting) return '实时通道连接中';
return '实时通道已断开';
});
const connectionType = computed(() =>
realtime.connected ? 'success' : realtime.connecting ? 'warning' : 'danger',
);
async function handleLogout(): Promise<void> {
await ElMessageBox.confirm('确认退出当前账号?', '退出登录', { type: 'warning' });
await auth.logout();
ElMessage.success('已退出登录');
void router.push({ name: 'login' });
}
/* ------------------------------------------------------------ 修改密码 */
const passwordVisible = ref(false);
const passwordFormRef = ref<FormInstance>();
const passwordForm = reactive({ currentPassword: '', newPassword: '', confirmPassword: '' });
const passwordRules: FormRules = {
currentPassword: [{ required: true, message: '请输入当前密码', trigger: 'blur' }],
newPassword: [
{ required: true, message: '请输入新密码', trigger: 'blur' },
{ min: 6, message: '新密码至少 6 位', trigger: 'blur' },
],
confirmPassword: [
{
validator: (_rule, value: string, callback: (error?: Error) => void) => {
if (!value) callback(new Error('请再次输入新密码'));
else if (value !== passwordForm.newPassword) callback(new Error('两次输入的密码不一致'));
else callback();
},
trigger: 'blur',
},
],
};
async function submitPassword(): Promise<void> {
const valid = await passwordFormRef.value?.validate().catch(() => false);
if (!valid) return;
await authApi.changePassword({
currentPassword: passwordForm.currentPassword,
newPassword: passwordForm.newPassword,
});
passwordVisible.value = false;
passwordForm.currentPassword = '';
passwordForm.newPassword = '';
passwordForm.confirmPassword = '';
ElMessage.success('密码修改成功,请重新登录');
await auth.logout();
void router.push({ name: 'login' });
}
</script>
<template>
<el-container class="layout">
<!-- 桌面/平板:常驻深色侧边栏 -->
<el-aside v-if="!isMobile" width="210px" class="layout-aside ch-sidebar">
<div class="brand">
<el-icon :size="22"><School /></el-icon>
<span class="brand-text">班级小助手</span>
</div>
<el-menu :default-active="activeMenu" class="layout-menu" @select="handleMenuSelect">
<el-menu-item v-for="item in menuItems" :key="item.path" :index="item.path">
<el-icon><component :is="item.icon" /></el-icon>
<span>{{ item.title }}</span>
</el-menu-item>
</el-menu>
</el-aside>
<el-container class="layout-body">
<el-header class="layout-header">
<div class="header-left">
<!-- 小屏:汉堡按钮唤出抽屉菜单 -->
<button
v-if="isMobile"
type="button"
class="nav-toggle"
aria-label="打开菜单"
:aria-expanded="drawerVisible ? 'true' : 'false'"
@click="drawerVisible = true"
>
<el-icon :size="20"><Menu /></el-icon>
</button>
<span class="header-title">{{ isMobile ? currentTitle : APP_TITLE }}</span>
<el-tag v-if="!isMobile" :type="connectionType" size="small" effect="light">
<span class="header-conn">{{ connectionText }}</span>
</el-tag>
<el-tag
v-else
:type="connectionType"
size="small"
effect="light"
class="header-conn-dot"
:title="connectionText"
>
<span class="conn-dot" :class="`conn-${connectionType}`"></span>
</el-tag>
<el-tag v-if="realtime.eventCount > 0 && !isMobile" size="small" type="info" effect="plain">
已接收 {{ realtime.eventCount }} 条实时事件
</el-tag>
</div>
<div class="header-right">
<el-dropdown trigger="click">
<span class="user-chip">
<el-icon><UserFilled /></el-icon>
<template v-if="!isMobile">
{{ auth.displayName }}
<el-tag size="small" type="info" effect="plain">{{ auth.roleLabel }}</el-tag>
</template>
<el-icon><ArrowDown /></el-icon>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item disabled>{{ auth.displayName }} · {{ auth.roleLabel }}</el-dropdown-item>
<el-dropdown-item divided @click="passwordVisible = true">
<el-icon><Lock /></el-icon>
修改密码
</el-dropdown-item>
<el-dropdown-item divided @click="handleLogout">
<el-icon><SwitchButton /></el-icon>
退出登录
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</el-header>
<el-main class="layout-main">
<router-view v-slot="{ Component }">
<component :is="Component" />
</router-view>
</el-main>
</el-container>
<!-- 小屏抽屉菜单(1Panel 风格:深色侧栏内容与桌面端完全一致) -->
<el-drawer
v-model="drawerVisible"
direction="ltr"
size="240px"
:with-header="false"
class="ch-nav-drawer"
>
<div class="ch-sidebar ch-sidebar-drawer">
<div class="brand">
<el-icon :size="22"><School /></el-icon>
<span class="brand-text">班级小助手</span>
</div>
<el-menu :default-active="activeMenu" class="layout-menu" @select="handleMenuSelect">
<el-menu-item v-for="item in menuItems" :key="item.path" :index="item.path">
<el-icon><component :is="item.icon" /></el-icon>
<span>{{ item.title }}</span>
</el-menu-item>
</el-menu>
</div>
</el-drawer>
<el-dialog v-model="passwordVisible" title="修改密码" width="420px">
<el-form ref="passwordFormRef" :model="passwordForm" :rules="passwordRules" label-width="90px">
<el-form-item label="当前密码" prop="currentPassword">
<el-input v-model="passwordForm.currentPassword" type="password" show-password />
</el-form-item>
<el-form-item label="新密码" prop="newPassword">
<el-input v-model="passwordForm.newPassword" type="password" show-password />
</el-form-item>
<el-form-item label="确认密码" prop="confirmPassword">
<el-input v-model="passwordForm.confirmPassword" type="password" show-password />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="passwordVisible = false">取消</el-button>
<el-button type="primary" @click="submitPassword">确认修改</el-button>
</template>
</el-dialog>
</el-container>
</template>
<style scoped>
.layout {
height: 100vh;
height: 100dvh;
}
.layout-body {
min-width: 0;
}
.layout-aside {
display: flex;
flex-direction: column;
}
.brand {
display: flex;
align-items: center;
gap: 8px;
height: 56px;
padding: 0 16px;
color: #fff;
font-weight: 600;
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.brand-text {
font-size: 16px;
}
.layout-menu {
border-right: none;
background: transparent;
flex: 1;
--el-menu-text-color: #cbd5e1;
--el-menu-hover-bg-color: rgba(255, 255, 255, 0.08);
--el-menu-active-color: #ffffff;
}
.layout-menu :deep(.el-menu-item.is-active) {
background: #409eff;
border-radius: 6px;
margin: 0 8px;
}
.layout-menu :deep(.el-menu-item) {
margin: 2px 8px;
border-radius: 6px;
}
.layout-header {
background: #fff;
border-bottom: 1px solid var(--ch-border);
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 0 16px;
}
.nav-toggle {
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
margin-right: 2px;
border: none;
border-radius: 8px;
background: #f2f3f5;
color: #303133;
cursor: pointer;
transition: background 0.18s ease;
}
.nav-toggle:hover {
background: #e6e8eb;
}
.header-left {
display: flex;
align-items: center;
gap: 10px;
min-width: 0;
}
.header-title {
font-weight: 600;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.header-conn {
margin-left: 4px;
}
.header-right {
display: flex;
align-items: center;
flex: 0 0 auto;
}
.user-chip {
display: flex;
align-items: center;
gap: 6px;
cursor: pointer;
outline: none;
}
.layout-main {
background: var(--ch-bg);
padding: 0;
overflow-y: auto;
}
/* 小屏:顶栏更紧凑,正文留出安全区 */
@media (max-width: 768px) {
.layout-header {
height: var(--ch-header-height);
padding: 0 10px;
gap: 8px;
}
.header-title {
font-size: 15px;
}
}
</style>
<style>
/* 侧边栏配色放在全局:桌面 asider 与移动端抽屉复用同一套样式(抽屉内容在 teleport 中,scoped 样式不生效) */
.ch-sidebar {
background: #1f2d3d;
color: #fff;
}
.ch-sidebar-drawer {
height: 100%;
display: flex;
flex-direction: column;
}
/* 小屏抽屉:去掉 Element Plus 默认内边距,让深色侧栏铺满 */
.ch-nav-drawer .el-drawer__body {
padding: 0;
overflow: hidden;
}
</style>