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>
This commit is contained in:
liufumingren 2026-10-21 17:00:09 +08:00
parent dc159c6f08
commit e6198a4334
6 changed files with 147 additions and 58 deletions

View File

@ -1059,6 +1059,11 @@ curl -X POST http://127.0.0.1:7300/api/internal/subject-orders/<orderNo>/pay \
- 不是该主体成员 / 主体不存在 → **404**
- 主体被停用 → **403** `subject_disabled`;主体到期 → **403** `subject_expired`(带 `expireAt`)
- 通过后写 `ctx.state.subjectId / subject / subjectMember`,service 一律用 `subjectId` 限定范围
- **业务数据只给主体管理员**:设备 / 录像机 / 摄像头 / 工作台 / 扫码记录这些接口在 `subjectAuth` 之后
再挂 `middleware.subjectAdmin()`,普通成员一律 **403** `{ status: 'subject_forbidden' }`
(判定与 `subjectService.ensureAdmin` 一致:创建者或 `role=1`)。
前端同步:左侧菜单只对管理员显示这几项,路由 `meta.adminOnly` 会把成员挡回首页;
`/api/web/stats` 与 `/api/web/pack-query` **不拦**(首页概览 / 桌面端打包查询普通成员也要用)。
- 登录(`POST /api/web/auth/login`)的返回里**带 `subjects` 列表**,前端存下并记住上次使用的主体(`localStorage.packs_subject_id`)。
- 设备(中控)没有「当前主体」概念:`GET /api/device/login?subjectId=` 可选,不传就用确认人可进入的第一个主体。

View File

@ -0,0 +1,40 @@
'use strict';
/**
* 主体管理员校验(**必须挂在 subjectAuth 之后**)
*
* 用法:`router.get('/api/web/nvrs', apiAuth, subjectAuth, subjectAdmin, controller...)`
*
* 为什么单独一个中间件:
* - `subjectAuth` 只保证「你是这个主体的成员」,业务数据(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录)
* 只有**主体管理员**能看,普通成员连列表都不该拿到;
* - 判断口径与 `subjectService.ensureAdmin` 一致:`subjects.admin_user_id`(创建者)或 `role = 1` 的成员。
*
* 失败:403 `{ ok:false, status:'subject_forbidden', message }`
*/
module.exports = () => {
return async function subjectAdmin(ctx, next) {
const member = ctx.state.subjectMember;
const subject = ctx.state.subject;
if (!subject || !member) {
// 正常不会走到:忘了挂 subjectAuth 时给个明确提示,而不是静默放行
ctx.status = 500;
ctx.body = { ok: false, message: '服务端路由缺少主体上下文(subjectAdmin 必须挂在 subjectAuth 之后)' };
return;
}
const isAdmin = Number(subject.adminUserId) === Number(member.userId) || Number(member.role) === 1;
if (!isAdmin) {
ctx.status = 403;
ctx.body = {
ok: false,
status: 'subject_forbidden',
message: '只有主体管理员可以查看该数据(请联系主体管理员)',
};
return;
}
await next();
};
};

View File

