203 lines
5.6 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 MainService = require('../main');
class WorkbenchService extends MainService {
constructor(ctx) {
super(ctx);
const _noNeedLogin = 'business,getWorkbenchInfo';
const _noNeedRight = 'list,detail,checkBarcode,business,getWorkbenchInfo';
const _noNeedLog = 'list,detail,checkBarcode,getWorkbenchInfo';
const _noNeedAuth = 'list,detail,checkBarcode,business,getWorkbenchInfo';
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
}
/**
* 工作台列表(分页 + 筛选)
* @param data
*/
async list(data) {
const { service } = this;
try {
const result = await service.systems.workbench.list(data || {});
return this.success('获取成功', result);
} catch (error) {
return this.fail(error.message);
}
}
/**
* 工作台详情
* @param data
*/
async detail(data) {
const { service } = this;
try {
const { id } = data || {};
if (!id) return this.fail('工作台ID不能为空');
const record = await service.systems.workbench.detail(parseInt(id));
return this.success('获取成功', record);
} catch (error) {
return this.fail(error.message);
}
}
/**
* 创建工作台
* @param data
*/
async create(data) {
const { service } = this;
try {
const record = await service.systems.workbench.create(data);
return this.success('创建成功', record);
} catch (error) {
return this.fail(error.message);
}
}
/**
* 更新工作台
* @param data
*/
async update(data) {
const { service } = this;
try {
const { id, ...fields } = data || {};
if (!id) return this.fail('工作台ID不能为空');
const record = await service.systems.workbench.update(parseInt(id), fields);
return this.success('更新成功', record);
} catch (error) {
return this.fail(error.message);
}
}
/**
* 删除工作台(软删除)
* @param data
*/
async delete(data) {
const { service } = this;
try {
const { id } = data || {};
if (!id) return this.fail('工作台ID不能为空');
await service.systems.workbench.delete(parseInt(id));
return this.success('删除成功');
} catch (error) {
return this.fail(error.message);
}
}
/**
* 工作台业务入口(免登录、免鉴权)
* @param {Object} data - { name, data }
*/
async business(data) {
const { ctx, service } = this;
const { name, data: bizData } = data || {};
ctx.logger.info('[Workbench.business] 收到业务请求 name=%s data=%j', name, bizData);
try {
const result = await service.systems.workbench.business(name, bizData);
return this.success('ok', result);
} catch (error) {
ctx.logger.error('[Workbench.business] 业务处理失败 name=%s error=%s', name, error.message);
return this.fail(error.message);
}
}
/**
* 工作台绑定摄像头
* @param data
*/
async bindCamera(data) {
const { service } = this;
try {
const { id, camera_ids } = data || {};
if (!id) return this.fail('工作台ID不能为空');
const record = await service.systems.workbench.bindCamera(parseInt(id), camera_ids);
return this.success('绑定成功', record);
} catch (error) {
return this.fail(error.message);
}
}
/**
* 检查条码是否已存在
* @param data
*/
async checkBarcode(data) {
const { service } = this;
try {
const { barcode, excludeId } = data || {};
if (!barcode) return this.fail('条码不能为空');
const exists = await service.systems.workbench.checkBarcodeExists(
String(barcode).trim(),
excludeId ? parseInt(excludeId) : undefined
);
return this.success('ok', { exists });
} catch (error) {
return this.fail(error.message);
}
}
/**
* 工作台绑定:校验缓存绑定码与工作台ID,写入随机 token(需登录、需权限)
* @param data - { code, id }
*/
async bind(data) {
const { service } = this;
try {
const result = await service.systems.workbench.bindWorkbench(data || {});
if (result && result.success) {
return this.success('绑定成功', result);
}
const statusCode = result && result.statusCode;
return this.fail(
result && result.message ? result.message : '绑定失败',
statusCode ? { code: statusCode } : undefined
);
} catch (error) {
return this.fail(error.message);
}
}
/**
* 使用 token 获取工作台信息(免登录、免鉴权)
* @param data - { token }
*/
async getWorkbenchInfo(data) {
const { service } = this;
try {
const result = await service.systems.workbench.getWorkbenchInfoByToken(data || {});
if (result && result.success) {
return this.success('获取成功', result);
}
const statusCode = result && result.statusCode;
return this.fail(
result && result.message ? result.message : '获取工作台信息失败',
statusCode ? { code: statusCode } : undefined
);
} catch (error) {
return this.fail(error.message);
}
}
/**
* 重置工作台登录码(需登录、需权限)
* @param data - { id }
*/
async resetLoginCode(data) {
const { service } = this;
try {
const { id } = data || {};
if (!id) return this.fail('工作台ID不能为空');
const result = await service.systems.workbench.resetLoginCode(parseInt(id));
return this.success('重置成功', result);
} catch (error) {
return this.fail(error.message);
}
}
}
module.exports = WorkbenchService;