Files
lpt-fe/src/components/MyHead.vue
T

250 lines
6.5 KiB
Vue

<template>
<div class="head surface-card" :class="{ 'compact-head': compactHead }">
<el-button
v-if="showBackButton"
text
class="back-btn"
:aria-label="backLabel"
@click="goBack"
>
<el-icon class="btn-icon"><ArrowLeft /></el-icon>
<span class="btn-label">← {{ backLabel }}</span>
</el-button>
<template v-if="pageTitle">
<h1 class="head-title">{{ pageTitle }}</h1>
</template>
<div v-else class="brand-block">
<p class="brand-title">学习进度跟踪系统</p>
<p class="brand-subtitle">把学习变成可记录、可回顾、可持续的日常流程</p>
</div>
<el-tooltip :content="themeActionLabel" placement="bottom">
<el-button text class="theme-btn" :aria-label="themeActionLabel" @click="toggleTheme">
<el-icon class="btn-icon"><Sunny v-if="isDark" /><Moon v-else /></el-icon>
<span class="btn-label">{{ themeActionLabel }}</span>
</el-button>
</el-tooltip>
<el-button
v-if="showAiUsageEntry"
plain
class="ai-usage-btn"
aria-label="AI 用量统计"
@click="openAiUsage"
>
<el-icon class="btn-icon"><DataLine /></el-icon>
<span class="btn-label">AI 用量统计</span>
</el-button>
<el-button
v-if="!isLoginRoute"
type="success"
plain
class="logout-btn"
:loading="loggingOut"
aria-label="退出登录"
@click="handleLogout"
>
<el-icon class="btn-icon"><SwitchButton /></el-icon>
<span class="btn-label">退出登录</span>
</el-button>
</div>
</template>
<script setup lang="ts">
import { computed, ref, watch } from "vue";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import { ArrowLeft, DataLine, Moon, Sunny, SwitchButton } from "@element-plus/icons-vue";
import router from "@/router";
import { clearRecallIdentity } from "@/utils/recallCache";
import { logout } from "@/api/login";
import { useTheme } from "@/components/composables/useTheme";
const route = useRoute();
const isLoginRoute = computed(() => route.path === "/login");
// 整页画布页(自由回忆)里页头浮在导图上,窄屏只留图标,把纵向空间让给内容
const compactHead = computed(() => route.meta.recallWorkspace === true);
const showBackButton = computed(() => route.path !== "/login" && route.path !== "/welcome");
const pageTitle = computed(() => route.meta?.title as string | undefined);
// 返回目标由路由 meta 声明,未声明时回首页
const backTo = computed(() => (route.meta?.backTo as string | undefined) || "/welcome");
const backLabel = computed(() => (route.meta?.backLabel as string | undefined) || "返回首页");
// 有多个入口的页面(如复习详情)优先回到用户来路,无历史时回退到声明的上级
const backUseHistory = computed(() => route.meta?.backUseHistory === true);
const loggingOut = ref(false);
const { isDark, themeActionLabel, toggleTheme } = useTheme();
// 白名单来自登录响应写入的 localStorage;头部组件只挂载一次,
// 因此换账号登录后要按路由变化重新读取,避免残留上一个账号的入口。
const aiAdmin = ref(localStorage.getItem("aiAdmin") === "true");
const showAiUsageEntry = computed(() => !isLoginRoute.value && aiAdmin.value);
watch(
() => route.path,
() => {
aiAdmin.value = localStorage.getItem("aiAdmin") === "true";
},
);
const openAiUsage = () => {
router.push("/ai-usage");
};
const goBack = () => {
if (backUseHistory.value && window.history.state?.back) {
router.back();
return;
}
router.push(backTo.value);
};
const handleLogout = async () => {
if (loggingOut.value) return;
loggingOut.value = true;
try {
try {
await logout();
} catch {
// 后端退出失败时,仍然清理前端登录态,避免用户被卡住。
}
clearRecallIdentity();
localStorage.removeItem("isLoggedIn");
localStorage.removeItem("aiAdmin");
ElMessage.success("已退出登录");
await router.push("/login");
} finally {
loggingOut.value = false;
}
};
defineExpose({ isLoginRoute, showBackButton, pageTitle, backTo, backLabel, loggingOut, goBack, handleLogout, showAiUsageEntry, openAiUsage });
</script>
<style scoped>
.head {
min-height: 82px;
padding: 16px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
/* 与 .head-title 对齐:撑满剩余空间,把右侧按钮组推到行尾。
首页没有页面标题,只有品牌区,缺了 flex:1 时 space-between 会把
「AI 用量统计」挤到页头中间,和「退出登录」分家。 */
.brand-block {
flex: 1;
min-width: 0;
}
.brand-title {
margin: 0;
font-weight: 700;
font-size: clamp(18px, 2.4vw, 24px);
letter-spacing: 0.3px;
color: var(--green-900);
}
.brand-subtitle {
margin: 4px 0 0;
font-size: 13px;
color: var(--text-secondary);
}
.head-title {
margin: 0;
font-size: clamp(18px, 2.4vw, 24px);
font-weight: 700;
color: var(--green-900);
flex: 1;
}
.back-btn {
font-size: 14px;
color: var(--green-700);
padding: 0 4px;
flex-shrink: 0;
}
.back-btn:hover {
color: var(--green-900);
}
.logout-btn {
border-color: var(--green-600);
color: var(--green-700);
}
/* 次级入口:中性描边,不与主按钮「退出登录」抢视觉 */
.theme-btn {
color: var(--green-700);
}
.theme-btn:hover {
color: var(--green-800);
}
.ai-usage-btn {
border-color: var(--border-soft);
color: var(--green-700);
}
.ai-usage-btn:hover {
border-color: var(--green-600);
color: var(--green-800);
}
/* 深色切换在桌面也保留图标(其他按钮只在画布页窄屏显示图标) */
.theme-btn .btn-icon {
display: inline-flex;
}
/* 图标只在画布页窄屏出现,其余形态保持文字按钮 */
.btn-icon {
display: none;
}
@media (max-width: 768px) {
.head {
align-items: flex-start;
flex-direction: column;
}
.logout-btn,
.theme-btn,
.ai-usage-btn {
width: 100%;
}
/* 画布页保持一行:文字换成图标,不再把整块页头竖向堆三行 */
.compact-head .btn-icon {
display: inline-flex;
}
.compact-head .btn-label {
display: none;
}
.head.compact-head {
flex-direction: row;
align-items: center;
flex-wrap: nowrap;
gap: 8px;
padding: 8px;
min-height: 0;
}
.compact-head .head-title {
font-size: 16px;
white-space: nowrap;
}
.compact-head .back-btn,
.compact-head .logout-btn,
.compact-head .theme-btn,
.compact-head .ai-usage-btn {
width: auto;
flex-shrink: 0;
padding: 8px;
}
}
</style>