ai.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { request } from '@/util/request'
  2. import { baseUrl } from './config.js'
  3. // 生成AI旅游计划
  4. export function generateAIPlan(data) {
  5. return request({
  6. url: `/api/ai/travel/generate`,
  7. method: 'POST',
  8. data,
  9. header: {
  10. 'Content-Type': 'application/json'
  11. }
  12. })
  13. }
  14. // 获取特定AI生成的旅游计划
  15. export function getAIPlan(planId) {
  16. return request({
  17. url: `/api/ai/travel/${planId}`,
  18. method: 'GET'
  19. })
  20. }
  21. // 获取用户所有AI生成的旅游计划
  22. export function getUserAIPlans(userId) {
  23. return request({
  24. url: `/api/ai/travel/user/${userId}`,
  25. method: 'GET'
  26. })
  27. }
  28. // 保存AI生成的旅游计划
  29. export function saveAIPlan(data) {
  30. return request({
  31. url: `/api/ai/travel/plans`,
  32. method: 'POST',
  33. data,
  34. header: {
  35. 'Content-Type': 'application/json'
  36. }
  37. })
  38. }
  39. // 获取WebSocket URL - 使用标准WebSocket端点
  40. export function getWebSocketUrl(sessionId) {
  41. let wsBaseUrl = baseUrl.replace(/^http:/, 'ws:').replace(/^https:/, 'wss:');
  42. console.log('WebSocket baseUrl:', wsBaseUrl);
  43. const finalUrl = `${wsBaseUrl}/api/ai/travel/ws/${sessionId}`;
  44. console.log('最终WebSocket URL:', finalUrl);
  45. return finalUrl;
  46. }
  47. // 开始新会话
  48. export const startAISession = async () => {
  49. try {
  50. const response = await uni.request({
  51. url: `${baseUrl}/api/ai/travel/start`,
  52. method: 'POST',
  53. header: {
  54. 'Content-Type': 'application/json'
  55. }
  56. });
  57. if (response.statusCode === 200) {
  58. return response.data;
  59. } else {
  60. throw new Error(response.data?.msg || '创建会话失败');
  61. }
  62. } catch (error) {
  63. console.error('创建会话失败:', error);
  64. throw error;
  65. }
  66. };
  67. // 与AI对话
  68. export const chatWithAI = async (sessionId, message) => {
  69. try {
  70. console.log('准备发送聊天请求:', {
  71. url: `${baseUrl}/api/ai/travel/chat`,
  72. sessionId,
  73. message
  74. });
  75. // 添加更多的日志信息,帮助调试
  76. const isLogMessage = message.startsWith('[LOG_TO_CONSOLE]');
  77. if (isLogMessage) {
  78. console.log('发送带日志标记的消息到服务器');
  79. }
  80. const response = await uni.request({
  81. url: `${baseUrl}/api/ai/travel/chat`,
  82. method: 'POST',
  83. header: {
  84. 'Content-Type': 'application/json',
  85. 'sessionId': sessionId,
  86. // 添加特殊标记,让后端知道要输出日志
  87. 'X-Log-To-Console': 'true'
  88. },
  89. data: {
  90. message: message,
  91. // 添加日志标志到数据中
  92. logToConsole: true
  93. }
  94. });
  95. console.log('服务器响应状态码:', response.statusCode);
  96. if (response.statusCode === 200) {
  97. return response.data;
  98. } else {
  99. throw new Error(response.data?.msg || `请求失败: ${response.statusCode}`);
  100. }
  101. } catch (error) {
  102. console.error('发送消息失败:', error);
  103. throw error;
  104. }
  105. };
  106. // 结束会话
  107. export async function endAISession(sessionId) {
  108. try {
  109. const response = await uni.request({
  110. url: `${baseUrl}/api/ai/travel/end`,
  111. method: 'POST',
  112. header: {
  113. 'Content-Type': 'application/json',
  114. 'sessionId': sessionId
  115. }
  116. });
  117. console.log('结束会话响应:', response);
  118. return response.data;
  119. } catch (error) {
  120. console.error('结束会话失败:', error);
  121. throw error;
  122. }
  123. }
  124. // 获取SSE连接URL
  125. export function getSSEUrl(sessionId) {
  126. return `${baseUrl}/api/ai/travel/stream?sessionId=${sessionId}`;
  127. }
  128. // 获取最新的AI回复
  129. export async function getLastAIReply(sessionId) {
  130. try {
  131. console.log('获取最新AI回复:', sessionId);
  132. // 使用自定义接口检查状态,而不是发送新消息
  133. const response = await uni.request({
  134. url: `${baseUrl}/api/ai/travel/status`,
  135. method: 'GET',
  136. header: {
  137. 'Content-Type': 'application/json',
  138. 'sessionId': sessionId
  139. },
  140. // 设置超时时间为5秒
  141. timeout: 5000
  142. });
  143. console.log('检查状态响应:', response);
  144. // 如果接口不存在,则认为没有新消息
  145. if (response.statusCode === 404) {
  146. console.log('状态检查接口不存在,视为无新消息');
  147. return {
  148. code: 200,
  149. reply: null,
  150. msg: '无新消息'
  151. };
  152. }
  153. // 尝试提取回复内容
  154. let reply = null;
  155. if (response.data) {
  156. if (response.data.reply) {
  157. reply = response.data.reply;
  158. } else if (response.data.data && response.data.data.reply) {
  159. reply = response.data.data.reply;
  160. } else if (response.data.content) {
  161. reply = response.data.content;
  162. } else if (response.data.msg && response.data.msg.length > 20) {
  163. reply = response.data.msg;
  164. }
  165. }
  166. return {
  167. code: response.data.code || 200,
  168. reply: reply,
  169. msg: response.data.msg || '无新消息',
  170. hasNewMessage: response.data.hasNewMessage || false
  171. };
  172. } catch (error) {
  173. // 超时或网络错误时,返回空结果而不是抛出异常
  174. console.error('获取最新回复失败:', error);
  175. return {
  176. code: 200,
  177. reply: null,
  178. msg: '检查新消息超时',
  179. hasNewMessage: false
  180. };
  181. }
  182. }
  183. // 获取服务器响应
  184. export async function fetchServerResponse(sessionId) {
  185. try {
  186. console.log('获取服务器响应:', sessionId);
  187. // 使用聊天接口,发送特殊命令获取服务器响应
  188. const response = await uni.request({
  189. url: `${baseUrl}/api/ai/travel/chat`,
  190. method: 'POST',
  191. header: {
  192. 'Content-Type': 'application/json',
  193. 'sessionId': sessionId
  194. },
  195. data: {
  196. message: '显示服务器响应',
  197. command: 'fetchServerResponse'
  198. }
  199. });
  200. console.log('服务器响应:', response);
  201. return {
  202. code: response.data.code || 200,
  203. data: response.data,
  204. statusCode: response.statusCode,
  205. msg: response.data.msg || '获取服务器响应成功'
  206. };
  207. } catch (error) {
  208. console.error('获取服务器响应失败:', error);
  209. throw error;
  210. }
  211. }