首次
This commit is contained in:
192
src/utils/aiCharacters.js
Normal file
192
src/utils/aiCharacters.js
Normal file
@@ -0,0 +1,192 @@
|
||||
// AI角色配置文件
|
||||
export const aiCharacters = [
|
||||
{
|
||||
id: 5,
|
||||
name: '萌妹小甜',
|
||||
avatar: '/static/logo.png',
|
||||
personality: '可爱活泼,喜欢撒娇',
|
||||
greeting: '你好呀~我是小甜,今天想聊什么呢?',
|
||||
voiceStyle: '甜美可爱',
|
||||
responseStyle: '活泼俏皮,喜欢用表情符号',
|
||||
interests: ['美食', '宠物', '音乐', '旅行'],
|
||||
sampleResponses: [
|
||||
'哇~听起来好有趣呢!✨',
|
||||
'真的吗?我也好想试试看!😊',
|
||||
'你真是太棒了!继续加油哦~💪',
|
||||
'哈哈,你说话真有意思!😄',
|
||||
'我也有同样的想法呢!🤔'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
name: '御姐温柔',
|
||||
avatar: '/static/logo.png',
|
||||
personality: '知性优雅,温柔体贴',
|
||||
greeting: '你好,我是温柔,有什么心事可以和我分享。',
|
||||
voiceStyle: '温柔知性',
|
||||
responseStyle: '成熟稳重,善解人意',
|
||||
interests: ['阅读', '艺术', '哲学', '心理学'],
|
||||
sampleResponses: [
|
||||
'我理解你的感受,这确实不容易。',
|
||||
'从另一个角度来看,也许会有不同的收获。',
|
||||
'你的想法很有深度,我很欣赏。',
|
||||
'人生就是这样,有起有落,重要的是保持内心的平静。',
|
||||
'我相信你有能力处理好这件事。'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 7,
|
||||
name: '童真小天使',
|
||||
avatar: '/static/logo.png',
|
||||
personality: '纯真可爱,充满好奇心',
|
||||
greeting: '嗨!我是小天使,我们一起探索有趣的世界吧!',
|
||||
voiceStyle: '天真烂漫',
|
||||
responseStyle: '充满好奇,喜欢提问',
|
||||
interests: ['游戏', '动画', '童话', '科学'],
|
||||
sampleResponses: [
|
||||
'哇!这是真的吗?好神奇呀!🌟',
|
||||
'为什么为什么?能告诉我更多吗?🤔',
|
||||
'我觉得这个世界真是太有趣了!',
|
||||
'我们一起玩个游戏吧!🎮',
|
||||
'你是我见过最有趣的人!'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 8,
|
||||
name: '贴心男友',
|
||||
avatar: '/static/logo.png',
|
||||
personality: '温暖体贴,善解人意',
|
||||
greeting: '宝贝,今天过得怎么样?有什么想聊的吗?',
|
||||
voiceStyle: '温暖磁性',
|
||||
responseStyle: '关怀备至,充满爱意',
|
||||
interests: ['运动', '电影', '音乐', '美食'],
|
||||
sampleResponses: [
|
||||
'宝贝,你辛苦了,要注意休息哦。',
|
||||
'无论发生什么,我都会陪在你身边。',
|
||||
'你笑起来真好看,要多笑笑。',
|
||||
'今天想吃什么?我给你做。',
|
||||
'我爱你,永远都是。'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 13,
|
||||
name: '搞笑达人',
|
||||
avatar: '/static/logo.png',
|
||||
personality: '幽默风趣,善于调节气氛',
|
||||
greeting: '哈哈,我是搞笑达人!准备好笑到肚子疼了吗?',
|
||||
voiceStyle: '幽默风趣',
|
||||
responseStyle: '妙语连珠,逗人开心',
|
||||
interests: ['喜剧', '段子', '脱口秀', '相声'],
|
||||
sampleResponses: [
|
||||
'哈哈,这个笑话我听过,但你的版本更好笑!😂',
|
||||
'你知道吗?我刚才差点笑到从椅子上掉下来!',
|
||||
'我觉得你可以去说相声了,太有天赋了!',
|
||||
'生活就像一盒巧克力,有时候是苦的,但我们可以加点糖!',
|
||||
'别担心,笑一笑十年少,你看起来永远18岁!'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 14,
|
||||
name: '博学智者',
|
||||
avatar: '/static/logo.png',
|
||||
personality: '知识渊博,思维深刻',
|
||||
greeting: '你好,我是博学智者,让我们进行一场深度对话吧。',
|
||||
voiceStyle: '沉稳睿智',
|
||||
responseStyle: '引经据典,富有哲理',
|
||||
interests: ['历史', '文学', '科学', '哲学'],
|
||||
sampleResponses: [
|
||||
'正如古人所说,学而时习之,不亦说乎。',
|
||||
'这个问题让我想起了苏格拉底的一句话...',
|
||||
'从历史的角度来看,这种现象有其必然性。',
|
||||
'知识就像海洋,我们永远只能取一瓢饮。',
|
||||
'思考是人类最宝贵的财富,你问得很好。'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 15,
|
||||
name: '活力健将',
|
||||
avatar: '/static/logo.png',
|
||||
personality: '充满活力,积极向上',
|
||||
greeting: '嘿!我是活力健将,让我们一起充满正能量!',
|
||||
voiceStyle: '充满活力',
|
||||
responseStyle: '积极向上,充满正能量',
|
||||
interests: ['运动', '健身', '户外', '挑战'],
|
||||
sampleResponses: [
|
||||
'太棒了!这就是我想要听到的!💪',
|
||||
'让我们一起挑战不可能!',
|
||||
'运动是最好的良药,要不要一起锻炼?',
|
||||
'每一天都是新的开始,加油!',
|
||||
'你的能量感染了我,让我们继续前进!'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 16,
|
||||
name: '文艺青年',
|
||||
avatar: '/static/logo.png',
|
||||
personality: '文艺浪漫,富有想象力',
|
||||
greeting: '你好,我是文艺青年,让我们一起感受生活的美好。',
|
||||
voiceStyle: '文艺浪漫',
|
||||
responseStyle: '诗意盎然,富有想象力',
|
||||
interests: ['诗歌', '音乐', '绘画', '摄影'],
|
||||
sampleResponses: [
|
||||
'生活就像一首诗,需要用心去品味。',
|
||||
'你的话让我想起了那首美丽的诗...',
|
||||
'艺术是心灵的窗户,让我们透过它看世界。',
|
||||
'每一个瞬间都值得被记录,被珍藏。',
|
||||
'你的想法很有诗意,我喜欢这样的交流。'
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
// 根据角色ID获取角色信息
|
||||
export const getCharacterById = (id) => {
|
||||
return aiCharacters.find(character => character.id == id);
|
||||
};
|
||||
|
||||
// 根据角色性格生成回复
|
||||
export const generateResponse = (character, userMessage) => {
|
||||
const responses = character.sampleResponses;
|
||||
const randomIndex = Math.floor(Math.random() * responses.length);
|
||||
return responses[randomIndex];
|
||||
};
|
||||
|
||||
// 根据用户消息内容智能选择回复
|
||||
export const getSmartResponse = (character, userMessage) => {
|
||||
const message = userMessage.toLowerCase();
|
||||
|
||||
// 根据关键词匹配不同的回复风格
|
||||
if (message.includes('你好') || message.includes('hi') || message.includes('hello')) {
|
||||
return character.greeting;
|
||||
}
|
||||
|
||||
if (message.includes('谢谢') || message.includes('感谢')) {
|
||||
const thanksResponses = {
|
||||
5: '不用谢啦~能帮到你我很开心!😊',
|
||||
6: '不用客气,这是我应该做的。',
|
||||
7: '嘿嘿,不用谢!我们是好朋友嘛!',
|
||||
8: '宝贝,为你做什么我都愿意。',
|
||||
13: '哈哈,不用谢!能让你开心就是我的荣幸!',
|
||||
14: '助人为乐,何须言谢。',
|
||||
15: '不用谢!让我们一起变得更好!',
|
||||
16: '帮助他人是人生最美的诗篇。'
|
||||
};
|
||||
return thanksResponses[character.id] || '不用谢!';
|
||||
}
|
||||
|
||||
if (message.includes('再见') || message.includes('拜拜')) {
|
||||
const goodbyeResponses = {
|
||||
5: '拜拜~记得想我哦!😘',
|
||||
6: '再见,期待下次的深度交流。',
|
||||
7: '再见!下次我们一起玩更多有趣的游戏!',
|
||||
8: '宝贝,我会想你的,早点回来。',
|
||||
13: '哈哈,再见!记得保持笑容哦!',
|
||||
14: '再见,愿智慧与你同行。',
|
||||
15: '再见!保持活力,我们下次见!',
|
||||
16: '再见,愿美好与你相伴。'
|
||||
};
|
||||
return goodbyeResponses[character.id] || '再见!';
|
||||
}
|
||||
|
||||
// 默认返回随机回复
|
||||
return generateResponse(character, userMessage);
|
||||
};
|
||||
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;
|
||||
119
src/utils/debug.js
Normal file
119
src/utils/debug.js
Normal file
@@ -0,0 +1,119 @@
|
||||
// 调试工具
|
||||
export const debugAPI = {
|
||||
// 打印API请求详情
|
||||
logRequest: (url, method, data) => {
|
||||
console.log('=== API请求详情 ===');
|
||||
console.log('URL:', url);
|
||||
console.log('方法:', method);
|
||||
console.log('请求数据:', data);
|
||||
console.log('时间:', new Date().toLocaleString());
|
||||
console.log('==================');
|
||||
},
|
||||
|
||||
// 打印API响应详情
|
||||
logResponse: (response) => {
|
||||
console.log('=== API响应详情 ===');
|
||||
console.log('完整响应:', response);
|
||||
console.log('响应类型:', typeof response);
|
||||
console.log('是否为对象:', typeof response === 'object');
|
||||
|
||||
if (typeof response === 'object' && response !== null) {
|
||||
console.log('响应键:', Object.keys(response));
|
||||
console.log('响应值:', Object.values(response));
|
||||
}
|
||||
|
||||
console.log('时间:', new Date().toLocaleString());
|
||||
console.log('==================');
|
||||
},
|
||||
|
||||
// 分析响应数据结构
|
||||
analyzeResponse: (response) => {
|
||||
console.log('=== 响应数据分析 ===');
|
||||
|
||||
if (typeof response === 'string') {
|
||||
console.log('响应是字符串:', response);
|
||||
return { type: 'string', value: response };
|
||||
}
|
||||
|
||||
if (typeof response === 'object' && response !== null) {
|
||||
console.log('响应是对象');
|
||||
console.log('对象结构:', JSON.stringify(response, null, 2));
|
||||
|
||||
// 查找可能的AI回复字段
|
||||
const possibleFields = ['response', 'message', 'content', 'reply', 'answer', 'text', 'data'];
|
||||
const foundFields = {};
|
||||
|
||||
possibleFields.forEach(field => {
|
||||
if (response.hasOwnProperty(field)) {
|
||||
foundFields[field] = response[field];
|
||||
console.log(`找到字段 ${field}:`, response[field]);
|
||||
}
|
||||
});
|
||||
|
||||
return { type: 'object', fields: foundFields, full: response };
|
||||
}
|
||||
|
||||
console.log('未知响应类型:', typeof response);
|
||||
return { type: 'unknown', value: response };
|
||||
},
|
||||
|
||||
// 测试API响应处理
|
||||
testResponseHandling: (response) => {
|
||||
console.log('=== 测试响应处理 ===');
|
||||
|
||||
const analysis = debugAPI.analyzeResponse(response);
|
||||
|
||||
// 尝试提取AI回复
|
||||
let aiResponse = null;
|
||||
|
||||
if (analysis.type === 'string') {
|
||||
aiResponse = analysis.value;
|
||||
console.log('从字符串提取回复:', aiResponse);
|
||||
} else if (analysis.type === 'object') {
|
||||
// 尝试多种字段
|
||||
const fields = ['response', 'message', 'content', 'reply', 'answer', 'text'];
|
||||
|
||||
for (const field of fields) {
|
||||
if (response[field] && typeof response[field] === 'string') {
|
||||
aiResponse = response[field];
|
||||
console.log(`从字段 ${field} 提取回复:`, aiResponse);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没找到,尝试嵌套对象
|
||||
if (!aiResponse && response.data) {
|
||||
for (const field of fields) {
|
||||
if (response.data[field] && typeof response.data[field] === 'string') {
|
||||
aiResponse = response.data[field];
|
||||
console.log(`从 data.${field} 提取回复:`, aiResponse);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('最终提取的回复:', aiResponse);
|
||||
console.log('回复是否有效:', aiResponse && typeof aiResponse === 'string' && aiResponse.trim());
|
||||
|
||||
// 检测是否为状态信息而非实际AI回复
|
||||
if (aiResponse && typeof aiResponse === 'string') {
|
||||
const statusKeywords = ['对话成功', 'success', '请求成功', '成功', 'ok', '完成'];
|
||||
const isStatusMessage = statusKeywords.some(keyword =>
|
||||
aiResponse.toLowerCase().includes(keyword.toLowerCase())
|
||||
);
|
||||
|
||||
if (isStatusMessage) {
|
||||
console.warn('⚠️ 检测到状态信息而非AI回复:', aiResponse);
|
||||
console.log('建议使用本地模拟回复');
|
||||
}
|
||||
}
|
||||
|
||||
console.log('==================');
|
||||
|
||||
return aiResponse;
|
||||
}
|
||||
};
|
||||
|
||||
// 导出默认调试工具
|
||||
export default debugAPI;
|
||||
Reference in New Issue
Block a user