diff --git a/apps/web-antd/src/views/ai/write/index/index.vue b/apps/web-antd/src/views/ai/write/index/index.vue index b2355af0e..2c2a3dea8 100644 --- a/apps/web-antd/src/views/ai/write/index/index.vue +++ b/apps/web-antd/src/views/ai/write/index/index.vue @@ -3,28 +3,30 @@ import type { AiWriteApi } from '#/api/ai/write'; import { nextTick, ref } from 'vue'; -import { alert, Page } from '@vben/common-ui'; +import { Page } from '@vben/common-ui'; import { WriteExample } from '@vben/constants'; +import { message } from 'ant-design-vue'; + import { writeStream } from '#/api/ai/write'; -import Left from './components/Left.vue'; -import Right from './components/Right.vue'; +import Left from './modules/left.vue'; +import Right from './modules/right.vue'; const writeResult = ref(''); // 写作结果 const isWriting = ref(false); // 是否正在写作中 const abortController = ref(); // // 写作进行中 abort 控制器(控制 stream 写作) +const rightRef = ref>(); // 写作面板 + /** 停止 stream 生成 */ -function stopStream() { +function handleStopStream() { abortController.value?.abort(); isWriting.value = false; } -/** 执行写作 */ -const rightRef = ref>(); - -function submit(data: Partial) { +/** 提交写作 */ +function handleSubmit(data: Partial) { abortController.value = new AbortController(); writeResult.value = ''; isWriting.value = true; @@ -33,8 +35,8 @@ function submit(data: Partial) { onMessage: async (res: any) => { const { code, data, msg } = JSON.parse(res.data); if (code !== 0) { - alert(`写作异常! ${msg}`); - stopStream(); + message.error(`写作异常! ${msg}`); + handleStopStream(); return; } writeResult.value = writeResult.value + data; @@ -43,10 +45,10 @@ function submit(data: Partial) { rightRef.value?.scrollToBottom(); }, ctrl: abortController.value, - onClose: stopStream, + onClose: handleStopStream, onError: (error: any) => { console.error('写作异常', error); - stopStream(); + handleStopStream(); // 需要抛出异常,禁止重试 throw error; }, @@ -59,7 +61,7 @@ function handleExampleClick(type: keyof typeof WriteExample) { } /** 点击重置的时候清空写作的结果*/ -function reset() { +function handleReset() { writeResult.value = ''; } @@ -70,13 +72,13 @@ function reset() { -// TODO @gjd:应该是 modules 模块,然后小写 import type { AiWriteApi } from '#/api/ai/write'; import { ref } from 'vue'; @@ -11,7 +10,7 @@ import { IconifyIcon } from '@vben/icons'; import { createReusableTemplate } from '@vueuse/core'; import { Button, message, Textarea } from 'ant-design-vue'; -import Tag from './Tag.vue'; +import Tag from './tag.vue'; type TabType = AiWriteApi.Write['type']; @@ -34,6 +33,7 @@ function omit(obj: Record, keysToOmit: string[]) { } return result; } + /** 点击示例的时候,将定义好的文章作为示例展示出来 */ function example(type: 'reply' | 'write') { formData.value = { @@ -79,13 +79,11 @@ const initData: AiWriteApi.Write = { length: 1, format: 1, }; - const formData = ref({ ...initData }); +const recordFormData = {} as Record; // 用来记录切换之前所填写的数据,切换的时候给赋值回来 -/** 用来记录切换之前所填写的数据,切换的时候给赋值回来 */ -const recordFormData = {} as Record; -/** 切换tab */ -function switchTab(value: TabType) { +/** 切换 tab */ +function handleSwitchTab(value: TabType) { if (value !== selectedTab.value) { // 保存之前的久数据 recordFormData[selectedTab.value] = formData.value; @@ -96,8 +94,11 @@ function switchTab(value: TabType) { } /** 提交写作 */ -function submit() { - if (selectedTab.value === 2 && !formData.value.originalContent) { +function handleSubmit() { + if ( + selectedTab.value === AiWriteTypeEnum.REPLY && + !formData.value.originalContent + ) { message.warning('请输入原文'); return; } @@ -105,12 +106,13 @@ function submit() { message.warning(`请输入${selectedTab.value === 1 ? '写作' : '回复'}内容`); return; } + emit('submit', { - /** 撰写的时候没有 originalContent 字段*/ + // 撰写的时候没有 originalContent 字段 ...(selectedTab.value === 1 ? omit(formData.value, ['originalContent']) : formData.value), - /** 使用选中 tab 值覆盖当前的 type 类型 */ + // 使用选中 tab 值覆盖当前的 type 类型 type: selectedTab.value, }); } @@ -156,7 +158,7 @@ function submit() { v-for="tab in tabs" :key="tab.value" :active="tab.value === selectedTab" - :item-click="() => switchTab(tab.value)" + :item-click="() => handleSwitchTab(tab.value)" :text="tab.text" class="relative z-20" /> @@ -167,7 +169,7 @@ function submit() { class="bg-card box-border h-full w-96 flex-grow overflow-y-auto px-7 pb-2 lg:block" >
-