@ -8,6 +8,7 @@ module.exports = (app) => {
const { router, controller, middleware } = app;
const apiAuth = middleware.apiAuth();
const subjectAuth = middleware.subjectAuth();
const subjectAdmin = middleware.subjectAdmin();
const subjectActive = middleware.subjectActive();
const internalAuth = middleware.internalAuth();
const scannerAuth = middleware.scannerAuth();
@ -80,55 +81,58 @@ module.exports = (app) => {
router.get('/api/web/stats', apiAuth, subjectAuth, controller.web.stats.index);
// 本主体设备:列表 / 详情 / 通道 / 解绑 / 删除(越权访问一律 404)
// ⚠️ 业务数据(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录)**只有主体管理员能看**,
// 普通成员访问一律 403(subjectAdmin),前端菜单也同步隐藏
// ⚠️ /devices/active 与 /devices/activate 必须在 /devices/:id 之前
router.get('/api/web/devices', apiAuth, subjectAuth, controller.web.device.index);
router.get('/api/web/devices/active', apiAuth, subjectAuth, controller.web.device.active);
router.post('/api/web/devices/activate', apiAuth, subjectAuth, controller.web.device.activate);
router.get('/api/web/devices/:id', apiAuth, subjectAuth, controller.web.device.show);
router.get('/api/web/devices/:id/channels', apiAuth, subjectAuth, controller.web.device.channels);
router.post('/api/web/devices/:id/unbind', apiAuth, subjectAuth, controller.web.device.unbind);
router.post('/api/web/devices/:id/logout', apiAuth, subjectAuth, controller.web.device.logout);
router.delete('/api/web/devices/:id', apiAuth, subjectAuth, controller.web.device.destroy);
router.get('/api/web/devices', apiAuth, subjectAuth, subjectAdmin, controller.web.device.index);
router.get('/api/web/devices/active', apiAuth, subjectAuth, subjectAdmin, controller.web.device.active);
router.post('/api/web/devices/activate', apiAuth, subjectAuth, subjectAdmin, controller.web.device.activate);
router.get('/api/web/devices/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.device.show);
router.get('/api/web/devices/:id/channels', apiAuth, subjectAuth, subjectAdmin, controller.web.device.channels);
router.post('/api/web/devices/:id/unbind', apiAuth, subjectAuth, subjectAdmin, controller.web.device.unbind);
router.post('/api/web/devices/:id/logout', apiAuth, subjectAuth, subjectAdmin, controller.web.device.logout);
router.delete('/api/web/devices/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.device.destroy);
// 本主体扫码记录(范围限定在当前主体的设备)
router.get('/api/web/records', apiAuth, subjectAuth, controller.web.record.index);
// 本主体扫码记录(范围限定在当前主体的设备;仅主体管理员)
router.get('/api/web/records', apiAuth, subjectAuth, subjectAdmin, controller.web.record.index);
// 运单回放查询:运单号 → 当时的工位快照(录像机/摄像头/布局)+ 要拉的回放区间
// 桌面端拿到后按 streams 去各 NVR 拉那段录像(凭据走 /api/web/nvrs/:id/credential)
// ⚠️ 这个不挂 subjectAdmin:它是打包工位的日常查询入口,普通成员也要能用
router.get('/api/web/pack-query', apiAuth, subjectAuth, controller.web.pack.query);
// ---------- 视频接入:NVR / 摄像头 / 工作台(只碰本主体的数据)----------
// ---------- 视频接入:NVR / 摄像头 / 工作台(只碰本主体的数据;仅主体管理员)----------
// 拉取通道 = 前端调本机软件(localhost:17300/api/hik/channels)→ 勾选 → POST /nvrs/:id/channels 落库
router.get('/api/web/nvrs', apiAuth, subjectAuth, controller.web.nvr.index);
router.post('/api/web/nvrs', apiAuth, subjectAuth, controller.web.nvr.create);
router.get('/api/web/nvrs/:id', apiAuth, subjectAuth, controller.web.nvr.show);
router.put('/api/web/nvrs/:id', apiAuth, subjectAuth, controller.web.nvr.update);
router.delete('/api/web/nvrs/:id', apiAuth, subjectAuth, controller.web.nvr.destroy);
router.get('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, controller.web.nvr.cameras);
router.post('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, controller.web.nvr.syncChannels);
router.get('/api/web/nvrs/:id/credential', apiAuth, subjectAuth, controller.web.nvr.credential);
router.get('/api/web/nvrs', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.index);
router.post('/api/web/nvrs', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.create);
router.get('/api/web/nvrs/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.show);
router.put('/api/web/nvrs/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.update);
router.delete('/api/web/nvrs/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.destroy);
router.get('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.cameras);
router.post('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.syncChannels);
router.get('/api/web/nvrs/:id/credential', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.credential);
// 摄像头:/options、/batch-delete 必须在 /:id 之前注册,否则会被当成 id
router.get('/api/web/cameras/options', apiAuth, subjectAuth, controller.web.camera.options);
router.post('/api/web/cameras/batch-delete', apiAuth, subjectAuth, controller.web.camera.batchDestroy);
router.get('/api/web/cameras', apiAuth, subjectAuth, controller.web.camera.index);
router.get('/api/web/cameras/:id', apiAuth, subjectAuth, controller.web.camera.show);
router.put('/api/web/cameras/:id', apiAuth, subjectAuth, controller.web.camera.update);
router.delete('/api/web/cameras/:id', apiAuth, subjectAuth, controller.web.camera.destroy);
router.get('/api/web/cameras/options', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.options);
router.post('/api/web/cameras/batch-delete', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.batchDestroy);
router.get('/api/web/cameras', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.index);
router.get('/api/web/cameras/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.show);
router.put('/api/web/cameras/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.update);
router.delete('/api/web/cameras/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.destroy);
// 工作台:CRUD + 绑定摄像头 + 绑定扫码通道
router.get('/api/web/workstation-scan-channels', apiAuth, subjectAuth, controller.web.workstation.availableScanChannels);
router.get('/api/web/workstations', apiAuth, subjectAuth, controller.web.workstation.index);
router.post('/api/web/workstations', apiAuth, subjectAuth, controller.web.workstation.create);
router.get('/api/web/workstations/:id', apiAuth, subjectAuth, controller.web.workstation.show);
router.put('/api/web/workstations/:id', apiAuth, subjectAuth, controller.web.workstation.update);
router.delete('/api/web/workstations/:id', apiAuth, subjectAuth, controller.web.workstation.destroy);
router.post('/api/web/workstations/:id/cameras', apiAuth, subjectAuth, controller.web.workstation.bindCameras);
router.delete('/api/web/workstations/:id/cameras/:cameraId', apiAuth, subjectAuth, controller.web.workstation.unbindCamera);
router.get('/api/web/workstation-scan-channels', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.availableScanChannels);
router.get('/api/web/workstations', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.index);
router.post('/api/web/workstations', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.create);
router.get('/api/web/workstations/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.show);
router.put('/api/web/workstations/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.update);
router.delete('/api/web/workstations/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.destroy);
router.post('/api/web/workstations/:id/cameras', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.bindCameras);
router.delete('/api/web/workstations/:id/cameras/:cameraId', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.unbindCamera);
// 合成画面布局(拖拽/缩放后保存)
router.put('/api/web/workstations/:id/cameras/:cameraId/layout', apiAuth, subjectAuth, controller.web.workstation.updateCameraLayout);
router.post('/api/web/workstations/:id/scan-channels', apiAuth, subjectAuth, controller.web.workstation.bindScanChannels);
router.delete('/api/web/workstations/:id/scan-channels/:linkId', apiAuth, subjectAuth, controller.web.workstation.unbindScanChannel);
router.put('/api/web/workstations/:id/cameras/:cameraId/layout', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.updateCameraLayout);
router.post('/api/web/workstations/:id/scan-channels', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.bindScanChannels);
router.delete('/api/web/workstations/:id/scan-channels/:linkId', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.unbindScanChannel);
// 前端「测试页」的模拟接口(模拟设备扫码登录 / 模拟中控上报),与真机走同一套 service
// 只在非生产环境注册:生产不暴露模拟链路

View File

@ -6,6 +6,7 @@ import { clearToken, currentUser, displayName } from '@/composables/useAuth';
import {
currentSubjectId,
hasSubject,
isSubjectAdmin,
loadSubjects,
setSubjectId,
subjects,
@ -20,22 +21,30 @@ withDefaults(defineProps<{ fill?: boolean }>(), { fill: false });
const router = useRouter();
/** 菜单项:`subject: true` 表示必须已经选中主体才显示 */
/**
* 菜单项:
* - `subject: true` → 必须已经选中主体才显示
* - `admin: 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: '/', 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 }] : []),
...(import.meta.env.DEV ? [{ to: '/test', label: '测试页', subject: true, admin: true }] : []),
];
/** 没有主体时只留首页 / 主体 / 个人中心(要求:先建主体才出现业务菜单) */
const menus = computed(() => allMenus.filter(item => (item.subject ? hasSubject.value : 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);

View File

@ -1,11 +1,14 @@
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';
import { getToken } from '@/composables/useAuth';
import { ensureSubjects, hasSubject } from '@/composables/useSubject';
import { ensureSubjects, hasSubject, isSubjectAdmin } from '@/composables/useSubject';
/**
* 路由表(meta.public = 不需要登录)
*
* 手机扫码打开的 `/wechat?spm=…` 也必须是公开路由(扫码的人可能还没登录)。
*
* `meta.adminOnly = true`:业务数据页(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录),
* **只有主体管理员**能进;普通成员直接跳回首页并提示(后端同名接口也返回 403)。
*/
const routes: RouteRecordRaw[] = [
{ path: '/login', name: 'login', component: () => import('@/views/LoginView.vue'), meta: { public: true, bare: true } },
@ -27,16 +30,16 @@ const routes: RouteRecordRaw[] = [
{ path: '/', name: 'home', component: () => import('@/views/HomeView.vue'), meta: { allowNoSubject: true } },
// 主体 / 成员:没有主体时只有这个入口(创建主体、切换主体、绑成员)
{ path: '/subjects', name: 'subjects', component: () => import('@/views/SubjectsView.vue'), meta: { allowNoSubject: true } },
{ path: '/devices', name: 'devices', component: () => import('@/views/DevicesView.vue') },
{ path: '/devices', name: 'devices', component: () => import('@/views/DevicesView.vue'), meta: { adminOnly: true } },
// 视频接入:NVR → 摄像头 → 工作台
{ path: '/nvrs', name: 'nvrs', component: () => import('@/views/NvrsView.vue') },
{ path: '/cameras', name: 'cameras', component: () => import('@/views/CamerasView.vue') },
{ path: '/workstations', name: 'workstations', component: () => import('@/views/WorkstationsView.vue') },
{ path: '/records', name: 'records', component: () => import('@/views/RecordsView.vue') },
{ path: '/nvrs', name: 'nvrs', component: () => import('@/views/NvrsView.vue'), meta: { adminOnly: true } },
{ path: '/cameras', name: 'cameras', component: () => import('@/views/CamerasView.vue'), meta: { adminOnly: true } },
{ path: '/workstations', name: 'workstations', component: () => import('@/views/WorkstationsView.vue'), meta: { adminOnly: true } },
{ path: '/records', name: 'records', component: () => import('@/views/RecordsView.vue'), meta: { adminOnly: true } },
{ path: '/profile', name: 'profile', component: () => import('@/views/ProfileView.vue'), meta: { allowNoSubject: true } },
// 测试页只在开发构建里存在:生产包不包含该路由,也不会产出它的 chunk
...(import.meta.env.DEV
? [{ path: '/test', name: 'test', component: () => import('@/views/TestView.vue') } as RouteRecordRaw]
? [{ path: '/test', name: 'test', component: () => import('@/views/TestView.vue'), meta: { adminOnly: true } } as RouteRecordRaw]
: []),
{ path: '/:pathMatch(.*)*', redirect: '/' },
@ -83,6 +86,10 @@ router.beforeEach(async (to) => {
if (to.meta.allowNoSubject !== true && !hasSubject.value) {
return { name: 'subjects', query: { need: '1' } };
}
// 业务数据(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录)只有主体管理员能进
if (to.meta.adminOnly === true && !isSubjectAdmin.value) {
return { name: 'home', query: { denied: '1' } };
}
}
return true;
});

View File

@ -1,18 +1,26 @@
<script setup lang="ts">
/** 首页:只统计「当前主体」的数据(设备 / 通道 / 扫码记录 + 最近一次扫码) */
import { onMounted, ref } from 'vue';
import { RouterLink } from 'vue-router';
import { computed, onMounted, ref } from 'vue';
import { RouterLink, useRoute } from 'vue-router';
import AppLayout from '@/components/AppLayout.vue';
import { ApiError } from '@/api/client';
import { fetchStats } from '@/api/web';
import type { Stats } from '@/api/types';
import { hasSubject } from '@/composables/useSubject';
import { currentSubject, hasSubject, isSubjectAdmin } from '@/composables/useSubject';
import { formatTime, fromNow } from '@/utils/format';
const route = useRoute();
const stats = ref<Stats | null>(null);
const error = ref('');
const loading = ref(true);
const noSubject = ref(!hasSubject.value);
/** 业务数据(设备 / 通道 / 扫码记录)只有主体管理员能看,普通成员首页只显示说明 */
const isAdmin = isSubjectAdmin;
/** 被路由守卫挡回来的提示(普通成员点了业务菜单) */
const denied = ref(route.query.denied === '1');
/** 当前主体名(成员视角的提示文案用) */
const currentSubjectName = computed(() => currentSubject.value?.name || '当前主体');
/** 测试页只在开发构建里存在,所以生产环境的提示文案不再提它(编译期就折掉 dev 文案) */
const emptyHint = import.meta.env.DEV
? '还没有扫码记录。设备扫码登录后,用「测试页」可以模拟上报一条。'
@ -23,6 +31,11 @@ onMounted(async () => {
loading.value = false;
return;
}
// 普通成员没有业务数据权限:不去请求(否则 403)
if (!isAdmin.value) {
loading.value = false;
return;
}
try {
stats.value = await fetchStats();
} catch (err) {
@ -48,6 +61,17 @@ onMounted(async () => {
<RouterLink class="btn" to="/subjects">去创建主体</RouterLink>
</div>
<!-- 普通成员:业务数据(设备 / 通道 / 扫码记录)只有主体管理员能看 -->
<div v-else-if="!isAdmin" class="card">
<div v-if="denied" class="alert alert-error">该页面仅主体管理员可访问,已返回首页。</div>
<h3>你是「{{ currentSubjectName }}」的成员</h3>
<p class="hint">
设备管理、NVR 录像机、摄像头、工作台、扫码记录只有<b>主体管理员</b>可以查看。
需要这些权限的话,请联系主体管理员在「主体 / 成员 → 成员」里把你设为管理员。
</p>
<RouterLink class="btn" to="/subjects">查看主体 / 成员</RouterLink>
</div>
<p v-else-if="loading" class="hint">加载中…</p>
<template v-else-if="stats">