当前位置:   article > 正文

vue + element UI 制作问答机器人_vue3 element ui开发智能对话

vue3 element ui开发智能对话
  1. <template>
  2. <div class="box">
  3. <div class="title">
  4. <img src="" alt class="logo" />
  5. <span class="title-hn">人工客服</span>
  6. </div>
  7. <div id="content" class="content">
  8. <div v-for="(item,index) in info" :key="index">
  9. <div class="info_r info_default" v-if="item.type == 'leftinfo'">
  10. <span class="circle circle_r"></span>
  11. <div class="con_r con_text">
  12. <div>{{item.content}}</div>
  13. <div v-for="(item2,index) in item.question" :key="index">
  14. <div class="con_que" @click="clickRobot(item2.content,item2.id)">
  15. <div class="czkj-question-msg">
  16. {{item2.index}}
  17. {{item2.content}}
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. <div class="time_r">{{item.time}}</div>
  23. </div>
  24. <div class="info_l" v-if="item.type == 'rightinfo'">
  25. <div class="con_r con_text">
  26. <span class="con_l">{{item.content}}</span>
  27. <span class="circle circle_l">
  28. <img src class="pic_l" />
  29. </span>
  30. </div>
  31. <div class="time_l">{{item.time}}</div>
  32. </div>
  33. </div>
  34. </div>
  35. <div class="setproblem">
  36. <textarea
  37. placeholder="请输入您的问题..."
  38. style="height: 68px;width: 100%;resize:none;padding-right:80px;outline: none;border-color:#ccc;border-radius:5px;"
  39. id="text"
  40. v-model="customerText"
  41. @keyup.enter="sentMsg()"
  42. ></textarea>
  43. <el-button @click="sentMsg()" class="buttonsend">
  44. <span style="vertical-align: 4px;">发送</span>
  45. </el-button>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. export default {
  51. data() {
  52. return {
  53. customerText: "",
  54. info: [
  55. {
  56. type: 'leftinfo',
  57. time: this.getTodayTime(),
  58. name: "robot",
  59. content:"您好,欢迎使用!",
  60. question: [
  61. { id: 1, content: "客户资料完善后是由谁来审批", index: 1 },
  62. { id: 2, content: "客户资料审批一直不通过", index: 2 },
  63. { id: 3, content: "客户资料审批需要多长时间", index: 3 },
  64. {id: 4,content: "注册网站时需要一次填写完所有的客户资料吗",index: 4 },
  65. { id: 5, content: "注册时使用的手机号变更怎么办", index: 5 }
  66. ]
  67. }
  68. ],
  69. timer: null,
  70. robotQuestion: [
  71. { id: 1, content: "客户资料完善后是由谁来审批", index: 1 },
  72. { id: 2, content: "客户资料审批一直不通过", index: 2 },
  73. { id: 3, content: "客户资料审批需要多长时间", index: 3 },
  74. { d: 4, content: "注册网站时需要一次填写完所有的客户资料吗", index: 4 },
  75. { id: 5, content: "注册时使用的手机号变更怎么办", index: 5 },
  76. ],
  77. robotAnswer: [
  78. { id: 1,content:"答案客户资料完善后是由谁来审批,答案客户资料完善后是由谁来审批,答案客户资料完善后是由谁来审批",index: 1},
  79. { id: 2, content: "测试", index: 2 },
  80. { id: 3, content: "测试", index: 3 },
  81. { id: 4,content: "3333333",index: 4 },
  82. { id: 5, content: "44444444", index: 5 },
  83. ]
  84. }
  85. },
  86. created() {
  87. this.showTimer();
  88. },
  89. methods: {
  90. // 用户发送消息
  91. sentMsg() {
  92. clearTimeout(this.timer)
  93. this.showTimer()
  94. let text = this.customerText.trim()
  95. if (text != '') {
  96. var obj = {
  97. type: 'rightinfo',
  98. time: this.getTodayTime(),
  99. content: text,
  100. }
  101. this.info.push(obj)
  102. this.appendRobotMsg(this.customerText)
  103. this.customerText = ''
  104. this.$nextTick(() => {
  105. var contentHeight = document.getElementById('content')
  106. contentHeight.scrollTop = contentHeight.scrollHeight
  107. })
  108. }
  109. },
  110. // 机器人回答消息
  111. appendRobotMsg(text) {
  112. clearTimeout(this.timer)
  113. this.showTimer()
  114. text = text.trim()
  115. let answerText = ''
  116. let flag;
  117. for (let i = 0; i < this.robotAnswer.length; i++) {
  118. if (this.robotAnswer[i].content.indexOf(text) != -1) {
  119. flag = true
  120. answerText = this.robotAnswer[i].content
  121. break
  122. }
  123. }
  124. if (flag) {
  125. let obj = {
  126. type: "leftinfo",
  127. time: this.getTodayTime(),
  128. name: "robot",
  129. content: answerText,
  130. question: [],
  131. }
  132. this.info.push(obj)
  133. } else {
  134. answerText = "您可能想问:"
  135. let obj = {
  136. type: 'leftinfo',
  137. time: this.getTodayTime(),
  138. name: "robot",
  139. content: answerText,
  140. question: this.robotQuestion,
  141. }
  142. this.info.push(obj)
  143. }
  144. this.$nextTick(() => {
  145. var contentHeight = document.getElementById('content')
  146. contentHeight.scrollTop = contentHeight.scrollHeight
  147. })
  148. },
  149. sentMsgById(val, id) {
  150. clearTimeout(this.timer)
  151. this.showTimer()
  152. let robotById = this.robotAnswer.filter((item) => {
  153. return item.id == id;
  154. })
  155. let obj_l = {
  156. type: 'leftinfo',
  157. time: this.getTodayTime(),
  158. name: 'robot',
  159. content: robotById[0].content,
  160. question: [],
  161. };
  162. let obj_r = {
  163. type: 'rightinfo',
  164. time: this.getTodayTime(),
  165. name: 'robot',
  166. content: val,
  167. question: [],
  168. };
  169. this.info.push(obj_r)
  170. this.info.push(obj_l)
  171. this.$nextTick(() => {
  172. var contentHeight = document.getElementById('content')
  173. contentHeight.scrollTop = contentHeight.scrollHeight
  174. })
  175. },
  176. // 点击机器人的单个问题
  177. clickRobot(val, id) {
  178. this.sentMsgById(val, id)
  179. },
  180. // 结束语
  181. endMsg() {
  182. let happyEnding = {
  183. type: 'leftinfo',
  184. time: this.getTodayTime(),
  185. content: "退出",
  186. question: [],
  187. };
  188. this.info.push(happyEnding)
  189. this.$nextTick(() => {
  190. var contentHeight = document.getElementById('content')
  191. contentHeight.scrollTop = contentHeight.scrollHeight
  192. })
  193. },
  194. showTimer() {
  195. this.timer = setTimeout(this.endMsg, 1000*60)
  196. },
  197. getTodayTime() {
  198. // 获取当前时间
  199. var day = new Date()
  200. let seconds = day.getSeconds()
  201. if (seconds < 10) {
  202. seconds = "0" + seconds
  203. } else {
  204. seconds = seconds
  205. }
  206. let minutes = day.getMinutes()
  207. if (minutes < 10) {
  208. minutes = "0" + minutes
  209. } else {
  210. minutes = minutes
  211. }
  212. let time =
  213. day.getFullYear() +
  214. "-" +
  215. (day.getMonth() + 1) +
  216. "-" +
  217. day.getDate() +
  218. " " +
  219. day.getHours() +
  220. ":" +
  221. minutes +
  222. ":" +
  223. seconds
  224. return time
  225. }
  226. }
  227. }
  228. </script>
  229. <style scoped>
  230. .box {
  231. width: 100%;
  232. height: 500px;
  233. background-color: #fafafa;
  234. position: relative;
  235. padding: 20px;
  236. }
  237. #content {
  238. height: 340px;
  239. overflow-y: scroll;
  240. font-size: 14px;
  241. width: 100%;
  242. }
  243. .circle {
  244. display: inline-block;
  245. width: 34px;
  246. height: 34px;
  247. border-radius: 50%;
  248. background-color: #eff1f3;
  249. }
  250. .con_text {
  251. color: #333;
  252. margin-bottom: 5px;
  253. }
  254. .con_que {
  255. color: #1c88ff;
  256. height: 30px;
  257. line-height: 30px;
  258. cursor: pointer;
  259. }
  260. .info_r {
  261. position: relative;
  262. }
  263. .circle_r {
  264. position: absolute;
  265. left: 0%;
  266. }
  267. .pic_r {
  268. width: 17px;
  269. height: 17px;
  270. margin: 8px;
  271. }
  272. .con_r {
  273. display: inline-block;
  274. width: 55%;
  275. min-height: 55px;
  276. background-color: #e2e2e2;
  277. border-radius: 6px;
  278. padding: 10px;
  279. margin-left: 40px;
  280. }
  281. .time_r {
  282. margin-left: 45px;
  283. color: #999999;
  284. font-size: 12px;
  285. }
  286. .info_l {
  287. text-align: right;
  288. color: #ffffff;
  289. color: #3163C5;
  290. margin-top: 10px;
  291. }
  292. .pic_l {
  293. width: 13px;
  294. height: 17px;
  295. margin: 8px 10px;
  296. }
  297. .time_l {
  298. margin-right: 45px;
  299. color: #999999;
  300. font-size: 12px;
  301. margin-top: 5px;
  302. }
  303. .con_l {
  304. display: inline-block;
  305. width: 220px;
  306. min-height: 51px;
  307. background-color: #3163C5;
  308. border-radius: 6px;
  309. padding: 10px;
  310. text-align: left;
  311. color: #fff;
  312. margin-right: 5px;
  313. }
  314. #question {
  315. cursor: pointer;
  316. }
  317. .setproblem {
  318. width: 90%;
  319. height: 68px;
  320. background-color: #ffffff;
  321. position: relative;
  322. margin-top: 20px;
  323. padding-bottom: 20px;
  324. }
  325. .setproblem textarea {
  326. margin-bottom: 10px;
  327. color: #999999;
  328. padding: 10px;
  329. box-sizing: border-box;
  330. }
  331. .buttonsend {
  332. background: #3163C5;
  333. opacity: 1;
  334. border-radius: 4px;
  335. font-size: 10px;
  336. color: #ffffff;
  337. position: absolute;
  338. right: -10%;
  339. top: 30%;
  340. cursor: pointer;
  341. border: none;
  342. }
  343. .czkj-item-title {
  344. line-height: 25px;
  345. border-bottom: 1px solid #ccc;
  346. padding-bottom: 5px;
  347. margin-bottom: 5px;
  348. }
  349. .czkj-item-question {
  350. cursor: pointer;
  351. display: block;
  352. padding: 8px;
  353. position: relative;
  354. border-bottom: 1px dashed #ccc;
  355. line-height: 20px;
  356. min-height: 20px;
  357. overflow: hidden;
  358. }
  359. .czkj-question-msg {
  360. float: left;
  361. font-size: 14px;
  362. color: #3163C5;
  363. }
  364. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/359203
推荐阅读