diff --git a/frontend/src/features/equipment-acceptance/api/equipment-acceptance.api.ts b/frontend/src/features/equipment-acceptance/api/equipment-acceptance.api.ts index 41ad0442..f1c2006c 100644 --- a/frontend/src/features/equipment-acceptance/api/equipment-acceptance.api.ts +++ b/frontend/src/features/equipment-acceptance/api/equipment-acceptance.api.ts @@ -3,6 +3,7 @@ * 用途:设备验收预警同步列表、详情和手动同步。 * 依赖:统一 requestJson;后端 /api/equipment-acceptance。 * 逻辑:Chat 作业面调用。同步只读 E10。配置只保存天数和系统用户,不保存发送时刻。 + * 保存配置自行声明 Content-Type: application/json,供 Flask get_json 解析。 * 发送记录只读已落库的行,扫描尚未写入时列表为空。 */ import { requestJson } from '@/shared/api/client'; @@ -206,8 +207,8 @@ export function getEquipmentAcceptanceConfig() { * @param payload.days - 四个逐档增大的天数 * @param payload.buyerManagerUserId - 采购经副理 * @param payload.cooUserId - COO - * @param payload.executiveUserId - 廖总对应岗位 - * @returns 保存后的配置 + * @param payload.executiveUserId - 总经理对应岗位 + * @returns 保存后的配置。请求头带 application/json,避免 Flask 把正文当成空对象。 */ export function saveEquipmentAcceptanceConfig(payload: { days: number[]; @@ -217,6 +218,7 @@ export function saveEquipmentAcceptanceConfig(payload: { }) { return requestJson('/api/equipment-acceptance/config', { method: 'PUT', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ days: payload.days, buyer_manager_user_id: payload.buyerManagerUserId, diff --git a/frontend/src/features/order-delivery-reply/api/orderDeliveryReply.api.spec.ts b/frontend/src/features/order-delivery-reply/api/orderDeliveryReply.api.spec.ts new file mode 100644 index 00000000..a9dbe6a0 --- /dev/null +++ b/frontend/src/features/order-delivery-reply/api/orderDeliveryReply.api.spec.ts @@ -0,0 +1,47 @@ +/** + * features/order-delivery-reply/api/orderDeliveryReply.api.spec.ts + * 用途:锁定交期回复 JSON 写请求自行声明 Content-Type。 + * 依赖:vitest、orderDeliveryReply.api。 + * 逻辑:requestJson 不再为所有 body 补 application/json;漏掉该头时 Flask get_json 会把保存当成空正文。 + */ +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +const requestJson = vi.hoisted(() => vi.fn()); + +vi.mock('@/shared/api/client', () => ({ + requestJson, +})); + +import { + batchReplyOrderDelivery, + notifyOrderDeliveryBatch, + saveOrderDeliveryBatches, +} from './orderDeliveryReply.api'; + +describe('order delivery reply JSON writes', () => { + beforeEach(() => { + requestJson.mockReset(); + requestJson.mockResolvedValue({}); + }); + + it('sends Content-Type application/json when saving one line', async () => { + await saveOrderDeliveryBatches('SO-1', 2, [{ planned_date: '2026-10-13', qty: '1' }]); + + const init = requestJson.mock.calls[0][1] as RequestInit; + // 断言:保存请求自己带 JSON 类型,不依赖公共客户端补头。 + expect((init.headers as Record)['Content-Type']).toBe('application/json'); + expect(init.method).toBe('PUT'); + expect(init.body).toBe(JSON.stringify({ batches: [{ planned_date: '2026-10-13', qty: '1' }] })); + }); + + it('sends Content-Type application/json for batch reply and batch notify', async () => { + await batchReplyOrderDelivery([]); + await notifyOrderDeliveryBatch([]); + + for (const [, init] of requestJson.mock.calls as [string, RequestInit][]) { + // 断言:联合回复与批量通知同样声明 JSON 类型。 + expect((init.headers as Record)['Content-Type']).toBe('application/json'); + expect(init.method).toBe('POST'); + } + }); +}); diff --git a/frontend/src/features/order-delivery-reply/api/orderDeliveryReply.api.ts b/frontend/src/features/order-delivery-reply/api/orderDeliveryReply.api.ts index 41d3b7d9..53774ac1 100644 --- a/frontend/src/features/order-delivery-reply/api/orderDeliveryReply.api.ts +++ b/frontend/src/features/order-delivery-reply/api/orderDeliveryReply.api.ts @@ -2,10 +2,26 @@ * 订单交期回复 API 类型与请求封装。 * 依赖:统一 requestJson(失败抛 BusinessError);后端 /api/order-delivery-reply。 * 逻辑:列表筛选分页、保存批次(单行/联合)、单行与批量通知、手动同步与最近同步状态。 + * 带 JSON 正文的写请求在本文件声明 Content-Type: application/json,供 Flask get_json 解析。 */ import { requestJson } from '@/shared/api/client'; +/** + * 组装带 JSON 正文的写请求。 + * 核心逻辑:显式声明 application/json。浏览器对字符串正文默认是 text/plain,Flask get_json 会读成空对象。 + * @param method - HTTP 方法 + * @param payload - 将序列化为 JSON 的请求体 + * @returns fetch 的 method、headers 与 body + */ +function jsonWrite(method: 'POST' | 'PUT', payload: unknown): RequestInit { + return { + method, + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(payload), + }; +} + /** 已保存的交期批次(计划交期 + 数量 + 可选备注)。 */ export interface DeliveryBatch { seq?: number; @@ -100,7 +116,7 @@ export const listOrderDeliveryLines = (params?: OrderDeliveryListParams) => { return requestJson(`/api/order-delivery-reply/lines${suffix}`); }; -/** 保存单行交期批次(未锁定行整表替换)。 */ +/** 保存单行交期批次(未锁定行整表替换)。请求头带 application/json,避免正文被 Flask 当成空对象。 */ export const saveOrderDeliveryBatches = ( orderNo: string, lineNo: number, @@ -108,17 +124,14 @@ export const saveOrderDeliveryBatches = ( ) => requestJson<{ order_no: string; line_no: number; batches: DeliveryBatch[]; total_qty: string }>( `/api/order-delivery-reply/lines/${encodeURIComponent(orderNo)}/${lineNo}/batches`, - { method: 'PUT', body: JSON.stringify({ batches }) }, + jsonWrite('PUT', { batches }), ); -/** 联合回复:对多行逐行保存批次(只保存,不通知不锁定)。 */ +/** 联合回复:对多行逐行保存批次(只保存,不通知不锁定)。请求头带 application/json。 */ export const batchReplyOrderDelivery = ( items: { order_no: string; line_no: number; batches: { planned_date: string; qty: string | number; remark?: string | null }[] }[], ) => - requestJson('/api/order-delivery-reply/batch-reply', { - method: 'POST', - body: JSON.stringify({ items }), - }); + requestJson('/api/order-delivery-reply/batch-reply', jsonWrite('POST', { items })); /** 单行发送通知业务部门(企微),成功后行级锁定。 */ export const notifyOrderDeliveryLine = (orderNo: string, lineNo: number) => @@ -127,12 +140,12 @@ export const notifyOrderDeliveryLine = (orderNo: string, lineNo: number) => { method: 'POST' }, ); -/** 批量通知勾选行;无批次/已锁定跳过,单行失败不回滚其它行。 */ +/** 批量通知勾选行;无批次/已锁定跳过,单行失败不回滚其它行。请求头带 application/json。 */ export const notifyOrderDeliveryBatch = (lines: { order_no: string; line_no: number }[]) => - requestJson('/api/order-delivery-reply/notify-batch', { - method: 'POST', - body: JSON.stringify({ lines }), - }); + requestJson( + '/api/order-delivery-reply/notify-batch', + jsonWrite('POST', { lines }), + ); /** 手动触发 ERP 全量同步(范围与 07:00 排程一致)。 */ export const runOrderDeliverySync = () => diff --git a/frontend/src/features/order-delivery-reply/views/OrderDeliveryReplyPage.vue b/frontend/src/features/order-delivery-reply/views/OrderDeliveryReplyPage.vue index 81a5fd40..dadb92e8 100644 --- a/frontend/src/features/order-delivery-reply/views/OrderDeliveryReplyPage.vue +++ b/frontend/src/features/order-delivery-reply/views/OrderDeliveryReplyPage.vue @@ -395,11 +395,12 @@ async function focusTargetLine(orderNo: string, lineNo: number) { highlightTimer = setTimeout(clearHighlight, 8000); } -/** 上报企微卡片打开回执(WecomNotifyAudit.opened_at);仅留痕,失败不影响页面使用。 */ +/** 上报企微卡片打开回执(WecomNotifyAudit.opened_at);仅留痕,失败不影响页面使用。请求头带 application/json,后端 get_json 才读得到 trace_id。 */ async function reportWecomTraceOpened(traceId: string) { try { await requestJson('/api/user/wecom/deeplink-opened', { method: 'POST', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ trace_id: traceId }), }); } catch { diff --git a/frontend/src/shared/api/client.ts b/frontend/src/shared/api/client.ts index 40c0b03e..3127d5a8 100644 --- a/frontend/src/shared/api/client.ts +++ b/frontend/src/shared/api/client.ts @@ -168,11 +168,6 @@ export async function requestJson(url: string, init?: RequestInit): Promise headers.set(k, v as string)); - // 带 body 的请求若未显式声明类型,统一补 JSON 头:否则 Flask get_json(silent=True) - // 会因 Content-Type 非 JSON 解析出 None,把有效载荷当空 body 处理(如订单交期回复保存被误判 400)。 - if (init?.body && !headers.has('Content-Type')) { - headers.set('Content-Type', 'application/json'); - } const res = await fetchJsonResponse(url, { ...init, headers }); let data: any = null;