飞天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.

206 lines
7.8 KiB

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