453 lines
16 KiB
TypeScript
453 lines
16 KiB
TypeScript
/**
|
|
* features/chat/api/shipping.api.ts
|
|
* 用途:封装发货通知相关 HTTP API,包括任务/批次查询、库存上传、映射确认、人工决策与 Excel 导出。
|
|
* 依赖:浏览器 fetch、getAuthHeaders、shipping.api.types 类型定义。
|
|
* 逻辑:上游由 useChatShipping 与 shipping/* handler 调用本文件;本文件统一请求工作台后端 `/api/workspace/*` 路由并抛出标准错误。
|
|
*/
|
|
import { getAuthHeaders } from '@/shared/utils/auth';
|
|
import type {
|
|
ShippingBatchSummary,
|
|
ShippingChatActionsResponse,
|
|
ShippingDecisionResponse,
|
|
ShippingInventoryUploadResponse,
|
|
ShippingNoticeHeader,
|
|
ShippingNoticeLine,
|
|
ShippingNoticeUploadResponse,
|
|
ShippingTaskCreateResponse,
|
|
ShippingTaskDetail,
|
|
ShippingTaskSummary,
|
|
} from '../types/shipping.api.types';
|
|
|
|
async function getJson<T>(url: string): Promise<T> {
|
|
const res = await fetch(url, { headers: getAuthHeaders() });
|
|
if (!res.ok) {
|
|
const body = await res.json().catch(() => ({}));
|
|
throw new Error((body as { error?: string }).error || res.statusText);
|
|
}
|
|
return res.json() as Promise<T>;
|
|
}
|
|
|
|
export const listWorkspaceShippingTasks = (params?: { process_status?: string; limit?: number }) => {
|
|
/** 拉取当前用户可见的发货任务列表。 */
|
|
const sp = new URLSearchParams();
|
|
if (params?.process_status) sp.set('process_status', params.process_status);
|
|
if (params?.limit) sp.set('limit', String(params.limit));
|
|
const qs = sp.toString();
|
|
return getJson<ShippingTaskSummary[]>(`/api/workspace/shipping-tasks${qs ? '?' + qs : ''}`);
|
|
};
|
|
|
|
export const listWorkspaceShippingBatches = (params?: { limit?: number }) => {
|
|
/** 拉取当前用户的发货批次摘要列表。 */
|
|
const sp = new URLSearchParams();
|
|
if (params?.limit) sp.set('limit', String(params.limit));
|
|
const qs = sp.toString();
|
|
return getJson<ShippingBatchSummary[]>(`/api/workspace/shipping-batches${qs ? '?' + qs : ''}`);
|
|
};
|
|
|
|
/** 获取批次详情、批次任务列表与当前焦点任务。 */
|
|
export const getWorkspaceShippingBatch = (batchNo: string) =>
|
|
getJson<ShippingBatchSummary & { tasks?: ShippingTaskSummary[]; focus_task?: ShippingTaskSummary | null }>(
|
|
`/api/workspace/shipping-batches/${encodeURIComponent(batchNo)}`,
|
|
);
|
|
|
|
/** 获取指定批次当前焦点任务。 */
|
|
export const getWorkspaceShippingBatchFocusTask = (batchNo: string) =>
|
|
getJson<ShippingTaskSummary>(
|
|
`/api/workspace/shipping-batches/${encodeURIComponent(batchNo)}/focus-task`,
|
|
);
|
|
|
|
/** 获取单个发货任务详情。 */
|
|
export const getWorkspaceShippingTask = (taskNo: string) =>
|
|
getJson<ShippingTaskDetail>(`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}`);
|
|
|
|
/** 生成任务导出文件的前端下载 URL。 */
|
|
export const workspaceShippingExportUrl = (taskNo: string) =>
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/export`;
|
|
|
|
/** 获取聊天气泡动作按钮、检查点和 human options。 */
|
|
export const getShippingChatActions = (taskNo: string) =>
|
|
getJson<ShippingChatActionsResponse>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/chat-actions`,
|
|
);
|
|
|
|
/**
|
|
* 上传库存 Excel,并由后端完成解析与 schema preview 生成。
|
|
* @param taskNo - 发货任务编号
|
|
* @param file - 用户选择的库存文件
|
|
* @returns 库存上传与解析结果
|
|
*/
|
|
export async function uploadWorkspaceShippingInventory(taskNo: string, file: File) {
|
|
const form = new FormData();
|
|
form.append('file', file);
|
|
const headers = getAuthHeaders();
|
|
delete (headers as Record<string, string>)['Content-Type'];
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/upload-inventory`,
|
|
{ method: 'POST', headers, body: form },
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingInventoryUploadResponse & { error?: string };
|
|
if (!res.ok) {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 提交库存字段映射。
|
|
* @param taskNo - 发货任务编号
|
|
* @param columnMappingJson - 前端表单确认后的 map_json
|
|
* @returns 映射应用与库存解析结果
|
|
*/
|
|
export async function applyWorkspaceShippingInventoryMapping(taskNo: string, columnMappingJson: Record<string, unknown>) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/apply-inventory-mapping`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ map_json: columnMappingJson }),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingInventoryUploadResponse & { error?: string };
|
|
if (!res.ok) {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 创建单个发货任务。
|
|
* @param params - 建单参数,可包含 source_type、conversation_id、raw_content
|
|
* @returns 建单结果
|
|
*/
|
|
export async function createWorkspaceShippingTask(params?: {
|
|
source_type?: string;
|
|
conversation_id?: string;
|
|
raw_content?: string;
|
|
}) {
|
|
const res = await fetch('/api/workspace/shipping-tasks', {
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(params || {}),
|
|
});
|
|
const body = (await res.json().catch(() => ({}))) as ShippingTaskCreateResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 上传单个发货通知附件并立即建单解析。
|
|
* @param file - 发货通知文件
|
|
* @param conversationId - 可选会话 ID,用于把任务绑定到聊天会话
|
|
* @returns 建单与解析结果
|
|
*/
|
|
export async function createWorkspaceShippingTaskFromNotice(file: File, conversationId?: string) {
|
|
const form = new FormData();
|
|
form.append('file', file);
|
|
if (conversationId) form.append('conversation_id', conversationId);
|
|
const headers = getAuthHeaders();
|
|
delete (headers as Record<string, string>)['Content-Type'];
|
|
const res = await fetch('/api/workspace/shipping-tasks/create-from-notice', {
|
|
method: 'POST',
|
|
headers,
|
|
body: form,
|
|
});
|
|
const body = (await res.json().catch(() => ({}))) as ShippingNoticeUploadResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 批量上传多份发货通知并创建批次。
|
|
* @param files - 发货通知附件列表
|
|
* @param conversationId - 可选会话 ID
|
|
* @returns 批次与任务创建结果
|
|
*/
|
|
export async function createWorkspaceShippingBatchFromNotices(files: File[], conversationId?: string) {
|
|
const form = new FormData();
|
|
files.forEach((file) => form.append('files', file));
|
|
if (conversationId) form.append('conversation_id', conversationId);
|
|
const headers = getAuthHeaders();
|
|
delete (headers as Record<string, string>)['Content-Type'];
|
|
const res = await fetch('/api/workspace/shipping-batches/create-from-notices', {
|
|
method: 'POST',
|
|
headers,
|
|
body: form,
|
|
});
|
|
const body = (await res.json().catch(() => ({}))) as ShippingNoticeUploadResponse & {
|
|
error?: string;
|
|
batch_no?: string;
|
|
data?: unknown;
|
|
};
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 切换批次当前焦点任务。
|
|
* @param batchNo - 批次编号
|
|
* @param taskNo - 目标任务编号
|
|
* @returns 更新后的批次摘要与焦点任务
|
|
*/
|
|
export async function switchWorkspaceShippingBatchFocusTask(batchNo: string, taskNo: string) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-batches/${encodeURIComponent(batchNo)}/focus-task`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ task_no: taskNo }),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as {
|
|
error?: string;
|
|
batch?: ShippingBatchSummary;
|
|
focus_task?: ShippingTaskSummary;
|
|
};
|
|
if (!res.ok) {
|
|
throw new Error(body.error || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 提交人工决策。
|
|
* @param taskNo - 发货任务编号
|
|
* @param decision - 决策 ID,如 confirm、reject、reparse
|
|
* @param comment - 可选备注
|
|
* @returns 决策执行结果
|
|
*/
|
|
export async function submitWorkspaceShippingDecision(taskNo: string, decision: string, comment?: string) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/submit-decision`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ decision, ...(comment ? { comment } : {}) }),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 更新发货通知单头字段。
|
|
* @param taskNo - 发货任务编号
|
|
* @param fields - 需要更新的单头字段
|
|
* @param comment - 可选操作备注
|
|
* @returns 更新结果
|
|
*/
|
|
export async function updateWorkspaceShippingNoticeFields(
|
|
taskNo: string,
|
|
fields: Partial<ShippingNoticeHeader>,
|
|
comment?: string,
|
|
) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/update-notice-fields`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ fields, ...(comment ? { comment } : {}) }),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 更新指定订单行。
|
|
* @param taskNo - 发货任务编号
|
|
* @param lineIndex - 订单行下标
|
|
* @param patch - 行字段补丁
|
|
* @param comment - 可选操作备注
|
|
* @returns 更新结果
|
|
*/
|
|
export async function updateWorkspaceShippingNoticeLine(
|
|
taskNo: string,
|
|
lineIndex: number,
|
|
patch: Partial<ShippingNoticeLine> & { quantity?: number },
|
|
comment?: string,
|
|
) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/update-notice-line`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ line_index: lineIndex, patch, ...(comment ? { comment } : {}) }),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 为发货通知新增订单行。
|
|
* @param taskNo - 发货任务编号
|
|
* @param line - 新增的订单行内容
|
|
* @param comment - 可选操作备注
|
|
* @returns 更新结果
|
|
*/
|
|
export async function addWorkspaceShippingNoticeLine(
|
|
taskNo: string,
|
|
line: Partial<ShippingNoticeLine> & { quantity?: number },
|
|
comment?: string,
|
|
) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/add-notice-line`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ line, ...(comment ? { comment } : {}) }),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 删除指定订单行。
|
|
* @param taskNo - 发货任务编号
|
|
* @param lineIndex - 待删除的订单行下标
|
|
* @param comment - 可选操作备注
|
|
* @returns 更新结果
|
|
*/
|
|
export async function removeWorkspaceShippingNoticeLine(
|
|
taskNo: string,
|
|
lineIndex: number,
|
|
comment?: string,
|
|
) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/remove-notice-line`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ line_index: lineIndex, ...(comment ? { comment } : {}) }),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 执行库存匹配。
|
|
* @param taskNo - 发货任务编号
|
|
* @returns 匹配结果与后续动作提示
|
|
*/
|
|
export async function matchWorkspaceShippingInventory(taskNo: string) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/match-inventory`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({}),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 以用户态触发自动推进,作为聊天前端兜底入口。
|
|
* @param taskNo - 发货任务编号
|
|
* @param lang - 可选语言代码
|
|
* @returns 自动推进结果
|
|
*/
|
|
export async function autoProgressWorkspaceShippingTask(taskNo: string, lang?: string) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/auto-progress`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(lang ? { lang } : {}),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 标记货代系统已人工上传完成。
|
|
* @param taskNo - 发货任务编号
|
|
* @returns 上传确认结果
|
|
*/
|
|
export async function markWorkspaceShippingManualUploaded(taskNo: string) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/mark-manual-uploaded`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({}),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/**
|
|
* 生成发货 Excel。
|
|
* @param taskNo - 发货任务编号
|
|
* @returns 生成结果,成功时通常会包含下载链接
|
|
*/
|
|
export async function generateWorkspaceShippingExcel(taskNo: string) {
|
|
const res = await fetch(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/generate-excel`,
|
|
{
|
|
method: 'POST',
|
|
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({}),
|
|
},
|
|
);
|
|
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
|
if (!res.ok || body.status === 'failed') {
|
|
throw new Error(body.error || body.message || res.statusText);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/** 货代发货 Agent 显示名称,用于聊天路由与 Agent 匹配。 */
|
|
export const SHIPPING_NOTICE_AGENT_NAME = '货代发货通知 Agent';
|
|
|
|
export type {
|
|
ShippingBatchSummary,
|
|
ShippingChatAction,
|
|
ShippingChatActionsResponse,
|
|
ShippingCurrentStepProgress,
|
|
ShippingDecisionResponse,
|
|
ShippingInventoryUploadResponse,
|
|
ShippingMatchLineResult,
|
|
ShippingNoticeHeader,
|
|
ShippingNoticeLine,
|
|
ShippingNoticeUploadResponse,
|
|
ShippingTaskCreateResponse,
|
|
ShippingTaskDetail,
|
|
ShippingTaskSummary,
|
|
} from '../types/shipping.api.types';
|