123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- import { request } from '@/util/request'
- import { baseUrl } from './config.js'
- // 生成AI旅游计划
- export function generateAIPlan(data) {
- return request({
- url: `/api/ai/travel/generate`,
- method: 'POST',
- data,
- header: {
- 'Content-Type': 'application/json'
- }
- })
- }
- // 获取特定AI生成的旅游计划
- export function getAIPlan(planId) {
- return request({
- url: `/api/ai/travel/${planId}`,
- method: 'GET'
- })
- }
- // 获取用户所有AI生成的旅游计划
- export function getUserAIPlans(userId) {
- return request({
- url: `/api/ai/travel/user/${userId}`,
- method: 'GET'
- })
- }
- // 保存AI生成的旅游计划
- export function saveAIPlan(data) {
- return request({
- url: `/api/ai/travel/plans`,
- method: 'POST',
- data,
- header: {
- 'Content-Type': 'application/json'
- }
- })
- }
- // 获取WebSocket URL - 使用标准WebSocket端点
- export function getWebSocketUrl(sessionId) {
- let wsBaseUrl = baseUrl.replace(/^http:/, 'ws:').replace(/^https:/, 'wss:');
- console.log('WebSocket baseUrl:', wsBaseUrl);
- const finalUrl = `${wsBaseUrl}/api/ai/travel/ws/${sessionId}`;
- console.log('最终WebSocket URL:', finalUrl);
- return finalUrl;
- }
- // 开始新会话
- export const startAISession = async () => {
- try {
- const response = await uni.request({
- url: `${baseUrl}/api/ai/travel/start`,
- method: 'POST',
- header: {
- 'Content-Type': 'application/json'
- }
- });
- if (response.statusCode === 200) {
- return response.data;
- } else {
- throw new Error(response.data?.msg || '创建会话失败');
- }
- } catch (error) {
- console.error('创建会话失败:', error);
- throw error;
- }
- };
- // 与AI对话
- export const chatWithAI = async (sessionId, message) => {
- try {
- console.log('准备发送聊天请求:', {
- url: `${baseUrl}/api/ai/travel/chat`,
- sessionId,
- message
- });
- // 添加更多的日志信息,帮助调试
- const isLogMessage = message.startsWith('[LOG_TO_CONSOLE]');
- if (isLogMessage) {
- console.log('发送带日志标记的消息到服务器');
- }
- const response = await uni.request({
- url: `${baseUrl}/api/ai/travel/chat`,
- method: 'POST',
- header: {
- 'Content-Type': 'application/json',
- 'sessionId': sessionId,
- // 添加特殊标记,让后端知道要输出日志
- 'X-Log-To-Console': 'true'
- },
- data: {
- message: message,
- // 添加日志标志到数据中
- logToConsole: true
- }
- });
- console.log('服务器响应状态码:', response.statusCode);
- if (response.statusCode === 200) {
- return response.data;
- } else {
- throw new Error(response.data?.msg || `请求失败: ${response.statusCode}`);
- }
- } catch (error) {
- console.error('发送消息失败:', error);
- throw error;
- }
- };
- // 结束会话
- export async function endAISession(sessionId) {
- try {
- const response = await uni.request({
- url: `${baseUrl}/api/ai/travel/end`,
- method: 'POST',
- header: {
- 'Content-Type': 'application/json',
- 'sessionId': sessionId
- }
- });
-
- console.log('结束会话响应:', response);
- return response.data;
- } catch (error) {
- console.error('结束会话失败:', error);
- throw error;
- }
- }
- // 获取SSE连接URL
- export function getSSEUrl(sessionId) {
- return `${baseUrl}/api/ai/travel/stream?sessionId=${sessionId}`;
- }
- // 获取最新的AI回复
- export async function getLastAIReply(sessionId) {
- try {
- console.log('获取最新AI回复:', sessionId);
-
- // 使用自定义接口检查状态,而不是发送新消息
- const response = await uni.request({
- url: `${baseUrl}/api/ai/travel/status`,
- method: 'GET',
- header: {
- 'Content-Type': 'application/json',
- 'sessionId': sessionId
- },
- // 设置超时时间为5秒
- timeout: 5000
- });
-
- console.log('检查状态响应:', response);
-
- // 如果接口不存在,则认为没有新消息
- if (response.statusCode === 404) {
- console.log('状态检查接口不存在,视为无新消息');
- return {
- code: 200,
- reply: null,
- msg: '无新消息'
- };
- }
-
- // 尝试提取回复内容
- let reply = null;
- if (response.data) {
- if (response.data.reply) {
- reply = response.data.reply;
- } else if (response.data.data && response.data.data.reply) {
- reply = response.data.data.reply;
- } else if (response.data.content) {
- reply = response.data.content;
- } else if (response.data.msg && response.data.msg.length > 20) {
- reply = response.data.msg;
- }
- }
-
- return {
- code: response.data.code || 200,
- reply: reply,
- msg: response.data.msg || '无新消息',
- hasNewMessage: response.data.hasNewMessage || false
- };
- } catch (error) {
- // 超时或网络错误时,返回空结果而不是抛出异常
- console.error('获取最新回复失败:', error);
- return {
- code: 200,
- reply: null,
- msg: '检查新消息超时',
- hasNewMessage: false
- };
- }
- }
- // 获取服务器响应
- export async function fetchServerResponse(sessionId) {
- try {
- console.log('获取服务器响应:', sessionId);
-
- // 使用聊天接口,发送特殊命令获取服务器响应
- const response = await uni.request({
- url: `${baseUrl}/api/ai/travel/chat`,
- method: 'POST',
- header: {
- 'Content-Type': 'application/json',
- 'sessionId': sessionId
- },
- data: {
- message: '显示服务器响应',
- command: 'fetchServerResponse'
- }
- });
-
- console.log('服务器响应:', response);
-
- return {
- code: response.data.code || 200,
- data: response.data,
- statusCode: response.statusCode,
- msg: response.data.msg || '获取服务器响应成功'
- };
- } catch (error) {
- console.error('获取服务器响应失败:', error);
- throw error;
- }
- }
|