飞天AI数字人展会页面
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.5 KiB

6 months ago
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
  8. <meta http-equiv="Pragma" content="no-cache" />
  9. <meta http-equiv="Expires" content="0" />
  10. <link rel="stylesheet" type="text/css" href="css/libs/reset.css">
  11. <link rel="stylesheet" type="text/css" href="css/common.css">
  12. <link rel="stylesheet" type="text/css" href="css/index.css">
  13. <title>AI数字人</title>
  14. <!--[if lt IE 9]>
  15. <script src="js/html5shiv.js"></script>
  16. <script src="js/respond.min.js"></script>
  17. <![endif]-->
  18. </head>
  19. <body>
  20. <div class="ai-wrapper">
  21. <video class="ai-bg" src="./images/bg.mp4" autoplay="autoplay" loop="loop" muted="muted"></video>
  22. <div class="ai-header-wrapper">
  23. <div class="ai-header">
  24. <div class="header-btn header-home" onclick="history.go(-1); return false;"><span></span><p>首页</p></div>
  25. <div class="header-title">
  26. <h2>AI编程机器人体验系统</h2>
  27. </div>
  28. <div class="header-btn header-login"><span></span><p>登录</p></div>
  29. </div>
  30. </div>
  31. <div class="chat-wrapper">
  32. <div class="chat-content">
  33. </div>
  34. <div class="chat-send">
  35. <textarea cols="50" rows="7"></textarea>
  36. <!-- <div class="send-button">send</div> -->
  37. <div class="send-button-container">
  38. <input type="button" value="发送" class="send-button">
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </body>
  44. <script type="text/javascript" src="js/libs/jquery-3.7.1.min.js"></script>
  45. <script type="text/javascript" src="js/libs/layer/layer.js"></script>
  46. <script type="text/javascript" src="js/libs/flexible.js"></script>
  47. <script>
  48. $(document).ready(function() {
  49. const $sendButton = $('.send-button');
  50. const $textArea = $('.chat-send textarea');
  51. const $chatContent = $('.chat-content');
  52. let isBotReplying = false;
  53. let isBotTyping = false;
  54. // 默认进来AI数字人发送欢迎消息
  55. function sendWelcomeMessage() {
  56. const welcomeMessage = '您好!请问有什么问题可以帮您解答吗?';
  57. appendMessage(welcomeMessage, false, false, true);
  58. }
  59. sendWelcomeMessage();
  60. // 初始化时检查按钮状态
  61. updateSendButtonState();
  62. function updateSendButtonState() {
  63. const message = $textArea.val().trim();
  64. // && !isBotReplying && !isBotTyping
  65. if (message) {
  66. $sendButton.prop('disabled', false).removeClass('send-disabled-button');
  67. } else {
  68. $sendButton.prop('disabled', true).addClass('send-disabled-button');
  69. }
  70. }
  71. // 当 textarea 内容变化时更新按钮状态
  72. $textArea.on('input', function() {
  73. updateSendButtonState();
  74. });
  75. // 发送消息并接收机器人的回复
  76. $sendButton.click(function() {
  77. if (!isBotReplying && !isBotTyping) {
  78. const message = $textArea.val().trim();
  79. if (message) {
  80. appendMessage(message, true, false);
  81. $textArea.val('');
  82. $sendButton.prop('disabled', true).addClass('send-disabled-button');
  83. // loading
  84. appendMessage('', false, true);
  85. isBotReplying = true;
  86. $.ajax({
  87. url: 'http://192.168.99.86:3001/api/v1/workspace/dxhtsg/chat',
  88. type: 'POST',
  89. headers: {
  90. 'Authorization': 'Bearer XVSPT0T-6P54SZH-QP36QKJ-KK77TN6'
  91. },
  92. contentType:'application/json',
  93. dataType: "json",
  94. data: JSON.stringify({
  95. 'message': message,
  96. 'mode':'chat'
  97. }),
  98. success: function (res) {
  99. $('.loading').parent().remove();
  100. const botReply = res.textResponse.replace(/\【(\/)?SYS\】/gi, '飞天智能AI小助手');
  101. // const botReply = res.textResponse.replace(/【(.*?)】/g, function(match, p1) {
  102. // return '【飞天智能AI小助手】';
  103. // });
  104. console.log(botReply);
  105. // 添加完整的机器人回复
  106. appendMessage('', false, false);
  107. isBotTyping = true;
  108. let i = 0;
  109. const speed = 50; // 每个字符的打字速度,单位:毫秒
  110. typeWriter();
  111. function typeWriter() {
  112. if (!isBotTyping) {
  113. // 如果isBotReplying为false,则停止打字
  114. return;
  115. }
  116. if (i < botReply.length) {
  117. $chatContent.find('.bot-message:last-child p').append(botReply.charAt(i));
  118. i++;
  119. setTimeout(typeWriter, speed);
  120. $chatContent.scrollTop($chatContent[0].scrollHeight);
  121. } else {
  122. // 完成打字后更新状态
  123. isBotTyping = false;
  124. isBotReplying = false;
  125. $('#stop-btn').remove()
  126. }
  127. }
  128. },
  129. error: function (err) {
  130. console.log(err);
  131. }
  132. });
  133. }
  134. } else if (isBotTyping) {
  135. console.log('isBotTyping',isBotTyping)
  136. // 输入中
  137. layer.msg('请等待当前对话完成,稍后再试。', {
  138. offset: 50,
  139. anim: 6
  140. });
  141. } else if (isBotReplying) {
  142. // loading中
  143. layer.msg('请等待当前对话完成,稍后再试。', {
  144. offset: 50,
  145. anim: 6
  146. });
  147. }
  148. });
  149. // 回车键发送消息
  150. $textArea.keypress(function(event) {
  151. if (event.which === 13) {
  152. event.preventDefault(); // 防止默认换行行为
  153. $sendButton.click();
  154. }
  155. });
  156. function appendMessage(content, isUserMessage, isLoading, isWelcomeMessage) {
  157. const className = isUserMessage ? 'user-message' : 'bot-message';
  158. const avatarSrc = isUserMessage ? 'images/other/pic1.jpg' : 'images/other/pic2.jpg';
  159. let messageContent
  160. if (isLoading) {
  161. messageContent = '<p class="loading"><img src="images/other/loading.png" alt="loading"></p>';
  162. } else {
  163. // 当isUserMessage为false时,才添加<span>停止输出</span>
  164. messageContent = isUserMessage
  165. ? `<p>${content}</p>`
  166. : isWelcomeMessage
  167. ? `<p>${content}</p>` // 如果是欢迎消息,即使isUserMessage为false,也不添加<span>停止输出</span>
  168. : `<p>${content}<span id="stop-btn">停止输出</span></p>`;
  169. }
  170. const messageHtml = `
  171. <div class="chat-message ${className}">
  172. ${messageContent}
  173. <img src="${avatarSrc}" alt="${isUserMessage ? '用户' : 'AI数字人'}">
  174. </div>
  175. `;
  176. $chatContent.append(messageHtml);
  177. $chatContent.scrollTop($chatContent[0].scrollHeight);
  178. $('#stop-btn').click(function() {
  179. isBotTyping = false;
  180. isBotReplying = false;
  181. $(this).removeAttr('id').addClass('remove-btn').html('用户取消')
  182. });
  183. }
  184. });
  185. </script>
  186. </html>