137 lines
4.7 KiB
JavaScript
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.

'use strict';
const { Controller } = require('egg');
/** 路径参数转正整数,非法返回 null */
function toId(value) {
const id = Number(value);
return Number.isInteger(id) && id > 0 ? id : null;
}
/**
* web 主体端:**当前主体的设备**(`/api/web/devices`)
*
* 鉴权:`middleware.apiAuth()` + `middleware.subjectAuth()`,主体 id 取自 header `X-Subject-Id`。
* 与 `controller/internal/device.js` 的区别:那边是运维视角(可看/可管**所有人**的设备),
* 这边只能看/动本主体的 —— 不属于本主体的一律 404(不暴露其他主体有哪些设备)。
*/
class WebDeviceController extends Controller {
/** 取「本主体的设备」;不是本主体的(或不存在)一律 404 */
async own(rawId) {
const { ctx } = this;
const id = toId(rawId);
if (!id) ctx.throw(400, 'id 必须是正整数');
const data = await ctx.service.scanner.findById(id);
if (!data || Number(data.subjectId) !== Number(ctx.state.subjectId)) {
ctx.throw(404, '设备不存在或不属于当前主体');
}
return data;
}
/** GET /api/web/devices?page=&pageSize=&keyword=&status= —— 本主体设备列表 */
async index() {
const { ctx } = this;
const { page, pageSize, keyword, status } = ctx.query;
const data = await ctx.service.scanner.list({
page,
pageSize,
keyword,
status,
// ⚠️ 强制按当前主体过滤,忽略前端传的 subjectId
subjectId: ctx.state.subjectId,
});
ctx.body = { ok: true, data };
}
/** GET /api/web/devices/:id —— 设备详情 */
async show() {
const { ctx } = this;
ctx.body = { ok: true, data: await this.own(ctx.params.id) };
}
/**
* GET /api/web/devices/active —— 本主体当前已绑定的设备
*
* 桌面端「开启设备服务」前用它检查:已有设备 -> 提示「开启后会踢掉它」。
*/
async active() {
const { ctx } = this;
const data = await ctx.service.scanner.list({
page: 1,
pageSize: 100,
subjectId: ctx.state.subjectId,
});
ctx.body = { ok: true, data };
}
/**
* POST /api/web/devices/activate —— 桌面端开启设备服务(**仅主体管理员**)
*
* body: `{ sn?, name?, vendor?, model? }`
* → `{ device, token, tokenType, expiresIn, kicked: [{ id, sn, name }] }`
*
* 规则:**一个主体只允许一台设备在线**,开启时同主体其他设备会被踢下线(旧令牌 401)。
*/
async activate() {
const { ctx } = this;
const { sn, name, vendor, model } = ctx.request.body || {};
// 只有主体管理员能开设备服务
await ctx.service.subject.ensureAdmin(ctx.state.subjectId, ctx.state.user.id);
const data = await ctx.service.scanner.activateForSubject({
subjectId: ctx.state.subjectId,
userId: ctx.state.user.id,
sn,
name,
vendor,
model,
});
ctx.logger.info('[web] 用户 %s 在主体 %s 开启设备服务 %s(踢下线 %d 台)',
ctx.state.user.id, ctx.state.subjectId, data.sn, data.kicked.length);
ctx.body = { ok: true, data };
}
/** POST /api/web/devices/:id/logout —— 关闭设备服务(让该设备令牌立即失效) */
async logout() {
const { ctx } = this;
const device = await this.own(ctx.params.id);
await ctx.service.scanner.revokeToken(device.id);
ctx.logger.info('[web] 用户 %s 在主体 %s 关闭设备服务 %s', ctx.state.user.id, ctx.state.subjectId, device.sn);
ctx.body = { ok: true, data: { id: device.id } };
}
/** GET /api/web/devices/:id/channels —— 该设备(中控)下已登记的通道 */
async channels() {
const { ctx } = this;
const device = await this.own(ctx.params.id);
const list = await ctx.service.scan.findChannelsByScanner(device.id);
ctx.body = { ok: true, data: { deviceId: device.id, list } };
}
/** POST /api/web/devices/:id/unbind —— 解绑(记录保留,设备可再扫码认领) */
async unbind() {
const { ctx } = this;
const device = await this.own(ctx.params.id);
const data = await ctx.service.scanner.unbind({ scannerId: device.id });
ctx.logger.info('[web] 用户 %s 在主体 %s 解绑了设备 %s', ctx.state.user.id, ctx.state.subjectId, device.sn);
ctx.body = { ok: true, data };
}
/** DELETE /api/web/devices/:id —— 删除(软删除)本主体的设备 */
async destroy() {
const { ctx } = this;
const device = await this.own(ctx.params.id);
await ctx.service.scanner.remove(device.id);
ctx.logger.info('[web] 用户 %s 在主体 %s 删除了设备 %s', ctx.state.user.id, ctx.state.subjectId, device.sn);
ctx.body = { ok: true, data: { id: device.id } };
}
}
module.exports = WebDeviceController;