liufumingren e6198a4334 web 端权限收紧:业务数据接口与菜单仅主体管理员
后端
- 新增 middleware/subject_admin.js:在 subjectAuth 之后校验「创建者 或 role=1」,否则 403
  { status: subject_forbidden, message: 只有主体管理员可以查看该数据(请联系主体管理员) }
- 挂到全部业务数据接口:/api/web/devices*、/records、/nvrs*(含 credential)、/cameras*、
  /workstations*、/workstation-scan-channels
- 有意不拦:/api/web/stats(首页概览)、/api/web/pack-query(桌面端打包查询,工位日常要用)

前端
- 路由加 meta.adminOnly(设备/录像机/摄像头/工作台/扫码记录,dev 的测试页同样),
  守卫里非管理员直接回首页 ?denied=1
- AppLayout 菜单按 isSubjectAdmin 过滤,成员只看到 首页 / 主体·成员 / 个人中心
- 首页对成员改成说明卡片(不再请求 stats、不显示设备/通道/记录数),被挡回时给出提示

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 17:00:09 +08:00

309 lines
7.9 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,
isSubjectAdmin,
loadSubjects,
setSubjectId,
subjects,
subjectsLoaded,
} from '@/composables/useSubject';
/**
* fill = true:列表页用 —— 内容区不滚动,卡片吃掉剩余高度,只有表格内部滚动(表头吸顶、分页贴底)
* 默认 false:表单/详情页 —— 滚动落在内容区
*/
withDefaults(defineProps<{ fill?: boolean }>(), { fill: false });
const router = useRouter();
/**
* 菜单项:
* - `subject: true` → 必须已经选中主体才显示
* - `admin: true` → **只有主体管理员**能看(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录)
*/
const allMenus = [
{ to: '/', label: '首页', subject: false, admin: false },
{ to: '/devices', label: '设备管理', subject: true, admin: true },
{ to: '/nvrs', label: 'NVR 录像机', subject: true, admin: true },
{ to: '/cameras', label: '摄像头', subject: true, admin: true },
{ to: '/workstations', label: '工作台', subject: true, admin: true },
{ to: '/records', label: '扫码记录', subject: true, admin: true },
{ to: '/subjects', label: '主体 / 成员', subject: false, admin: false },
{ to: '/profile', label: '个人中心', subject: false, admin: false },
// 测试页只在开发构建里出现(生产构建不会产出该页面)
...(import.meta.env.DEV ? [{ to: '/test', label: '测试页', subject: true, admin: true }] : []),
];
/** 没有主体时只留首页 / 主体 / 个人中心;普通成员看不到业务数据菜单 */
const menus = computed(() => allMenus.filter(item => {
if (item.subject && !hasSubject.value) return false;
if (item.admin && !isSubjectAdmin.value) return false;
return 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>