feat:【antd】【ai】chat/manager 的代码优化

This commit is contained in:
YunaiV
2025-10-26 17:56:47 +08:00
parent 86cae454d1
commit 02b6ab1886
4 changed files with 40 additions and 48 deletions

View File

@@ -1,11 +1,16 @@
import type { VbenFormSchema } from '#/adapter/form'; import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table'; import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemUserApi } from '#/api/system/user';
import { DICT_TYPE } from '@vben/constants'; import { DICT_TYPE } from '@vben/constants';
import { getSimpleUserList } from '#/api/system/user'; import { getSimpleUserList } from '#/api/system/user';
import { getRangePickerDefaultProps } from '#/utils'; import { getRangePickerDefaultProps } from '#/utils';
/** 关联数据 */
let userList: SystemUserApi.User[] = [];
getSimpleUserList().then((data) => (userList = data));
/** 列表的搜索表单 */ /** 列表的搜索表单 */
export function useGridFormSchemaConversation(): VbenFormSchema[] { export function useGridFormSchemaConversation(): VbenFormSchema[] {
return [ return [
@@ -13,11 +18,19 @@ export function useGridFormSchemaConversation(): VbenFormSchema[] {
fieldName: 'userId', fieldName: 'userId',
label: '用户编号', label: '用户编号',
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入用户编号',
allowClear: true,
},
}, },
{ {
fieldName: 'title', fieldName: 'title',
label: '聊天标题', label: '聊天标题',
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入聊天标题',
allowClear: true,
},
}, },
{ {
fieldName: 'createTime', fieldName: 'createTime',
@@ -49,7 +62,13 @@ export function useGridColumnsConversation(): VxeTableGridOptions['columns'] {
{ {
title: '用户', title: '用户',
minWidth: 180, minWidth: 180,
slots: { default: 'userId' }, field: 'userId',
formatter: ({ cellValue }) => {
if (cellValue === 0) {
return '系统';
}
return userList.find((user) => user.id === cellValue)?.nickname || '-';
},
}, },
{ {
field: 'roleName', field: 'roleName',
@@ -103,6 +122,10 @@ export function useGridFormSchemaMessage(): VbenFormSchema[] {
fieldName: 'conversationId', fieldName: 'conversationId',
label: '对话编号', label: '对话编号',
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入对话编号',
allowClear: true,
},
}, },
{ {
fieldName: 'userId', fieldName: 'userId',
@@ -112,6 +135,8 @@ export function useGridFormSchemaMessage(): VbenFormSchema[] {
api: getSimpleUserList, api: getSimpleUserList,
labelField: 'nickname', labelField: 'nickname',
valueField: 'id', valueField: 'id',
placeholder: '请选择用户编号',
allowClear: true,
}, },
}, },
{ {
@@ -144,7 +169,9 @@ export function useGridColumnsMessage(): VxeTableGridOptions['columns'] {
{ {
title: '用户', title: '用户',
minWidth: 180, minWidth: 180,
slots: { default: 'userId' }, field: 'userId',
formatter: ({ cellValue }) =>
userList.find((user) => user.id === cellValue)?.nickname || '-',
}, },
{ {
field: 'roleName', field: 'roleName',

View File

@@ -5,8 +5,8 @@ import { DocAlert, Page } from '@vben/common-ui';
import { Card, Tabs } from 'ant-design-vue'; import { Card, Tabs } from 'ant-design-vue';
import ChatConversationList from './modules/ChatConversationList.vue'; import ChatConversationList from './modules/conversation-list.vue';
import ChatMessageList from './modules/ChatMessageList.vue'; import ChatMessageList from './modules/message-list.vue';
const activeTabName = ref('conversation'); const activeTabName = ref('conversation');
</script> </script>
@@ -16,6 +16,7 @@ const activeTabName = ref('conversation');
<template #doc> <template #doc>
<DocAlert title="AI 对话聊天" url="https://doc.iocoder.cn/ai/chat/" /> <DocAlert title="AI 对话聊天" url="https://doc.iocoder.cn/ai/chat/" />
</template> </template>
<Card> <Card>
<Tabs v-model:active-key="activeTabName"> <Tabs v-model:active-key="activeTabName">
<Tabs.TabPane tab="对话列表" key="conversation"> <Tabs.TabPane tab="对话列表" key="conversation">

View File

@@ -1,9 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table'; import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiChatConversationApi } from '#/api/ai/chat/conversation'; import type { AiChatConversationApi } from '#/api/ai/chat/conversation';
import type { SystemUserApi } from '#/api/system/user';
import { onMounted, ref } from 'vue';
import { Page } from '@vben/common-ui'; import { Page } from '@vben/common-ui';
@@ -14,7 +11,6 @@ import {
deleteChatConversationByAdmin, deleteChatConversationByAdmin,
getChatConversationPage, getChatConversationPage,
} from '#/api/ai/chat/conversation'; } from '#/api/ai/chat/conversation';
import { getSimpleUserList } from '#/api/system/user';
import { $t } from '#/locales'; import { $t } from '#/locales';
import { import {
@@ -22,23 +18,20 @@ import {
useGridFormSchemaConversation, useGridFormSchemaConversation,
} from '../data'; } from '../data';
const userList = ref<SystemUserApi.User[]>([]); //
/** 刷新表格 */ /** 刷新表格 */
function handleRefresh() { function handleRefresh() {
gridApi.query(); gridApi.query();
} }
/** 删除 */ /** 删除消息 */
async function handleDelete(row: AiChatConversationApi.ChatConversation) { async function handleDelete(row: AiChatConversationApi.ChatConversation) {
const hideLoading = message.loading({ const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.id]), content: $t('ui.actionMessage.deleting', [row.id]),
duration: 0, duration: 0,
}); });
try { try {
await deleteChatConversationByAdmin(row.id as number); await deleteChatConversationByAdmin(row.id!);
message.success({ message.success($t('ui.actionMessage.deleteSuccess', [row.id]));
content: $t('ui.actionMessage.deleteSuccess', [row.id]),
});
handleRefresh(); handleRefresh();
} finally { } finally {
hideLoading(); hideLoading();
@@ -66,17 +59,13 @@ const [Grid, gridApi] = useVbenVxeGrid({
}, },
rowConfig: { rowConfig: {
keyField: 'id', keyField: 'id',
isHover: true,
}, },
toolbarConfig: { toolbarConfig: {
refresh: true, refresh: true,
search: true, search: true,
}, },
} as VxeTableGridOptions<AiChatConversationApi.ChatConversation>, } as VxeTableGridOptions<AiChatConversationApi.ChatConversation>,
separator: false,
});
onMounted(async () => {
//
userList.value = await getSimpleUserList();
}); });
</script> </script>
@@ -86,12 +75,6 @@ onMounted(async () => {
<template #toolbar-tools> <template #toolbar-tools>
<TableAction :actions="[]" /> <TableAction :actions="[]" />
</template> </template>
<template #userId="{ row }">
<span>
{{ userList.find((item) => item.id === row.userId)?.nickname }}
</span>
<span v-if="row.userId === 0">系统</span>
</template>
<template #actions="{ row }"> <template #actions="{ row }">
<TableAction <TableAction
:actions="[ :actions="[

View File

@@ -1,9 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table'; import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiChatConversationApi } from '#/api/ai/chat/conversation'; import type { AiChatConversationApi } from '#/api/ai/chat/conversation';
import type { SystemUserApi } from '#/api/system/user';
import { onMounted, ref } from 'vue';
import { Page } from '@vben/common-ui'; import { Page } from '@vben/common-ui';
@@ -14,28 +11,24 @@ import {
deleteChatMessageByAdmin, deleteChatMessageByAdmin,
getChatMessagePage, getChatMessagePage,
} from '#/api/ai/chat/message'; } from '#/api/ai/chat/message';
import { getSimpleUserList } from '#/api/system/user';
import { $t } from '#/locales'; import { $t } from '#/locales';
import { useGridColumnsMessage, useGridFormSchemaMessage } from '../data'; import { useGridColumnsMessage, useGridFormSchemaMessage } from '../data';
const userList = ref<SystemUserApi.User[]>([]); //
/** 刷新表格 */ /** 刷新表格 */
function handleRefresh() { function handleRefresh() {
gridApi.query(); gridApi.query();
} }
/** 删除 */ /** 删除消息 */
async function handleDelete(row: AiChatConversationApi.ChatConversation) { async function handleDelete(row: AiChatConversationApi.ChatConversation) {
const hideLoading = message.loading({ const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.id]), content: $t('ui.actionMessage.deleting', [row.id]),
duration: 0, duration: 0,
}); });
try { try {
await deleteChatMessageByAdmin(row.id as number); await deleteChatMessageByAdmin(row.id!);
message.success({ message.success($t('ui.actionMessage.deleteSuccess', [row.id]));
content: $t('ui.actionMessage.deleteSuccess', [row.id]),
});
handleRefresh(); handleRefresh();
} finally { } finally {
hideLoading(); hideLoading();
@@ -63,31 +56,19 @@ const [Grid, gridApi] = useVbenVxeGrid({
}, },
rowConfig: { rowConfig: {
keyField: 'id', keyField: 'id',
isHover: true,
}, },
toolbarConfig: { toolbarConfig: {
refresh: true, refresh: true,
search: true, search: true,
}, },
} as VxeTableGridOptions<AiChatConversationApi.ChatConversation>, } as VxeTableGridOptions<AiChatConversationApi.ChatConversation>,
separator: false,
});
onMounted(async () => {
//
userList.value = await getSimpleUserList();
}); });
</script> </script>
<template> <template>
<Page auto-content-height> <Page auto-content-height>
<Grid table-title="消息列表"> <Grid table-title="消息列表">
<template #toolbar-tools>
<TableAction :actions="[]" />
</template>
<template #userId="{ row }">
<span>
{{ userList.find((item) => item.id === row.userId)?.nickname }}
</span>
</template>
<template #actions="{ row }"> <template #actions="{ row }">
<TableAction <TableAction
:actions="[ :actions="[