oms/backend/app/controller/orderSyncTask.js
liufumingren e1e3bb638f feat(order): 拉单任务、外部发货同步与取号记录
- 拉单任务:新增 order_sync_tasks 异步任务池,按店铺逐店拉取外部打单平台订单,前端轮询进度
- 外部发货同步:配送后把发货结果同步回店管家,提供数量统计与批量重试(externalSendSync)
- 取号:新增 waybill_fetch_records 记录取号过程,外部快递接口补充菜鸟授权信息
- 补齐相关迁移、订单页同步入口/状态展示,并新增对接说明文档

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-04 20:53:35 +08:00

102 lines
3.2 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');
const { parsePagination, buildPageResult } = require('../lib/pagination');
/**
* @controller OrderSyncTaskController 拉单任务(异步任务池)
*
* 一键拉取订单:提交(指定范围/回看天数/店铺)→ 立即返回任务ID → 定时任务逐店拉单 →
* 前端轮询进度查看每店结果。所有任务按主体隔离(principal_id 由 principalCheck 写入)。
*/
class OrderSyncTaskController extends Controller {
/**
* @summary 提交拉单任务
* @description 落一条「待处理」任务并立即返回任务ID;实际拉取由定时任务异步逐店执行
* @router post /api/order-sync-tasks
* @apikey
* @request body createOrderSyncTaskRequest *body
* @response 200 baseResponse success
*/
async submit() {
const { ctx } = this;
try {
const task = await ctx.service.orderSyncTask.submit(
ctx.request.body || {},
ctx.state.principalId,
ctx.userId
);
ctx.body = {
code: 0,
message: 'success',
data: {
id: Number(task.id),
scope: task.scope,
status: task.status,
total_shops: task.total_shops,
start_time: task.start_time,
end_time: task.end_time,
},
};
} catch (err) {
ctx.status = err.status || 500;
ctx.body = { code: err.status || 500, message: err.message };
}
}
/**
* @summary 拉单任务列表
* @router get /api/order-sync-tasks
* @apikey
* @request query integer page 页码
* @request query integer pageSize 每页条数
* @request query integer status 按状态筛选(0待处理 1处理中 2已完成 3部分失败 4失败)
* @request query string scope 按范围筛选
* @response 200 orderSyncTaskListResponse success
*/
async index() {
const { ctx } = this;
const { page, pageSize } = parsePagination(ctx.query);
const { list, total } = await ctx.service.orderSyncTask.list(ctx.state.principalId, {
page,
pageSize,
status: ctx.query.status,
scope: ctx.query.scope,
});
ctx.body = { code: 0, message: 'success', data: buildPageResult(list, total, { page, pageSize }) };
}
/**
* @summary 当前未完成的拉单任务
* @description 进入页面时恢复进度条用;没有未完成任务时 data 为 null
* @router get /api/order-sync-tasks/running
* @apikey
* @response 200 baseResponse success
*/
async running() {
const { ctx } = this;
const task = await ctx.service.orderSyncTask.runningTask(ctx.state.principalId);
ctx.body = { code: 0, message: 'success', data: task };
}
/**
* @summary 拉单任务进度
* @description 返回进度百分比与逐店结果(items)
* @router get /api/order-sync-tasks/:id
* @apikey
* @response 200 baseResponse success
*/
async progress() {
const { ctx } = this;
const task = await ctx.service.orderSyncTask.progress(Number(ctx.params.id), ctx.state.principalId);
if (!task) {
ctx.status = 404;
ctx.body = { code: 404, message: '任务不存在' };
return;
}
ctx.body = { code: 0, message: 'success', data: task };
}
}
module.exports = OrderSyncTaskController;