首次
This commit is contained in:
554
src/utils/api.js
Normal file
554
src/utils/api.js
Normal file
@@ -0,0 +1,554 @@
|
||||
// API服务文件
|
||||
import { useUserStore } from '@/stores/user.js';
|
||||
|
||||
// 基础配置
|
||||
const BASE_URL = 'http://8.145.52.111:8091'; // 根据后端地址调整
|
||||
|
||||
// 检查用户登录状态
|
||||
const checkLoginStatus = () => {
|
||||
const userStore = useUserStore();
|
||||
const customToken = uni.getStorageSync('custom_token');
|
||||
const userToken = uni.getStorageSync('user_token');
|
||||
const userInfo = uni.getStorageSync('userInfo');
|
||||
|
||||
// 解析userInfo,检查是否有有效的token
|
||||
let userInfoToken = '';
|
||||
if (userInfo) {
|
||||
try {
|
||||
const parsedUserInfo = JSON.parse(userInfo);
|
||||
userInfoToken = parsedUserInfo.token || '';
|
||||
} catch (e) {
|
||||
console.warn('解析userInfo失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 只有存在有效token才认为已登录
|
||||
const hasValidToken = !!(userStore.token || customToken || userToken || userInfoToken);
|
||||
|
||||
return {
|
||||
isLoggedIn: hasValidToken,
|
||||
token: userStore.token || customToken || userToken || userInfoToken
|
||||
};
|
||||
};
|
||||
|
||||
// 请求拦截器
|
||||
const request = (options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const userStore = useUserStore();
|
||||
const loginStatus = checkLoginStatus();
|
||||
|
||||
// 默认配置
|
||||
const defaultOptions = {
|
||||
url: BASE_URL + options.url,
|
||||
method: options.method || 'GET',
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': loginStatus.token || '',
|
||||
...options.header
|
||||
},
|
||||
data: options.data || {},
|
||||
timeout: options.timeout || 30000
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
uni.request({
|
||||
...defaultOptions,
|
||||
success: (res) => {
|
||||
console.log('API请求成功:', res);
|
||||
console.log('响应状态码:', res.statusCode);
|
||||
console.log('响应数据:', res.data);
|
||||
|
||||
// 处理响应
|
||||
if (res.statusCode === 200) {
|
||||
resolve(res.data);
|
||||
} else {
|
||||
reject({
|
||||
code: res.statusCode,
|
||||
message: res.data?.message || '请求失败',
|
||||
data: res.data
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('API请求失败:', err);
|
||||
reject({
|
||||
code: -1,
|
||||
message: '网络请求失败',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// AI聊天API
|
||||
export const chatAPI = {
|
||||
// 同步聊天接口
|
||||
syncChat: async (params) => {
|
||||
const loginStatus = checkLoginStatus();
|
||||
|
||||
// 如果用户未登录,直接返回失败,让前端使用本地模拟
|
||||
if (!loginStatus.isLoggedIn) {
|
||||
console.log('用户未登录,跳过API调用,使用本地模拟');
|
||||
return {
|
||||
success: false,
|
||||
error: { message: '用户未登录,使用本地模拟回复' },
|
||||
isAnonymous: true
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await request({
|
||||
url: '/api/chat/sync',
|
||||
method: 'POST',
|
||||
data: {
|
||||
message: params.message,
|
||||
useFunctionCall: false,
|
||||
modelId: null, // 使用默认模型
|
||||
templateId: params.characterId // 使用角色模板ID
|
||||
}
|
||||
});
|
||||
|
||||
console.log('API原始响应:', response);
|
||||
|
||||
// 处理不同的响应格式 - 参考chat.vue的实现
|
||||
let processedResponse = null;
|
||||
|
||||
// 如果响应是字符串,直接返回
|
||||
if (typeof response === 'string') {
|
||||
processedResponse = response;
|
||||
}
|
||||
// 如果响应是对象,尝试提取AI回复
|
||||
else if (typeof response === 'object' && response !== null) {
|
||||
// 优先处理嵌套结构:res.data.data.response
|
||||
if (response.data && response.data.data && response.data.data.response) {
|
||||
processedResponse = response.data.data.response;
|
||||
console.log('从嵌套结构 data.data.response 提取回复:', processedResponse);
|
||||
}
|
||||
// 备用:直接使用 data.response
|
||||
else if (response.data && response.data.response) {
|
||||
processedResponse = response.data.response;
|
||||
console.log('从字段 data.response 提取回复:', processedResponse);
|
||||
}
|
||||
// 检查是否为状态消息
|
||||
else if (response.data && response.data.message) {
|
||||
if (response.data.message === '对话成功') {
|
||||
// 后端返回成功消息,使用默认回复
|
||||
processedResponse = '我收到了你的消息,很高兴和你聊天!';
|
||||
console.log('检测到对话成功状态,使用默认回复');
|
||||
} else {
|
||||
// 如果后端返回了错误信息,抛出错误
|
||||
throw new Error(response.data.message);
|
||||
}
|
||||
}
|
||||
// 尝试其他可能的字段名
|
||||
else {
|
||||
const possibleFields = ['message', 'response', 'content', 'reply', 'answer', 'text'];
|
||||
|
||||
for (const field of possibleFields) {
|
||||
if (response[field] && typeof response[field] === 'string') {
|
||||
processedResponse = response[field];
|
||||
console.log(`从字段 ${field} 提取回复:`, processedResponse);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果还是没找到,尝试在data中查找
|
||||
if (!processedResponse && response.data) {
|
||||
for (const field of possibleFields) {
|
||||
if (response.data[field] && typeof response.data[field] === 'string') {
|
||||
processedResponse = response.data[field];
|
||||
console.log(`从字段 data.${field} 提取回复:`, processedResponse);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果仍然没有找到有效回复,使用默认回复
|
||||
if (!processedResponse) {
|
||||
processedResponse = '我收到了你的消息,很高兴和你聊天!';
|
||||
console.log('未找到有效回复,使用默认回复');
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: processedResponse,
|
||||
originalResponse: response
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('AI聊天API调用失败:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
// 异步聊天接口(如果需要)
|
||||
asyncChat: async (params) => {
|
||||
try {
|
||||
const response = await request({
|
||||
url: '/api/chat/async',
|
||||
method: 'POST',
|
||||
data: {
|
||||
message: params.message,
|
||||
characterId: params.characterId,
|
||||
conversationId: params.conversationId || null,
|
||||
...params
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: response
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('AI异步聊天API调用失败:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
// 获取聊天历史
|
||||
getChatHistory: async (conversationId) => {
|
||||
try {
|
||||
const response = await request({
|
||||
url: `/api/chat/history/${conversationId}`,
|
||||
method: 'GET'
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: response
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('获取聊天历史失败:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
// 创建新对话
|
||||
createConversation: async (characterId) => {
|
||||
try {
|
||||
const response = await request({
|
||||
url: '/api/chat/conversation',
|
||||
method: 'POST',
|
||||
data: {
|
||||
characterId: characterId
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: response
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('创建对话失败:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 语音相关API
|
||||
export const voiceAPI = {
|
||||
// 1. 文本转语音 - TTS功能
|
||||
textToSpeech: async (text, options = {}) => {
|
||||
try {
|
||||
const loginStatus = checkLoginStatus();
|
||||
|
||||
if (!loginStatus.isLoggedIn) {
|
||||
return {
|
||||
success: false,
|
||||
error: { message: '用户未登录' }
|
||||
};
|
||||
}
|
||||
|
||||
console.log('开始文本转语音:', text);
|
||||
|
||||
const response = await request({
|
||||
url: '/api/chat/tts',
|
||||
method: 'POST',
|
||||
data: {
|
||||
text: text,
|
||||
voiceStyle: options.voiceStyle || 'default',
|
||||
modelId: options.modelId || null,
|
||||
templateId: options.templateId || null
|
||||
}
|
||||
});
|
||||
|
||||
console.log('文本转语音响应:', response);
|
||||
|
||||
// 处理响应数据
|
||||
let audioUrl = null;
|
||||
if (response.data && response.data.audioUrl) {
|
||||
audioUrl = response.data.audioUrl;
|
||||
} else if (response.audioUrl) {
|
||||
audioUrl = response.audioUrl;
|
||||
} else if (response.data && response.data.url) {
|
||||
audioUrl = response.data.url;
|
||||
} else if (response.url) {
|
||||
audioUrl = response.url;
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
audioUrl: audioUrl,
|
||||
originalResponse: response
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('文本转语音失败:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
// 2. 对话+语音合成 - 对话后转语音
|
||||
chatWithTTS: async (message, options = {}) => {
|
||||
try {
|
||||
const loginStatus = checkLoginStatus();
|
||||
|
||||
if (!loginStatus.isLoggedIn) {
|
||||
return {
|
||||
success: false,
|
||||
error: { message: '用户未登录' }
|
||||
};
|
||||
}
|
||||
|
||||
console.log('开始对话+语音合成:', message);
|
||||
|
||||
const response = await request({
|
||||
url: '/api/chat/answer-tts',
|
||||
method: 'POST',
|
||||
data: {
|
||||
message: message,
|
||||
modelId: options.modelId || null,
|
||||
templateId: options.templateId || null,
|
||||
voiceStyle: options.voiceStyle || 'default'
|
||||
}
|
||||
});
|
||||
|
||||
console.log('对话+语音合成响应:', response);
|
||||
|
||||
// 处理响应数据
|
||||
let aiResponse = null;
|
||||
let audioUrl = null;
|
||||
|
||||
if (response.data) {
|
||||
aiResponse = response.data.response || response.data.text || response.data.message;
|
||||
audioUrl = response.data.audioUrl || response.data.url;
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
aiResponse: aiResponse,
|
||||
audioUrl: audioUrl,
|
||||
originalResponse: response
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('对话+语音合成失败:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
// 3. 语音对话 - 完整语音交互流程
|
||||
voiceChat: async (filePath, options = {}) => {
|
||||
try {
|
||||
const loginStatus = checkLoginStatus();
|
||||
|
||||
// 如果用户未登录,直接返回失败,让前端使用降级处理
|
||||
if (!loginStatus.isLoggedIn) {
|
||||
console.log('用户未登录,语音对话跳过API调用');
|
||||
return {
|
||||
success: false,
|
||||
error: { message: '用户未登录,使用降级处理' }
|
||||
};
|
||||
}
|
||||
|
||||
console.log('开始语音对话,文件路径:', filePath);
|
||||
|
||||
// 构建认证头
|
||||
let authHeader = '';
|
||||
if (loginStatus.token) {
|
||||
authHeader = loginStatus.token.startsWith('Bearer ') ? loginStatus.token : 'Bearer ' + loginStatus.token;
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
uni.uploadFile({
|
||||
url: BASE_URL + '/api/chat/voice-chat',
|
||||
filePath: filePath,
|
||||
name: 'audio',
|
||||
header: authHeader ? {
|
||||
'Authorization': authHeader
|
||||
} : {},
|
||||
formData: {
|
||||
modelId: options.modelId || null,
|
||||
templateId: options.templateId || null,
|
||||
voiceStyle: options.voiceStyle || 'default'
|
||||
},
|
||||
success: (res) => {
|
||||
console.log('语音对话上传成功:', res);
|
||||
|
||||
try {
|
||||
const data = JSON.parse(res.data);
|
||||
console.log('语音对话响应数据:', data);
|
||||
|
||||
if (data.code === 200 && data.data) {
|
||||
resolve({
|
||||
success: true,
|
||||
data: {
|
||||
userText: data.data.userText || data.data.text,
|
||||
aiResponse: data.data.aiResponse || data.data.response,
|
||||
audioUrl: data.data.audioUrl || data.data.url
|
||||
}
|
||||
});
|
||||
} else {
|
||||
resolve({
|
||||
success: false,
|
||||
error: { message: data.message || '语音对话失败' }
|
||||
});
|
||||
}
|
||||
} catch (parseError) {
|
||||
console.error('解析语音对话响应失败:', parseError);
|
||||
resolve({
|
||||
success: false,
|
||||
error: { message: '解析响应失败' }
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('语音对话上传失败:', err);
|
||||
resolve({
|
||||
success: false,
|
||||
error: err
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('语音对话失败:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
// 4. 音频文件上传语音对话
|
||||
uploadVoiceChat: async (filePath, options = {}) => {
|
||||
try {
|
||||
const loginStatus = checkLoginStatus();
|
||||
|
||||
// 如果用户未登录,直接返回失败,让前端使用降级处理
|
||||
if (!loginStatus.isLoggedIn) {
|
||||
console.log('用户未登录,上传音频文件语音对话跳过API调用');
|
||||
return {
|
||||
success: false,
|
||||
error: { message: '用户未登录,使用降级处理' }
|
||||
};
|
||||
}
|
||||
|
||||
console.log('开始上传音频文件语音对话,文件路径:', filePath);
|
||||
|
||||
// 构建认证头
|
||||
let authHeader = '';
|
||||
if (loginStatus.token) {
|
||||
authHeader = loginStatus.token.startsWith('Bearer ') ? loginStatus.token : 'Bearer ' + loginStatus.token;
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
uni.uploadFile({
|
||||
url: BASE_URL + '/api/chat/upload-voice-chat',
|
||||
filePath: filePath,
|
||||
name: 'audio',
|
||||
header: authHeader ? {
|
||||
'Authorization': authHeader
|
||||
} : {},
|
||||
formData: {
|
||||
modelId: options.modelId || null,
|
||||
templateId: options.templateId || null,
|
||||
voiceStyle: options.voiceStyle || 'default'
|
||||
},
|
||||
success: (res) => {
|
||||
console.log('上传音频文件语音对话成功:', res);
|
||||
|
||||
try {
|
||||
const data = JSON.parse(res.data);
|
||||
console.log('上传音频文件语音对话响应数据:', data);
|
||||
|
||||
if (data.code === 200 && data.data) {
|
||||
resolve({
|
||||
success: true,
|
||||
data: {
|
||||
userText: data.data.userText || data.data.text,
|
||||
aiResponse: data.data.aiResponse || data.data.response,
|
||||
audioUrl: data.data.audioUrl || data.data.url
|
||||
}
|
||||
});
|
||||
} else {
|
||||
resolve({
|
||||
success: false,
|
||||
error: { message: data.message || '上传音频文件语音对话失败' }
|
||||
});
|
||||
}
|
||||
} catch (parseError) {
|
||||
console.error('解析上传音频文件语音对话响应失败:', parseError);
|
||||
resolve({
|
||||
success: false,
|
||||
error: { message: '解析响应失败' }
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('上传音频文件语音对话失败:', err);
|
||||
resolve({
|
||||
success: false,
|
||||
error: err
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('上传音频文件语音对话失败:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
// 兼容性方法:语音识别(降级处理)
|
||||
speechToText: async (filePath) => {
|
||||
console.warn('speechToText 方法已废弃,请使用 voiceChat 或 uploadVoiceChat');
|
||||
// 降级处理:使用语音对话接口
|
||||
const result = await voiceAPI.voiceChat(filePath);
|
||||
if (result.success) {
|
||||
return {
|
||||
success: true,
|
||||
data: result.data.userText
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
// 导出默认请求方法
|
||||
export default request;
|
||||
Reference in New Issue
Block a user