299 lines
7.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
/** 业务页公用布局:左侧菜单 + 顶栏(右上角主体切换) + 内容区 */
import { computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { clearToken, currentUser, displayName } from '@/composables/useAuth';
import {
currentSubjectId,
hasSubject,
loadSubjects,
setSubjectId,
subjects,
subjectsLoaded,
} from '@/composables/useSubject';
/**
* fill = true:列表页用 —— 内容区不滚动,卡片吃掉剩余高度,只有表格内部滚动(表头吸顶、分页贴底)
* 默认 false:表单/详情页 —— 滚动落在内容区
*/
withDefaults(defineProps<{ fill?: boolean }>(), { fill: false });
const router = useRouter();
/** 菜单项:`subject: true` 表示必须已经选中主体才显示 */
const allMenus = [
{ to: '/', label: '首页', subject: false },
{ to: '/devices', label: '设备管理', subject: true },
{ to: '/nvrs', label: 'NVR 录像机', subject: true },
{ to: '/cameras', label: '摄像头', subject: true },
{ to: '/workstations', label: '工作台', subject: true },
{ to: '/records', label: '扫码记录', subject: true },
{ to: '/subjects', label: '主体 / 成员', subject: false },
{ to: '/profile', label: '个人中心', subject: false },
{ to: '/test', label: '测试页', subject: true },
];
/** 没有主体时只留首页 / 主体 / 个人中心(要求:先建主体才出现业务菜单) */
const menus = computed(() => allMenus.filter(item => (item.subject ? hasSubject.value : true)));
/** 当前主体(用于右上角展示 / 过期提示) */
const current = computed(() => subjects.value.find(item => item.id === currentSubjectId.value) || null);
/** 右上角头像里的首字 */
const avatarText = computed(() => {
const name = (displayName(currentUser.value) || '?').trim();
return name.slice(0, 1).toUpperCase();
});
/** 剩余天数(未过期时才显示;今天到期算 0 天) */
const daysLeft = computed(() => {
if (!current.value || current.value.expired) return null;
const diff = new Date(current.value.expireAt).getTime() - Date.now();
return Math.max(Math.ceil(diff / 86400000), 0);
});
function onSwitch(event: Event) {
const id = Number((event.target as HTMLSelectElement).value);
if (!id || id === currentSubjectId.value) return;
setSubjectId(id);
// 各页面的数据都是挂载时拉取的,切主体后整页刷新最省事
window.location.reload();
}
function logout() {
// 令牌在本地,退出即清掉并回登录页(服务端无需回调)
clearToken();
router.replace({ name: 'login' });
}
onMounted(() => {
// 直接打开内页(刷新)时也能把主体下拉填满
if (!subjectsLoaded.value) loadSubjects().catch(() => undefined);
});
</script>
<template>
<div class="layout">
<aside class="sidebar">
<div class="brand">云仓视频溯源系统</div>
<nav>
<RouterLink v-for="item in menus" :key="item.to" :to="item.to">{{ item.label }}</RouterLink>
</nav>
</aside>
<div class="main">
<header class="topbar">
<slot name="title">控制台</slot>
<div class="topbar-right">
<!-- 主体切换(右上角) -->
<div v-if="subjects.length" class="subject-picker">
<span class="subject-label">主体</span>
<select :value="currentSubjectId ?? ''" @change="onSwitch">
<option v-for="item in subjects" :key="item.id" :value="item.id">
{{ item.name }}
</option>
</select>
<span v-if="current?.expired" class="subject-tag is-danger">已到期</span>
<span v-else-if="daysLeft !== null" class="subject-tag" :class="{ 'is-warn': daysLeft <= 3 }">
剩 {{ daysLeft }} 天
</span>
<RouterLink class="subject-manage" to="/subjects">管理</RouterLink>
</div>
<RouterLink v-else class="subject-empty" to="/subjects">还没有主体,去创建 →</RouterLink>
<span class="topbar-divider" aria-hidden="true" />
<!-- 账号(右上角,与主体切换同一组) -->
<div class="user-box">
<span class="user-avatar">{{ avatarText }}</span>
<span class="user-name" :title="displayName(currentUser)">{{ displayName(currentUser) }}</span>
<a class="user-logout" href="#" @click.prevent="logout">退出登录</a>
</div>
</div>
</header>
<section :class="['content', { 'content-fill': fill }]">
<slot />
</section>
</div>
</div>
</template>
<style scoped>
/* 标题与右上角一组:标题可以被压缩,右侧不折行 */
.topbar {
gap: 12px;
}
.topbar-right {
display: flex;
align-items: center;
gap: 10px;
flex: 0 0 auto;
}
/* 右上角的分隔线 */
.topbar-divider {
width: 1px;
height: 18px;
background: var(--border);
margin: 0 2px;
}
/* 账号区:头像 + 昵称 + 退出登录 */
.user-box {
display: flex;
align-items: center;
gap: 8px;
}
.user-avatar {
width: 26px;
height: 26px;
flex: 0 0 26px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: 600;
color: #334155;
background: #e2e8f0;
}
.user-name {
max-width: 120px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
font-size: 13px;
color: #334155;
}
.user-logout {
font-size: 12px;
color: #64748b;
padding: 4px 8px;
border-radius: 6px;
white-space: nowrap;
}
.user-logout:hover {
color: #b91c1c;
background: #fef2f2;
}
/* 主体切换:一行「标签 + 下拉 + 期限标 + 管理」 */
.subject-picker {
display: flex;
align-items: center;
gap: 8px;
height: 32px;
padding: 0 8px 0 12px;
border: 1px solid var(--border);
border-radius: 999px;
background: #f8fafc;
}
.subject-label {
font-size: 12px;
color: #94a3b8;
white-space: nowrap;
}
.subject-picker select {
max-width: 220px;
min-width: 0;
flex: 0 1 auto;
height: 24px;
padding: 0 4px;
border: none;
background: transparent;
font-size: 13px;
color: #0f172a;
cursor: pointer;
outline: none;
}
.subject-picker select:focus-visible {
outline: 2px solid rgba(59, 130, 246, 0.35);
outline-offset: 1px;
border-radius: 6px;
}
/* 期限标(默认灰,快到期黄,已到期红) */
.subject-tag {
font-size: 12px;
line-height: 18px;
padding: 0 8px;
border-radius: 999px;
color: #475569;
background: #e2e8f0;
white-space: nowrap;
}
.subject-tag.is-warn {
color: #92400e;
background: #fef3c7;
}
.subject-tag.is-danger {
color: #b91c1c;
background: #fee2e2;
}
.subject-manage {
font-size: 12px;
color: #2563eb;
padding: 0 6px;
border-left: 1px solid var(--border);
white-space: nowrap;
}
.subject-manage:hover {
text-decoration: underline;
}
/* 没有主体时的入口 */
.subject-empty {
font-size: 13px;
color: #b45309;
padding: 5px 12px;
border: 1px dashed #f59e0b;
border-radius: 999px;
background: #fffbeb;
white-space: nowrap;
}
.subject-empty:hover {
background: #fef3c7;
}
/* 窗口变窄时依次收起次要信息:昵称 → 「主体」标签 → 期限标,保证不换行不溢出 */
@media (max-width: 1180px) {
.user-name {
display: none;
}
}
@media (max-width: 1000px) {
.subject-label {
display: none;
}
.subject-picker {
padding-left: 10px;
}
}
@media (max-width: 860px) {
.subject-tag {
display: none;
}
.subject-picker select {
max-width: 140px;
}
}
</style>