fix: 调用会话接口
This commit is contained in:
parent
ecc604ec6d
commit
11ce542b5d
@ -1 +1,2 @@
|
||||
export * from './auth';
|
||||
export * from './session';
|
||||
|
||||
14
src/api/session/index.ts
Normal file
14
src/api/session/index.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import type {
|
||||
ChatSessionVo,
|
||||
CreateSessionDTO,
|
||||
GetSessionListParams,
|
||||
} from './types';
|
||||
import { get, post } from '@/utils/request';
|
||||
|
||||
export function getSessionList(params: GetSessionListParams) {
|
||||
return get<ChatSessionVo[]>('/system/session/list', params);
|
||||
}
|
||||
|
||||
export function createSession(data: CreateSessionDTO) {
|
||||
return post<null>('/system/session', data);
|
||||
}
|
||||
138
src/api/session/types.ts
Normal file
138
src/api/session/types.ts
Normal file
@ -0,0 +1,138 @@
|
||||
export interface GetSessionListParams {
|
||||
/**
|
||||
* 创建者
|
||||
*/
|
||||
createBy?: number;
|
||||
/**
|
||||
* 创建部门
|
||||
*/
|
||||
createDept?: number;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
createTime?: Date;
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
id?: number;
|
||||
/**
|
||||
* 排序的方向desc或者asc
|
||||
*/
|
||||
isAsc?: string;
|
||||
/**
|
||||
* 排序列
|
||||
*/
|
||||
orderByColumn?: string;
|
||||
/**
|
||||
* 当前页数
|
||||
*/
|
||||
pageNum?: number;
|
||||
/**
|
||||
* 分页大小
|
||||
*/
|
||||
pageSize?: number;
|
||||
/**
|
||||
* 请求参数
|
||||
*/
|
||||
params?: { [key: string]: { [key: string]: any } };
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
remark?: string;
|
||||
/**
|
||||
* 会话内容
|
||||
*/
|
||||
sessionContent?: string;
|
||||
/**
|
||||
* 会话标题
|
||||
*/
|
||||
sessionTitle?: string;
|
||||
/**
|
||||
* 更新者
|
||||
*/
|
||||
updateBy?: number;
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
updateTime?: Date;
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
userId: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* ChatSessionVo,会话管理视图对象 chat_session
|
||||
*/
|
||||
export interface ChatSessionVo {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
id?: number;
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
remark?: string;
|
||||
/**
|
||||
* 会话内容
|
||||
*/
|
||||
sessionContent?: string;
|
||||
/**
|
||||
* 会话标题
|
||||
*/
|
||||
sessionTitle?: string;
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
userId?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* ChatSessionBo,会话管理业务对象 chat_session
|
||||
*/
|
||||
export interface CreateSessionDTO {
|
||||
/**
|
||||
* 创建者
|
||||
*/
|
||||
createBy?: number;
|
||||
/**
|
||||
* 创建部门
|
||||
*/
|
||||
createDept?: number;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
createTime?: Date;
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
id?: number;
|
||||
/**
|
||||
* 请求参数
|
||||
*/
|
||||
params?: { [key: string]: { [key: string]: any } };
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
remark: string;
|
||||
/**
|
||||
* 会话内容
|
||||
*/
|
||||
sessionContent: string;
|
||||
/**
|
||||
* 会话标题
|
||||
*/
|
||||
sessionTitle: string;
|
||||
/**
|
||||
* 更新者
|
||||
*/
|
||||
updateBy?: number;
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
updateTime?: Date;
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
userId: number;
|
||||
}
|
||||
@ -1,66 +1,76 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ChatSessionVo } from '@/api/session/types';
|
||||
import type { ConversationItem } from 'vue-element-plus-x/types/Conversations';
|
||||
import { getSessionList } from '@/api';
|
||||
import { useUserStore } from '@/store';
|
||||
import { Conversations } from 'vue-element-plus-x';
|
||||
import { useRoute, useRouter } from 'vue-router';
|
||||
|
||||
interface ChatItem {
|
||||
key: string;
|
||||
}
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const userStore = useUserStore();
|
||||
const active = computed<string>(() => (route.params?.id as string) ?? '');
|
||||
const items = ref<ConversationItem<ChatItem>[]>([
|
||||
{
|
||||
key: '1',
|
||||
label: '今天的会话111111111111111111111111111',
|
||||
group: 'today',
|
||||
disabled: true,
|
||||
},
|
||||
{
|
||||
key: '2',
|
||||
group: 'today',
|
||||
label: '今天的会话2',
|
||||
},
|
||||
{
|
||||
key: '3',
|
||||
group: 'yesterday',
|
||||
label: '昨天的会话1',
|
||||
},
|
||||
{
|
||||
key: '4',
|
||||
label: '昨天的会话2',
|
||||
},
|
||||
{
|
||||
key: '5',
|
||||
label: '一周前的会话',
|
||||
},
|
||||
{
|
||||
key: '6',
|
||||
label: '一个月前的会话',
|
||||
},
|
||||
{
|
||||
key: '7',
|
||||
label: '很久以前的会话',
|
||||
},
|
||||
]);
|
||||
const items = ref<ConversationItem<ChatSessionVo>[]>([]);
|
||||
|
||||
function handleChange(item: ConversationItem<ChatItem>) {
|
||||
function handleChange(item: ConversationItem<ChatSessionVo>) {
|
||||
console.log(item);
|
||||
router.replace({
|
||||
name: 'chat',
|
||||
params: {
|
||||
id: item.key,
|
||||
id: item.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function getSessions() {
|
||||
try {
|
||||
const res = await getSessionList({
|
||||
userId: userStore.userInfo?.userId as number,
|
||||
});
|
||||
console.log(res);
|
||||
items.value
|
||||
= res.rows?.map(item => ({
|
||||
...item,
|
||||
label: item.sessionTitle as string,
|
||||
})) ?? [];
|
||||
}
|
||||
catch (error) {
|
||||
console.error('getSessions:', error);
|
||||
}
|
||||
}
|
||||
getSessions();
|
||||
|
||||
const sessionId = computed<string>(() => route.params?.id as string);
|
||||
function handleNewSession() {
|
||||
if (sessionId.value) {
|
||||
router.replace({ name: 'chatWithoutId' });
|
||||
}
|
||||
}
|
||||
|
||||
watchEffect(() => {
|
||||
console.log('active', active.value, '>>>');
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-container class="h-screen overflow-hidden">
|
||||
<el-aside>
|
||||
<el-button>新增</el-button>
|
||||
<Conversations :active="active" :items="items" @change="handleChange" />
|
||||
<Conversations
|
||||
:active="active"
|
||||
:items="items"
|
||||
row-key="id"
|
||||
label-key="sessionTitle"
|
||||
@change="handleChange"
|
||||
>
|
||||
<template #header>
|
||||
<el-button @click="handleNewSession">
|
||||
新增
|
||||
</el-button>
|
||||
</template>
|
||||
<template #footer>
|
||||
<div>用户信息</div>
|
||||
</template>
|
||||
</Conversations>
|
||||
</el-aside>
|
||||
<el-main>
|
||||
<RouterView />
|
||||
|
||||
@ -1,11 +1,33 @@
|
||||
<script setup lang="ts">
|
||||
import { useRoute } from 'vue-router';
|
||||
import { createSession } from '@/api';
|
||||
import { useUserStore } from '@/store';
|
||||
import { Sender } from 'vue-element-plus-x';
|
||||
import { useRoute, useRouter } from 'vue-router';
|
||||
|
||||
const route = useRoute();
|
||||
console.log(route.params, '>>>>>>');
|
||||
const _router = useRouter();
|
||||
console.log(route.params, '>>>>>>', route.query);
|
||||
const chatId = computed(() => route.params?.id);
|
||||
const senderValue = ref('');
|
||||
const userStore = useUserStore();
|
||||
async function handleSend() {
|
||||
console.log('chatId', chatId.value);
|
||||
console.log('value', senderValue.value);
|
||||
if (!chatId.value) {
|
||||
await createSession({
|
||||
userId: userStore.userInfo?.userId as number,
|
||||
sessionContent: senderValue.value,
|
||||
sessionTitle: senderValue.value.slice(0, 10),
|
||||
remark: senderValue.value.slice(0, 10),
|
||||
});
|
||||
// 跳转到会话页面(传入query参数-sendValue)
|
||||
// router.replace(`${}`)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>{{ chatId }}</div>
|
||||
<div>
|
||||
<Sender v-model="senderValue" @submit="handleSend" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@ -1,15 +1,17 @@
|
||||
import type { HookFetchPlugin } from 'hook-fetch';
|
||||
import { useUserStore } from '@/store';
|
||||
import { ElMessage } from 'element-plus';
|
||||
import hookFetch from 'hook-fetch';
|
||||
import { sseTextDecoderPlugin } from 'hook-fetch/plugins';
|
||||
|
||||
interface BaseResponse {
|
||||
code: number;
|
||||
data: null;
|
||||
data: never;
|
||||
msg: string;
|
||||
rows: never;
|
||||
}
|
||||
|
||||
export const request = hookFetch.create<BaseResponse, 'data'>({
|
||||
export const request = hookFetch.create<BaseResponse, 'data' | 'rows'>({
|
||||
baseURL: 'https://web.pandarobot.chat/api',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@ -18,8 +20,14 @@ export const request = hookFetch.create<BaseResponse, 'data'>({
|
||||
});
|
||||
|
||||
function jwtPlugin(): HookFetchPlugin<BaseResponse> {
|
||||
const userStore = useUserStore();
|
||||
return {
|
||||
name: 'jwt',
|
||||
beforeRequest: async (config) => {
|
||||
config.headers = new Headers(config.headers);
|
||||
config.headers.set('authorization', `Bearer ${userStore.token}`);
|
||||
return config;
|
||||
},
|
||||
afterResponse: async (response) => {
|
||||
console.log(response);
|
||||
if (response.result?.code === 200) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user