mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-15 22:46:49 +00:00
first commit
This commit is contained in:
51
webapp/packages/chat-sdk/src/service/axiosInstance.ts
Normal file
51
webapp/packages/chat-sdk/src/service/axiosInstance.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
// 引入axios库
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
import { getToken } from '../utils/utils';
|
||||
|
||||
// 创建axios实例
|
||||
const axiosInstance: AxiosInstance = axios.create({
|
||||
// 设置基本URL,所有请求都会使用这个URL作为前缀
|
||||
baseURL: '',
|
||||
// 设置请求超时时间(毫秒)
|
||||
timeout: 30000,
|
||||
// 设置请求头
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// 请求拦截器
|
||||
axiosInstance.interceptors.request.use(
|
||||
(config: any) => {
|
||||
const token = getToken();
|
||||
if (token && config?.headers) {
|
||||
config.headers.auth = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
// 请求错误时的处理
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 响应拦截器
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response: any) => {
|
||||
if (Number(response.data.code) === 403) {
|
||||
window.location.href = '/#/login';
|
||||
return response;
|
||||
}
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
// 对响应错误进行处理
|
||||
if (error.response && error.response.status === 401) {
|
||||
// 如果响应状态码为401,表示未授权,可以在这里处理重新登录等操作
|
||||
console.log('Unauthorized, please log in again.');
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default axiosInstance;
|
||||
70
webapp/packages/chat-sdk/src/service/index.ts
Normal file
70
webapp/packages/chat-sdk/src/service/index.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import axios from './axiosInstance';
|
||||
import { ChatContextType, HistoryType, MsgDataType, SearchRecommendItem } from '../common/type';
|
||||
import { QueryDataType } from '../common/type';
|
||||
|
||||
const DEFAULT_CHAT_ID = 0;
|
||||
|
||||
const prefix = '/api';
|
||||
|
||||
export function searchRecommend(queryText: string, chatId?: number, domainId?: number) {
|
||||
return axios.post<Result<SearchRecommendItem[]>>(`${prefix}/chat/query/search`, {
|
||||
queryText,
|
||||
chatId: chatId || DEFAULT_CHAT_ID,
|
||||
domainId,
|
||||
});
|
||||
}
|
||||
|
||||
export function chatQuery(queryText: string, chatId?: number, domainId?: number, isSaveQuestionAnswer?: boolean) {
|
||||
return axios.post<Result<MsgDataType>>(`${prefix}/chat/query/query`, {
|
||||
queryText,
|
||||
chatId: chatId || DEFAULT_CHAT_ID,
|
||||
domainId,
|
||||
isSaveQuestionAnswer
|
||||
});
|
||||
}
|
||||
|
||||
export function queryData(chatContext: ChatContextType) {
|
||||
return axios.post<Result<QueryDataType>>(`${prefix}/chat/query/queryData`, chatContext);
|
||||
}
|
||||
|
||||
export function queryContext(queryText: string, chatId?: number) {
|
||||
return axios.post<Result<ChatContextType>>(`${prefix}/chat/query/queryContext`, {
|
||||
queryText,
|
||||
chatId: chatId || DEFAULT_CHAT_ID,
|
||||
});
|
||||
}
|
||||
|
||||
export function querySuggestionInfo(domainId: number) {
|
||||
return axios.get<Result<any>>(`${prefix}/chat/recommend/${domainId}`);
|
||||
}
|
||||
|
||||
export function getHistoryMsg(current: number, chatId: number = DEFAULT_CHAT_ID, pageSize: number = 10) {
|
||||
return axios.post<Result<HistoryType>>(`${prefix}/chat/manage/pageQueryInfo?chatId=${chatId}`, {
|
||||
current,
|
||||
pageSize,
|
||||
});
|
||||
}
|
||||
|
||||
export function queryMetricInfo(data: any) {
|
||||
return axios.get(`/semantic/metric/getMetric/${data.classId}/${data.uniqueId}`);
|
||||
}
|
||||
|
||||
export function getRelatedDimensionFromStatInfo(data: any) {
|
||||
return axios.get(
|
||||
`/semantic/metric/getRelatedDimensionFromStatInfo/${data.classId}/${data.uniqueId}`,
|
||||
);
|
||||
}
|
||||
|
||||
export function getMetricQueryInfo(data: any) {
|
||||
return axios.get<any>(
|
||||
`getMetricQueryInfo/${data.classId}/${data.metricName}`
|
||||
);
|
||||
}
|
||||
|
||||
export function saveConversation(chatName: string) {
|
||||
return axios.post<Result<any>>(`${prefix}/chat/manage/save?chatName=${chatName}`);
|
||||
}
|
||||
|
||||
export function getAllConversations() {
|
||||
return axios.get<Result<any>>(`${prefix}/chat/manage/getAll`);
|
||||
}
|
||||
Reference in New Issue
Block a user