- 工作台成员扫码登入:workstation_login_records 记录谁在什么时候登入/下线了哪台工作台, 超时(团购 1h / 散单 30min)由 schedule/workstation_idle_offline 自动下线; 运单查询的 packers 取当时的登入成员 - 面单摄像头识别:workstation_cameras.is_label_camera + service/label_scan 下发本机该跑的识别配置, 设备端(桌面端 LabelScan 工作线程)按配置做面单识别并上报 - 虚拟通道 CAM-<工作台id>:工作台没接扫码枪时,面单摄像头那一路顶替扫码枪上报,records 规则按 ws_type 区分 - 桌面端:新增 LabelScanMonitor/LabelScanWorker/ScanDeviceKind 与面单预览页,扫描通道支持改名,静默窗口 5s Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
247 lines
8.6 KiB
JavaScript
247 lines
8.6 KiB
JavaScript
'use strict';
|
||
|
||
const { app, mock, assert } = require('egg-mock/bootstrap');
|
||
|
||
/** 伪造的 sequelize 实例(toJSON + update 直接改同一份数据) */
|
||
const instance = (data) => Object.assign(
|
||
{
|
||
toJSON: () => data,
|
||
update: async (fields) => Object.assign(data, fields),
|
||
},
|
||
data,
|
||
);
|
||
|
||
/**
|
||
* 工作台成员登录(`workstation_login_records`)
|
||
*
|
||
* 登入方式已改为「微信登录网关扫码」:网关回调确认了「谁」之后,
|
||
* 由 `service/wxGateway` 调 `service/workstationLogin.loginByUser()` 落地(见 wx-gateway.test.js)。
|
||
*
|
||
* 这里只盯最容易错的三件事:
|
||
* 1. **名字**:属于工作台所属主体的成员 → 记成员姓名(= 工号);否则记用户昵称;
|
||
* 2. **在线唯一**:同一个人重新登录 → 旧记录按 `relogin` 下线,只留一条在线;
|
||
* 3. **下线**:手动下线 / 一键全部下线都要写 `status=0 + logout_at`,历史记录保留。
|
||
*/
|
||
describe('test/app/controller/workstation-login.test.js', () => {
|
||
const me = { id: 1, username: 'admin', nickname: '小王', status: 1 };
|
||
const SUBJECT_ID = 5;
|
||
|
||
const BEARER = 'Bearer';
|
||
const token = () => app.createAnonymousContext().service.jwt.signUser(me);
|
||
const withToken = (req) => req.set('Authorization', BEARER + ' ' + token());
|
||
const withSubject = (req) => withToken(req).set('X-Subject-Id', String(SUBJECT_ID));
|
||
|
||
const subjectInstance = () => instance({
|
||
id: SUBJECT_ID,
|
||
name: '测试主体',
|
||
adminUserId: 1,
|
||
status: 1,
|
||
expireAt: new Date(Date.now() + 86400000),
|
||
});
|
||
|
||
const memberInstance = (name = 'K0012') => instance({
|
||
id: 11,
|
||
subjectId: SUBJECT_ID,
|
||
userId: 1,
|
||
name,
|
||
role: 1,
|
||
status: 1,
|
||
});
|
||
|
||
/** 主体/用户/成员三件套(登录与主体上下文都要用) */
|
||
function baseModels(extra = {}) {
|
||
return Object.assign({
|
||
User: { async findByPk() { return instance(me); } },
|
||
Subject: { async findByPk() { return subjectInstance(); } },
|
||
SubjectMember: { async findOne() { return memberInstance(); } },
|
||
}, extra);
|
||
}
|
||
|
||
/** 直接调 service(扫码确认那一步在网关回调里,不经过 HTTP) */
|
||
const loginByUser = (params) => app.createAnonymousContext().service.workstationLogin.loginByUser(params);
|
||
|
||
it('service.workstationLogin.loginByUser:主体成员 → 记录成员姓名(工号)', async () => {
|
||
let created = null;
|
||
let lastActiveAt = null;
|
||
const workstation = instance({
|
||
id: 3,
|
||
subjectId: SUBJECT_ID,
|
||
code: 'WS-01',
|
||
name: '1号工作台',
|
||
wsType: 2,
|
||
status: 1,
|
||
update: async (fields) => { lastActiveAt = fields.lastActiveAt; },
|
||
});
|
||
|
||
mock(app, 'model', baseModels({
|
||
WorkstationLoginRecord: {
|
||
async update(fields, options) {
|
||
assert(options.where.userId === 1 && options.where.status === 1, '只下线同一个人原来的在线记录');
|
||
assert(fields.logoutReason === 'relogin');
|
||
return [ 0 ];
|
||
},
|
||
async create(row) { created = row; return instance(Object.assign({ id: 99 }, row)); },
|
||
},
|
||
}));
|
||
|
||
const data = await loginByUser({ user: me, workstation });
|
||
|
||
assert(data.isMember === true, JSON.stringify(data));
|
||
assert(data.name === 'K0012');
|
||
assert(data.workstation.id === 3);
|
||
assert(data.workstation.subjectName === '测试主体');
|
||
assert(created.name === 'K0012', '成员登录要记成员姓名(工号)');
|
||
assert(created.status === 1 && created.type === 1, '默认在线 + 扫码登录类型');
|
||
assert(created.memberId === 11);
|
||
assert(lastActiveAt instanceof Date, '登录也要刷新工作台最后活跃时间');
|
||
});
|
||
|
||
it('service.workstationLogin.loginByUser:不是主体成员 → 记用户昵称', async () => {
|
||
let created = null;
|
||
mock(app, 'model', baseModels({
|
||
SubjectMember: { async findOne() { return null; } },
|
||
WorkstationLoginRecord: {
|
||
async update() { return [ 0 ]; },
|
||
async create(row) { created = row; return instance(Object.assign({ id: 100 }, row)); },
|
||
},
|
||
}));
|
||
|
||
const data = await loginByUser({
|
||
user: me,
|
||
workstation: instance({
|
||
id: 4,
|
||
subjectId: SUBJECT_ID,
|
||
code: 'WS-02',
|
||
name: '2号工作台',
|
||
wsType: 1,
|
||
status: 1,
|
||
}),
|
||
});
|
||
|
||
assert(data.isMember === false, JSON.stringify(data));
|
||
assert(data.name === '小王', '非成员用自己设置的昵称');
|
||
assert(created.name === '小王' && created.memberId === null);
|
||
});
|
||
|
||
it('service.workstationLogin.loginByUser:工作台停用 → 403;主体过期 → 403', async () => {
|
||
mock(app, 'model', baseModels({
|
||
WorkstationLoginRecord: { async update() { return [ 0 ]; } },
|
||
}));
|
||
|
||
await assert.rejects(
|
||
() => loginByUser({
|
||
user: me,
|
||
workstation: instance({ id: 5, subjectId: SUBJECT_ID, code: 'WS-03', status: 0 }),
|
||
}),
|
||
err => err.status === 403 && /停用/.test(err.message),
|
||
);
|
||
|
||
mock(app, 'model', baseModels({
|
||
Subject: {
|
||
async findByPk() {
|
||
return instance({
|
||
id: 6,
|
||
name: '过期主体',
|
||
adminUserId: 1,
|
||
status: 1,
|
||
expireAt: new Date(Date.now() - 1000),
|
||
});
|
||
},
|
||
},
|
||
WorkstationLoginRecord: { async update() { return [ 0 ]; } },
|
||
}));
|
||
|
||
await assert.rejects(
|
||
() => loginByUser({
|
||
user: me,
|
||
workstation: instance({ id: 6, subjectId: 6, code: 'WS-04', status: 1 }),
|
||
}),
|
||
err => err.status === 403 && /使用期限已到/.test(err.message),
|
||
);
|
||
});
|
||
|
||
const onlineRecord = () => instance({
|
||
id: 21,
|
||
subjectId: SUBJECT_ID,
|
||
workstationId: 3,
|
||
userId: 1,
|
||
memberId: 11,
|
||
name: 'K0012',
|
||
type: 1,
|
||
loginAt: new Date('2026-10-24T09:00:00+08:00'),
|
||
logoutAt: null,
|
||
status: 1,
|
||
logoutReason: null,
|
||
loginIp: '127.0.0.1',
|
||
});
|
||
|
||
it('GET /api/web/workstations/:id/members?status=1:只看在线,带在线人数', async () => {
|
||
mock(app, 'model', baseModels({
|
||
Workstation: {
|
||
async findByPk() {
|
||
return instance({ id: 3, subjectId: SUBJECT_ID, code: 'WS-01', name: '1号工作台' });
|
||
},
|
||
},
|
||
WorkstationLoginRecord: {
|
||
async findAndCountAll(options) {
|
||
assert(options.where.workstationId === 3, '必须按工作台过滤');
|
||
assert(options.where.status === 1, 'status=1 时只看在线');
|
||
// 时间段过滤(配合工作台 id 查「昨天这台工作台上是谁」)
|
||
assert(options.where.loginAt, '带时间段时必须有 loginAt 条件');
|
||
assert(Object.getOwnPropertySymbols(options.where.loginAt).length === 2, '起止时间都要带上');
|
||
return { rows: [ onlineRecord() ], count: 1 };
|
||
},
|
||
async count() { return 1; },
|
||
},
|
||
}));
|
||
|
||
const res = await withSubject(app.httpRequest()
|
||
.get('/api/web/workstations/3/members?status=1&startTime=2026-10-24%2000:00:00&endTime=2026-10-25%2000:00:00'))
|
||
.expect(200);
|
||
assert(res.body.data.onlineCount === 1, JSON.stringify(res.body));
|
||
assert(res.body.data.list[0].name === 'K0012');
|
||
assert(res.body.data.list[0].status === 1);
|
||
});
|
||
|
||
it('POST /api/web/workstations/:id/members/:recordId/offline:写下线时间与原因', async () => {
|
||
const record = onlineRecord();
|
||
mock(app, 'model', baseModels({
|
||
Workstation: {
|
||
async findByPk() {
|
||
return instance({ id: 3, subjectId: SUBJECT_ID, code: 'WS-01' });
|
||
},
|
||
},
|
||
WorkstationLoginRecord: {
|
||
async findOne() { return record; },
|
||
},
|
||
}));
|
||
|
||
const res = await withSubject(app.httpRequest().post('/api/web/workstations/3/members/21/offline')).expect(200);
|
||
|
||
assert(res.body.data.status === 0, JSON.stringify(res.body));
|
||
assert(res.body.data.logoutReason === 'manual');
|
||
assert(record.toJSON().logoutAt instanceof Date, '必须记录下线时间');
|
||
});
|
||
|
||
it('POST /api/web/workstations/:id/members/offline-all:把在线成员全部下线', async () => {
|
||
mock(app, 'model', baseModels({
|
||
Workstation: {
|
||
async findByPk() {
|
||
return instance({ id: 3, subjectId: SUBJECT_ID, code: 'WS-01' });
|
||
},
|
||
},
|
||
WorkstationLoginRecord: {
|
||
async update(fields, options) {
|
||
assert(options.where.workstationId === 3 && options.where.status === 1, '只下线本工作台的在线记录');
|
||
assert(fields.logoutReason === 'offline_all');
|
||
return [ 2 ];
|
||
},
|
||
},
|
||
}));
|
||
|
||
const res = await withSubject(app.httpRequest().post('/api/web/workstations/3/members/offline-all')).expect(200);
|
||
|
||
assert(res.body.data.offline === 2, JSON.stringify(res.body));
|
||
});
|
||
});
|