11
This commit is contained in:
@@ -308,6 +308,98 @@ module.exports = {
|
||||
console.error('[任务管理] 获取任务统计失败:', error);
|
||||
return ctx.fail('获取任务统计失败: ' + (error.message || '未知错误'));
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 按设备 SN 分页查询任务指令列表(展示请求参数与响应结果)
|
||||
*/
|
||||
'POST /task/command/page_list': async (ctx) => {
|
||||
try {
|
||||
const body = ctx.getBody() || {};
|
||||
const sn_code = body.sn_code || ctx.query?.sn_code;
|
||||
const page = Math.max(1, Number(body.page) || 1);
|
||||
const raw_page_size = body.page_size ?? body.pageSize ?? 20;
|
||||
const page_size = Math.min(100, Math.max(1, Number(raw_page_size) || 20));
|
||||
const offset = (page - 1) * page_size;
|
||||
|
||||
if (!sn_code) {
|
||||
return ctx.fail('请提供设备SN码');
|
||||
}
|
||||
|
||||
const { task_status, task_commands, op } = await Framework.getModels();
|
||||
|
||||
// 先查当前账号对应任务,再按 task_id 关联查指令
|
||||
const task_rows = await task_status.findAll({
|
||||
where: { sn_code },
|
||||
attributes: ['id'],
|
||||
order: [['id', 'DESC']],
|
||||
limit: 2000
|
||||
});
|
||||
const task_ids = task_rows.map((row) => row.id).filter((id) => id != null);
|
||||
if (!task_ids.length) {
|
||||
return ctx.success({
|
||||
list: [],
|
||||
total: 0,
|
||||
page,
|
||||
page_size
|
||||
});
|
||||
}
|
||||
|
||||
const where = {
|
||||
task_id: { [op.in]: task_ids }
|
||||
};
|
||||
if (body.command_type) {
|
||||
where.command_type = String(body.command_type).trim();
|
||||
}
|
||||
if (body.status) {
|
||||
where.status = String(body.status).trim();
|
||||
}
|
||||
|
||||
const { rows, count } = await task_commands.findAndCountAll({
|
||||
where,
|
||||
attributes: [
|
||||
'id',
|
||||
'task_id',
|
||||
'command_type',
|
||||
'command_name',
|
||||
'command_params',
|
||||
'status',
|
||||
'priority',
|
||||
'sequence',
|
||||
'retry_count',
|
||||
'max_retries',
|
||||
'start_time',
|
||||
'end_time',
|
||||
'duration',
|
||||
'result',
|
||||
'error_message',
|
||||
'progress'
|
||||
],
|
||||
order: [['id', 'DESC']],
|
||||
limit: page_size,
|
||||
offset
|
||||
});
|
||||
|
||||
const list = rows.map((r) => {
|
||||
const j = r.toJSON ? r.toJSON() : r;
|
||||
return {
|
||||
...j,
|
||||
// 前端直接展示,避免超长字段压垮 UI
|
||||
command_params_preview: j.command_params ? String(j.command_params).slice(0, 1200) : '',
|
||||
result_preview: j.result ? String(j.result).slice(0, 1200) : ''
|
||||
};
|
||||
});
|
||||
|
||||
return ctx.success({
|
||||
list,
|
||||
total: count,
|
||||
page,
|
||||
page_size
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[任务管理] 获取指令分页失败:', error);
|
||||
return ctx.fail('获取指令分页失败: ' + (error.message || '未知错误'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user