refactor(frontend): 重构前端第二版
This commit is contained in:
1 parent
2631f78b00
commit
4b562cfa82
205 files changed
+15399
-12015
No files matched your search
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* 出货聊天:库存/发货通知文件上传、隐藏 file input、附件建议提示。
|
||||
*/
|
||||
import { computed } from 'vue';
|
||||
import {
|
||||
createWorkspaceShippingTask,
|
||||
createWorkspaceShippingTaskFromNotice,
|
||||
uploadWorkspaceShippingInventory,
|
||||
} from '@/features/chat/api/shipping.api';
|
||||
import { extractExt } from '../constants/shipping';
|
||||
import { buildShippingInventoryUploadPrompt, classifyShippingUploadFile } from './shippingUploadPrompt';
|
||||
import type { PendingAttachment } from '../types/shipping.types';
|
||||
import type { Ref } from 'vue';
|
||||
|
||||
export interface ShippingUploadHandlerContext {
|
||||
conversationId: Ref<string | null>;
|
||||
attachments: Ref<PendingAttachment[]>;
|
||||
input: Ref<string>;
|
||||
shippingAutoFlowRequested: Ref<boolean>;
|
||||
shippingInventoryPickerRef: Ref<HTMLInputElement | null>;
|
||||
pendingShippingUploadTaskId: Ref<string>;
|
||||
resolvedShippingTaskId: Ref<string>;
|
||||
shippingInventorySchemaPreviewCache: Ref<Record<string, unknown>>;
|
||||
getActiveShippingTaskId: () => string;
|
||||
sendShippingMenuMessage: (message: string) => Promise<void>;
|
||||
pushShippingLocalAssistantMessage: (text: string) => Promise<void>;
|
||||
refreshShippingActions: () => Promise<void>;
|
||||
msg: { warning: (text: string) => void };
|
||||
t: (key: string) => string;
|
||||
}
|
||||
|
||||
export function createShippingUploadHandlers(ctx: ShippingUploadHandlerContext) {
|
||||
const continueShippingInventoryUploadWithDify = async (
|
||||
taskId: string,
|
||||
result: Record<string, unknown> | null | undefined,
|
||||
fileName?: string,
|
||||
) => {
|
||||
const key = String(taskId || '').trim().toUpperCase();
|
||||
const schemaPreview = (result?.data as { schema_preview?: unknown } | undefined)?.schema_preview;
|
||||
if (key && schemaPreview && typeof schemaPreview === 'object') {
|
||||
ctx.shippingInventorySchemaPreviewCache.value = {
|
||||
...ctx.shippingInventorySchemaPreviewCache.value,
|
||||
[key]: schemaPreview,
|
||||
};
|
||||
}
|
||||
ctx.resolvedShippingTaskId.value = taskId;
|
||||
await ctx.sendShippingMenuMessage(buildShippingInventoryUploadPrompt(result, fileName));
|
||||
};
|
||||
|
||||
const handleShippingAutoUploadFile = async (file: File) => {
|
||||
const kind = classifyShippingUploadFile(file, extractExt);
|
||||
if (kind === 'inventory') {
|
||||
let taskId = ctx.getActiveShippingTaskId();
|
||||
if (!taskId) {
|
||||
const created = await createWorkspaceShippingTask({
|
||||
source_type: 'manual',
|
||||
conversation_id: String(ctx.conversationId.value || '').trim() || undefined,
|
||||
});
|
||||
taskId = String(created.task_id || '').trim();
|
||||
if (!taskId) {
|
||||
ctx.msg.warning('自动创建发货任务失败');
|
||||
return;
|
||||
}
|
||||
}
|
||||
ctx.resolvedShippingTaskId.value = taskId;
|
||||
const result = await uploadWorkspaceShippingInventory(taskId, file);
|
||||
await continueShippingInventoryUploadWithDify(taskId, result as unknown as Record<string, unknown>, file.name);
|
||||
return;
|
||||
}
|
||||
const result = await createWorkspaceShippingTaskFromNotice(
|
||||
file,
|
||||
String(ctx.conversationId.value || '').trim() || undefined,
|
||||
);
|
||||
const taskId = String(result.task_id || '').trim();
|
||||
if (taskId) ctx.resolvedShippingTaskId.value = taskId;
|
||||
const hint = String(result.message || '').trim();
|
||||
const status = String((result as { status?: string }).status || '').trim();
|
||||
const tail = status === 'needs_human' ? '请根据提示进行确认/驳回。' : '';
|
||||
await ctx.pushShippingLocalAssistantMessage(
|
||||
[
|
||||
hint || `已处理发货通知附件:${file.name}`,
|
||||
taskId ? `任务编号:${taskId}` : '',
|
||||
tail,
|
||||
].filter(Boolean).join('\n'),
|
||||
);
|
||||
void ctx.refreshShippingActions();
|
||||
};
|
||||
|
||||
const openShippingInventoryPicker = (): boolean => {
|
||||
const taskId = ctx.getActiveShippingTaskId();
|
||||
if (!taskId) {
|
||||
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
|
||||
return false;
|
||||
}
|
||||
ctx.pendingShippingUploadTaskId.value = taskId;
|
||||
ctx.shippingInventoryPickerRef.value?.click();
|
||||
return true;
|
||||
};
|
||||
|
||||
const handleShippingInventorySelected = async (event: Event) => {
|
||||
const fileInput = event.target as HTMLInputElement;
|
||||
const file = fileInput.files?.[0];
|
||||
fileInput.value = '';
|
||||
const taskId = ctx.pendingShippingUploadTaskId.value;
|
||||
ctx.pendingShippingUploadTaskId.value = '';
|
||||
if (!file || !taskId) return;
|
||||
try {
|
||||
const result = await uploadWorkspaceShippingInventory(taskId, file);
|
||||
await continueShippingInventoryUploadWithDify(taskId, result as unknown as Record<string, unknown>, file.name);
|
||||
} catch (e: unknown) {
|
||||
const errorMessage = e instanceof Error ? e.message : ctx.t('chat.shippingNoticeUploadFailed');
|
||||
ctx.msg.warning(errorMessage);
|
||||
}
|
||||
};
|
||||
|
||||
const shippingSuggestedPrompt = computed(() => {
|
||||
const first = ctx.attachments.value.find((a) => a.kind === 'local') as
|
||||
| Extract<PendingAttachment, { kind: 'local' }>
|
||||
| undefined;
|
||||
if (!first) return '';
|
||||
const kind = classifyShippingUploadFile(first.file, extractExt);
|
||||
if (kind === 'inventory') return '上传库存并进入字段对齐';
|
||||
return '解析发货通知并创建任务';
|
||||
});
|
||||
|
||||
const useShippingSuggestedPrompt = () => {
|
||||
const prompt = shippingSuggestedPrompt.value;
|
||||
if (!prompt) return;
|
||||
ctx.input.value = prompt;
|
||||
ctx.shippingAutoFlowRequested.value = true;
|
||||
};
|
||||
|
||||
return {
|
||||
handleShippingAutoUploadFile,
|
||||
openShippingInventoryPicker,
|
||||
handleShippingInventorySelected,
|
||||
shippingSuggestedPrompt,
|
||||
useShippingSuggestedPrompt,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user