Blogger Information
Blog 26
fans 0
comment 0
visits 12187
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
留言板案例改成"智能客服系统",实现机器人回复,附前后端源码(1108)
高空中的云
Original
568 people have browsed it


点击自己的信息,是弹出删除提示框,忘记操作了…
源码如下,

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>微信对话</title>
  8. <style>
  9. ul {
  10. list-style: none;
  11. display: grid;
  12. padding: 0;
  13. }
  14. ul li.right {
  15. place-self: end;
  16. }
  17. .right > div,
  18. .left > div {
  19. background-color: #fff;
  20. padding: 5px;
  21. max-width: 280px;
  22. margin-bottom: 10px;
  23. }
  24. .chat {
  25. width: 375px;
  26. height: 600px;
  27. background-color: #fefefe;
  28. margin: 100px auto 0 auto;
  29. border: 1px solid #000;
  30. overflow-y: scroll;
  31. overflow-x: hidden;
  32. }
  33. section.dialog {
  34. padding: 15px;
  35. min-height: 520px;
  36. background-color: rgb(245,245,245);
  37. }
  38. footer {
  39. background-color: aliceblue;
  40. width: 100%;
  41. height: 40px;
  42. position: sticky;
  43. bottom: 0;
  44. left: 0;
  45. padding: 5px;
  46. display: grid;
  47. grid-template-columns: 250px auto;
  48. place-content: center;
  49. }
  50. footer>input {
  51. height: 30px;
  52. margin-right: 20px;
  53. }
  54. footer>button {
  55. height: 36px;
  56. width: 80px;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <div class="chat">
  62. <section class="dialog">
  63. <ul class="list">
  64. <!-- 其实每一条新留言,应该添加到ul的起始标签的后面: afterbegin -->
  65. </ul>
  66. </section>
  67. <div id="anchor"></div>
  68. <footer>
  69. <input class="input" type="text" onkeydown="insertComment(this)" placeholder="请输入留言" autofocus />
  70. <button onclick="sendComment()">发送</button>
  71. </footer>
  72. </div>
  73. <script>
  74. // 计算页面高度,如果聊天内容高于页面窗口高度,则向上滚动一定高度,否则,不动
  75. const scrollPage = function(){
  76. let list = document.querySelector('.list');
  77. let listHeight = list.scrollHeight;
  78. if(listHeight >= 600){
  79. window.scrollTo({
  80. top: 800,
  81. behavior: "smooth"
  82. });
  83. }
  84. }
  85. const Comment = function (ele) {
  86. if (ele.value.length === 0) {
  87. // 提示用户
  88. alert('留言不能为空');
  89. // 重置焦点
  90. ele.focus();
  91. // 直接返回
  92. return false;
  93. }
  94. const ul = document.querySelector('.list');
  95. scrollPage();
  96. // ele.value += `<button onclick="del(this.parentNode)">删除</button>`;
  97. ul.insertAdjacentHTML('beforeend', `<li class="right" onclick="del(this)"><div>${ele.value}</div></li>`);
  98. const url = 'http://chatdemo.com/index.php?msg='+ele.value;
  99. fetch(url)
  100. .then(function (response) {
  101. return response.json();
  102. })
  103. .then(function (json) {
  104. scrollPage();
  105. ul.insertAdjacentHTML('beforeend', `<li class="left"><div>${json.content}</div></li>`);
  106. });
  107. // 3. 清空输入框
  108. ele.value = null;
  109. }
  110. const insertComment = function (ele) {
  111. // 只有按下回车键才提交
  112. if (event.key === 'Enter') {
  113. Comment(ele);
  114. }
  115. };
  116. const sendComment = function () {
  117. const ele = document.querySelector('.input');
  118. Comment(ele);
  119. };
  120. // 删除
  121. const del = function (ele) {
  122. // console.log(ele);
  123. // confirm: 有一个确定,还有一个取消,确定:true, 取消:false
  124. // return confirm('是否删除?') ? ele.remove() : false;
  125. return confirm('是否删除?') ? (ele.outerHTML = null) : false;
  126. };
  127. </script>
  128. </body>
  129. </html>

因为用的这个机器人api不支持前端js跨域,所以写了个php,从后端操作(chatdemo.com/index.php 对应的就是下边这个’站点文件’):

  1. // 允许跨域
  2. header("access-control-allow-origin: *");
  3. $msg = $_GET['msg'] ?? '';
  4. $url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg=' . $msg;
  5. $ch = curl_init();
  6. curl_setopt($ch, CURLOPT_URL, $url); //设置访问的url地址
  7. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  8. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不输出内容
  10. $result = curl_exec($ch);
  11. curl_close($ch);
  12. // echo $result;die;
  13. echo $result;
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:效果非常好啊, 继续加油
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post