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,346 @@
/**
* features/chat/components/ChatInputBar.vue
* 聊天输入栏:文本框、附件、语音、知识库选择与发送按钮。
*/
<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue';
import type { Ref } from 'vue';
import {
Check,
ChevronDown,
Database,
Globe,
Link2,
Loader2,
Mic,
Paperclip,
Send,
X,
} from 'lucide-vue-next';
import type { PendingAttachment } from '../types/shipping.types';
export interface ChatInputAttachmentPolicy {
anyEnabled: boolean;
canLocal: boolean;
canRemote: boolean;
accept: string;
}
const inputValue = defineModel<string>({ required: true });
const selectedKb = defineModel<string>('selectedKb', { required: true });
const props = defineProps<{
attachments: PendingAttachment[];
attachmentPolicy: ChatInputAttachmentPolicy;
availableKbOptions: string[];
kbDropdownDisabled: boolean;
isThinking: boolean;
isVoiceRecording: boolean;
isVoiceTranscribing: boolean;
isUploadingAttachments: boolean;
canSendMessage: boolean;
isShippingNoticeAgent: boolean;
shippingSuggestedPrompt: string;
/** 用对象包裹 Ref,避免 template 自动解包 */
shippingPickerRefHolder: { pickerRef: Ref<HTMLInputElement | null> };
getKbLabel: (kb: string) => string;
browserPanelOpen: boolean;
}>();
const emit = defineEmits<{
send: [];
'toggle-browser': [];
'voice-click': [];
'remove-attachment': [id: string];
'local-files-selected': [event: Event];
'add-remote-url': [url: string];
'shipping-inventory-selected': [event: Event];
'use-shipping-suggested-prompt': [];
}>();
const filePickerRef = ref<HTMLInputElement | null>(null);
const attachmentMenuRef = ref<HTMLElement | null>(null);
const kbMenuRef = ref<HTMLElement | null>(null);
const attachmentUrlInputRef = ref<HTMLInputElement | null>(null);
const showAttachmentMenu = ref(false);
const showAttachmentUrlPanel = ref(false);
const showKbDropdown = ref(false);
const attachmentUrlInput = ref('');
const closeAttachmentMenu = () => {
showAttachmentMenu.value = false;
showAttachmentUrlPanel.value = false;
attachmentUrlInput.value = '';
};
const toggleAttachmentMenu = () => {
if (props.isThinking || props.isUploadingAttachments) return;
showAttachmentMenu.value = !showAttachmentMenu.value;
if (!showAttachmentMenu.value) {
showAttachmentUrlPanel.value = false;
attachmentUrlInput.value = '';
}
};
const selectAttachmentLocal = () => {
if (!props.attachmentPolicy.canLocal) return;
closeAttachmentMenu();
filePickerRef.value?.click();
};
const selectAttachmentUrl = async () => {
if (!props.attachmentPolicy.canRemote) return;
showAttachmentMenu.value = true;
showAttachmentUrlPanel.value = true;
await nextTick();
attachmentUrlInputRef.value?.focus();
};
const toggleKbDropdown = () => {
if (props.kbDropdownDisabled) {
showKbDropdown.value = false;
return;
}
showKbDropdown.value = !showKbDropdown.value;
};
const selectKb = (option: string) => {
if (!props.availableKbOptions.includes(option)) return;
selectedKb.value = option;
showKbDropdown.value = false;
};
const submitRemoteUrl = () => {
const raw = attachmentUrlInput.value.trim();
if (!raw) return;
emit('add-remote-url', raw);
closeAttachmentMenu();
};
const bindShippingPickerRef = (el: unknown) => {
props.shippingPickerRefHolder.pickerRef.value = (el as HTMLInputElement | null) ?? null;
};
const handleDocumentPointerDown = (event: MouseEvent | TouchEvent) => {
const target = event.target as Node | null;
if (!target) return;
if (showKbDropdown.value && kbMenuRef.value && !kbMenuRef.value.contains(target)) {
showKbDropdown.value = false;
}
if (showAttachmentMenu.value && attachmentMenuRef.value && !attachmentMenuRef.value.contains(target)) {
closeAttachmentMenu();
}
};
onMounted(() => {
document.addEventListener('mousedown', handleDocumentPointerDown);
document.addEventListener('touchstart', handleDocumentPointerDown, { passive: true });
});
onBeforeUnmount(() => {
document.removeEventListener('mousedown', handleDocumentPointerDown);
document.removeEventListener('touchstart', handleDocumentPointerDown);
});
</script>
<template>
<footer class="safe-bottom sticky bottom-0 z-20 bg-transparent p-3 md:p-6">
<div class="relative mx-auto max-w-4xl">
<div class="flex flex-col rounded-2xl bg-surface p-2 shadow-[0_0_15px_rgba(0,0,0,0.05)] transition-all">
<textarea
v-model="inputValue"
rows="1"
:placeholder="isVoiceTranscribing ? $t('chat.voiceTranscribing') : isVoiceRecording ? $t('chat.voiceListening') : $t('chat.askAnything')"
class="w-full resize-none border-none bg-transparent px-3 py-2 text-[15px] leading-relaxed text-primary placeholder-slate-400 focus:border-transparent focus:outline-none focus:ring-0"
style="min-height: 44px; max-height: 220px;"
:disabled="isThinking || isVoiceTranscribing"
spellcheck="false"
autocorrect="off"
autocapitalize="off"
@keydown.enter.prevent="emit('send')"
/>
<input
:ref="bindShippingPickerRef"
type="file"
class="hidden"
accept=".xlsx,.xls,.csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,text/csv"
@change="emit('shipping-inventory-selected', $event)"
/>
<input
ref="filePickerRef"
type="file"
multiple
class="hidden"
:accept="attachmentPolicy.accept || undefined"
@change="emit('local-files-selected', $event)"
/>
<div v-if="attachments.length > 0" class="flex flex-wrap gap-2 px-3 pb-1">
<div
v-for="a in attachments"
:key="a.id"
class="flex items-center gap-2 rounded-lg border border-default bg-surface-soft px-2 py-1 text-xs text-secondary"
>
<span class="max-w-[220px] truncate">
{{ a.kind === 'local' ? a.file.name : a.url }}
</span>
<button type="button" class="text-muted hover:text-red-500" @click="emit('remove-attachment', a.id)">
<X class="h-3.5 w-3.5" />
</button>
</div>
</div>
<div
v-if="isShippingNoticeAgent && attachments.length > 0"
class="flex flex-wrap items-center gap-2 px-3 pb-1 text-xs text-muted"
>
<button
type="button"
class="rounded-full border border-default bg-surface-soft px-3 py-1 font-medium text-secondary transition-colors hover:bg-surface-soft/70"
@click="emit('use-shipping-suggested-prompt')"
>
{{ shippingSuggestedPrompt || '推荐问题' }}
</button>
<span>点发送开始处理</span>
</div>
<div class="mt-2 flex items-center justify-between px-2 pb-1">
<div class="flex items-center gap-3">
<button
type="button"
:disabled="isThinking || isVoiceTranscribing"
:title="isVoiceRecording ? $t('chat.voiceStop') : isVoiceTranscribing ? $t('chat.voiceTranscribing') : $t('chat.voiceFeature')"
class="transition-colors disabled:cursor-not-allowed disabled:opacity-50"
:class="isVoiceRecording ? 'animate-pulse text-red-500' : isVoiceTranscribing ? 'text-brand-500' : 'text-muted hover:text-brand-600'"
@click="emit('voice-click')"
>
<Loader2 v-if="isVoiceTranscribing" class="h-5 w-5 animate-spin" />
<Mic v-else class="h-5 w-5" />
</button>
<div v-if="attachmentPolicy.anyEnabled" ref="attachmentMenuRef" class="relative">
<button
type="button"
:disabled="isThinking || isUploadingAttachments"
title="附件"
class="flex h-9 w-9 items-center justify-center rounded-lg text-muted transition-colors hover:text-brand-600 disabled:cursor-not-allowed disabled:opacity-50"
@click="toggleAttachmentMenu"
>
<Paperclip class="h-5 w-5" />
</button>
<div
v-if="showAttachmentMenu"
class="absolute bottom-full left-0 mb-2 w-72 animate-fade-in overflow-hidden rounded-xl border border-subtle bg-surface py-1 shadow-xl"
>
<button
v-if="attachmentPolicy.canLocal"
type="button"
class="flex w-full items-center gap-2 px-3 py-2 text-xs text-secondary transition-colors hover:bg-surface-soft"
@click="selectAttachmentLocal"
>
<Paperclip class="h-4 w-4 text-brand-500" />
上传本地文件
</button>
<button
v-if="attachmentPolicy.canRemote"
type="button"
class="flex w-full items-center gap-2 px-3 py-2 text-xs text-secondary transition-colors hover:bg-surface-soft"
@click="selectAttachmentUrl"
>
<Link2 class="h-4 w-4 text-brand-500" />
粘贴 URL
</button>
<div v-if="showAttachmentUrlPanel" class="px-3 py-2">
<div class="flex items-center gap-2">
<input
ref="attachmentUrlInputRef"
v-model="attachmentUrlInput"
:disabled="isThinking || isUploadingAttachments"
placeholder="粘贴附件 URL(http/https)"
class="flex-1 rounded-lg border border-default bg-surface-soft px-3 py-2 text-xs text-secondary placeholder-slate-400 focus:outline-none focus:ring-0"
@keydown.enter.prevent="submitRemoteUrl"
/>
<button
type="button"
:disabled="isThinking || isUploadingAttachments"
class="flex h-9 w-9 items-center justify-center rounded-lg border border-default text-muted transition-colors hover:bg-surface-soft disabled:cursor-not-allowed disabled:opacity-50"
title="添加"
@click="submitRemoteUrl"
>
<Check class="h-4 w-4" />
</button>
<button
type="button"
:disabled="isThinking || isUploadingAttachments"
class="flex h-9 w-9 items-center justify-center rounded-lg border border-default text-muted transition-colors hover:bg-surface-soft disabled:cursor-not-allowed disabled:opacity-50"
title="取消"
@click="closeAttachmentMenu"
>
<X class="h-4 w-4" />
</button>
</div>
</div>
</div>
</div>
<div ref="kbMenuRef" class="relative">
<button
type="button"
:disabled="kbDropdownDisabled"
class="flex items-center gap-1 rounded-lg border border-default px-3 py-1.5 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft disabled:cursor-not-allowed disabled:opacity-60"
@click="toggleKbDropdown"
>
<Database class="h-3.5 w-3.5 text-brand-500" />
{{ getKbLabel(selectedKb) }} <ChevronDown class="h-3 w-3" />
</button>
<div
v-if="showKbDropdown && !kbDropdownDisabled"
class="absolute bottom-full left-0 mb-2 w-36 animate-fade-in overflow-hidden rounded-xl border border-subtle bg-surface py-1 shadow-xl"
>
<button
v-for="option in availableKbOptions"
:key="option"
type="button"
class="flex w-full items-center justify-between px-3 py-2 text-xs text-secondary transition-colors hover:bg-surface-soft"
:class="{ 'font-bold text-brand-700': selectedKb === option }"
@click="selectKb(option)"
>
{{ getKbLabel(option) }}
<Check v-if="selectedKb === option" class="h-3.5 w-3.5" />
</button>
</div>
</div>
<button
type="button"
:disabled="isThinking"
class="flex items-center gap-1 rounded-lg border border-default px-3 py-1.5 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft disabled:cursor-not-allowed disabled:opacity-50"
@click="emit('toggle-browser')"
>
<Globe class="h-3.5 w-3.5 text-brand-500" />
{{ browserPanelOpen ? $t('chat.collapseBrowser') : $t('chat.openBrowser') }}
</button>
</div>
<button
type="button"
:disabled="!canSendMessage"
class="flex h-10 w-10 items-center justify-center rounded-full border shadow-sm transition-all duration-200 disabled:cursor-not-allowed disabled:opacity-50"
:class="!canSendMessage
? 'border-default bg-slate-100 text-muted'
: 'border-brand-500 bg-brand-500 text-white shadow-[0_0_0_4px_rgba(59,130,246,0.18)] hover:scale-105 hover:bg-brand-600 hover:shadow-[0_0_0_6px_rgba(59,130,246,0.24)] active:scale-95'"
@click="emit('send')"
>
<Loader2 v-if="isUploadingAttachments" class="h-4 w-4 animate-spin" />
<Send v-else class="h-4 w-4" />
</button>
</div>
</div>
<div class="mt-3 text-center text-[11px] text-muted">
{{ $t('chat.disclaimer') }}
</div>
</div>
</footer>
</template>