refactor(frontend): 重构前端第二版

This commit is contained in:
sophie committed 2026-06-11 13:47:06 +08:00
1 parent 2631f78b00
commit 4b562cfa82
205 files changed
+15399 -12015

No files matched your search

@@ -0,0 +1,176 @@
/**
* 出货聊天:消息气泡动作按钮与菜单项点击分发。
*
* 按 action.id 路由到 API 调用、决策提交、文件选择器或 Dify 菜单消息。
*/
import {
generateWorkspaceShippingExcel,
matchWorkspaceShippingInventory,
} from '@/features/chat/api/shipping.api';
import { SHIPPING_DECISION_ACTIONS } from '../constants/shipping';
import { downloadWorkspaceShippingExcelFile } from './shippingDownload';
import type { ChatMessage } from '../types/chat.types';
import type { Ref } from 'vue';
export interface ShippingMenuHandlerContext {
isThinking: Ref<boolean>;
getActiveShippingTaskId: () => string;
messages: Ref<ChatMessage[]>;
newLocalMessageId: (prefix: string) => string;
saveLocalMessageToBackend: (msg: ChatMessage, userQuery?: string) => Promise<void>;
sendShippingMenuMessage: (message: string) => Promise<void>;
openShippingInventoryMappingFormForTask: (taskId: string) => Promise<boolean>;
openShippingInventoryPicker: () => boolean;
executeShippingDecision: (decision: string, displayText?: string) => Promise<boolean>;
executeShippingManualUpload: (taskId: string, displayText?: string) => Promise<boolean>;
refreshShippingActions: () => Promise<void>;
msg: { warning: (text: string) => void; info: (text: string) => void };
t: (key: string) => string;
}
export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
const handleShippingMenuAction = async (
action: { id?: string; message: string },
chatMsg?: ChatMessage & { shipping_actions_used?: boolean },
) => {
if (ctx.isThinking.value) return;
if (action.id === 'refresh') {
await ctx.sendShippingMenuMessage(action.message || '刷新');
return;
}
if (action.id === 'download_excel') {
const taskId = ctx.getActiveShippingTaskId();
if (!taskId) {
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
return;
}
try {
await downloadWorkspaceShippingExcelFile(taskId);
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : '下载 Excel 失败';
ctx.msg.warning(errorMessage);
}
return;
}
if (action.id === 'generate_excel') {
if (chatMsg?.shipping_actions_used) return;
if (chatMsg) chatMsg.shipping_actions_used = true;
const taskId = ctx.getActiveShippingTaskId();
if (!taskId) {
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
return;
}
try {
const result = await generateWorkspaceShippingExcel(taskId);
const userMsg: ChatMessage = {
role: 'user',
type: 'text',
content: `${action.message || '生成发货 Excel'}(任务编号:${taskId})`,
local_id: ctx.newLocalMessageId('shipping-user'),
local_only: true,
};
const assistantMsg: ChatMessage = {
role: 'assistant',
type: 'text',
content: [
String(result.message || '').trim(),
`任务编号:${taskId}`,
].filter(Boolean).join('\n'),
local_id: ctx.newLocalMessageId('shipping-assistant'),
local_only: true,
};
ctx.messages.value.push(userMsg, assistantMsg);
await ctx.saveLocalMessageToBackend(userMsg, action.message || '生成发货 Excel');
await ctx.saveLocalMessageToBackend(assistantMsg, action.message || '生成发货 Excel');
void ctx.refreshShippingActions();
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : '生成发货 Excel 失败';
ctx.msg.warning(errorMessage);
}
return;
}
if (action.id === 'edit_inventory_mapping') {
const taskId = ctx.getActiveShippingTaskId();
if (!taskId) {
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
return;
}
await ctx.openShippingInventoryMappingFormForTask(taskId);
return;
}
if (action.id === 'upload_inventory_file') {
ctx.openShippingInventoryPicker();
return;
}
if (action.id === 'match_inventory') {
if (chatMsg?.shipping_actions_used) return;
if (chatMsg) chatMsg.shipping_actions_used = true;
const taskId = ctx.getActiveShippingTaskId();
if (!taskId) {
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
return;
}
try {
const result = await matchWorkspaceShippingInventory(taskId);
const userMsg: ChatMessage = {
role: 'user',
type: 'text',
content: `${action.message || '匹配库存'}(任务编号:${taskId})`,
local_id: ctx.newLocalMessageId('shipping-user'),
local_only: true,
};
const assistantMsg: ChatMessage = {
role: 'assistant',
type: 'text',
content: [
String(result.message || '').trim(),
`任务编号:${taskId}`,
].filter(Boolean).join('\n'),
local_id: ctx.newLocalMessageId('shipping-assistant'),
local_only: true,
};
ctx.messages.value.push(userMsg, assistantMsg);
await ctx.saveLocalMessageToBackend(userMsg, action.message || '匹配库存');
await ctx.saveLocalMessageToBackend(assistantMsg, action.message || '匹配库存');
void ctx.refreshShippingActions();
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : '匹配库存失败';
ctx.msg.warning(errorMessage);
}
return;
}
if (action.id === 'mark_manual_uploaded') {
if (chatMsg?.shipping_actions_used) return;
if (chatMsg) chatMsg.shipping_actions_used = true;
const taskId = ctx.getActiveShippingTaskId();
if (!taskId) {
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
return;
}
await ctx.executeShippingManualUpload(taskId, action.message || '已在货代系统手工上传完成');
return;
}
if (action.id === 'paste_inventory_json') {
ctx.msg.info(ctx.t('chat.shippingNoticePasteHint'));
return;
}
if (action.id && SHIPPING_DECISION_ACTIONS.has(action.id)) {
if (chatMsg?.shipping_actions_used) return;
if (chatMsg) chatMsg.shipping_actions_used = true;
await ctx.executeShippingDecision(action.id, action.message);
return;
}
if (chatMsg?.shipping_actions_used) return;
if (chatMsg) chatMsg.shipping_actions_used = true;
await ctx.sendShippingMenuMessage(action.message);
};
const handleShippingActionClick = async (
chatMsg: ChatMessage & { shipping_actions_used?: boolean },
action: { id?: string; message: string },
) => {
await handleShippingMenuAction(action, chatMsg);
};
return { handleShippingMenuAction, handleShippingActionClick };
}