vue实现在线客服功能(附完整代码)

您所在的位置:网站首页 易聊客服系统常用语添加 vue实现在线客服功能(附完整代码)

vue实现在线客服功能(附完整代码)

2023-03-09 22:24| 来源: 网络整理| 查看: 265

公司需要在网站实现一个在线客服的功能,开始是用的jq实现的,但是!!!把代码拿到vue里使用的时候,Jq的事件一直没有办法触发,各种百度都没有结果然后就放弃了,最后找到一种很好的解决方法,完美实现了功能!!!还有感谢我的外援,哈哈哈哈哈,一直是我的后盾!!!!!!!!哈哈哈哈哈

 

以下为vue部分

主要是循环info里的数据,通过type来判断显示机器人还是用户。

用户发送消息后直接push进数组里即可

人工客服 {{item.content}} {{item2.index}} {{item2.content}} {{item.time}} {{item.content}} {{item.time}} 发 送 export default { name: "onlineCustomer", components: {}, computed: {}, data() { return { customerText: "", info: [ { type: "leftinfo", time: this.getTodayTime(), name: "robot", content: "您好,欢迎使用网上营业厅,为保证账户安全,在会话过程中请勿透露您的账号、", question: [], }, ], timer: null, robotQuestion: [ { id: 1, content: "客户资料完善后是由谁来审批", index: 1 }, { id: 2, content: "客户资料审批一直不通过", index: 2 }, { id: 3, content: "客户资料审批需要多长时间", index: 3 }, { id: 4, content: "注册网站时需要一次填写完所有的客户资料吗", index: 4, }, { id: 5, content: "注册时使用的手机号变更怎么办", index: 5 }, ], robotAnswer: [ { id: 1, content: "答案客户资料完善后是由谁来审批,答案客户资料完善后是由谁来审批,答案客户资料完善后是由谁来审批", index: 1, }, { id: 2, content: "测试", index: 2 }, { id: 3, content: "测试", index: 3 }, { id: 4, content: "3333333", index: 4, }, { id: 5, content: "44444444", index: 5 }, ], }; }, created() { this.showTimer(); }, watch: {}, methods: { // 用户发送消息 sentMsg() { clearTimeout(this.timer); this.showTimer(); let text = this.customerText.trim(); if (text != "") { var obj = { type: "rightinfo", time: this.getTodayTime(), content: text, }; this.info.push(obj); this.appendRobotMsg(this.customerText); this.customerText = ""; this.$nextTick(() => { var contentHeight = document.getElementById("content"); contentHeight.scrollTop = contentHeight.scrollHeight; }); } }, // 机器人回答消息 appendRobotMsg(text) { clearTimeout(this.timer); this.showTimer(); text = text.trim(); let answerText = ""; let flag; for (let i = 0; i < this.robotAnswer.length; i++) { if (this.robotAnswer[i].content.indexOf(text) != -1) { flag = true; answerText = this.robotAnswer[i].content; break; } } if (flag) { let obj = { type: "leftinfo", time: this.getTodayTime(), name: "robot", content: answerText, question: [], }; this.info.push(obj); } else { answerText = "您可能想问:"; let obj = { type: "leftinfo", time: this.getTodayTime(), name: "robot", content: answerText, question: this.robotQuestion, }; this.info.push(obj); } this.$nextTick(() => { var contentHeight = document.getElementById("content"); contentHeight.scrollTop = contentHeight.scrollHeight; }); }, sentMsgById(val, id) { clearTimeout(this.timer); this.showTimer(); let robotById = this.robotAnswer.filter((item) => { return item.id == id; }); let obj_l = { type: "leftinfo", time: this.getTodayTime(), name: "robot", content: robotById[0].content, question: [], }; let obj_r = { type: "rightinfo", time: this.getTodayTime(), name: "robot", content: val, question: [], }; this.info.push(obj_r); this.info.push(obj_l); this.$nextTick(() => { var contentHeight = document.getElementById("content"); contentHeight.scrollTop = contentHeight.scrollHeight; }); }, // 点击机器人的单个问题 clickRobot(val, id) { this.sentMsgById(val, id); }, // 结束语 endMsg() { let happyEnding = { type: "leftinfo", time: this.getTodayTime(), content: "退下吧", question: [], }; this.info.push(happyEnding); this.$nextTick(() => { var contentHeight = document.getElementById("content"); contentHeight.scrollTop = contentHeight.scrollHeight; }); }, showTimer() { this.timer = setTimeout(this.endMsg, 1000*60); }, getTodayTime() { // 获取当前时间 var day = new Date(); let seconds = day.getSeconds(); if (seconds < 10) { seconds = "0" + seconds; } else { seconds = seconds; } let minutes = day.getMinutes(); if (minutes < 10) { minutes = "0" + minutes; } else { minutes = minutes; } let time = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate() + " " + day.getHours() + ":" + minutes + ":" + seconds; return time; }, }, mounted() {}, props: {}, destroyed() {}, }; .main { width: 100%; // height: 100vh; background: linear-gradient( 180deg, rgba(149, 179, 212, 1) 0%, rgba(74, 131, 194, 1) 100% ); overflow: hidden; .box { width: 100%; /* width: 680px; */ height: 500px; background-color: #fafafa; position: relative; padding: 1.25rem; #content { height: 340px; overflow-y: scroll; font-size: 14px; width: 100%; .circle { display: inline-block; width: 34px; height: 34px; border-radius: 50%; background-color: #eff1f3; } .con_text { color: #333; margin-bottom: 5px; } .con_que { color: #1c88ff; height: 30px; line-height: 30px; cursor: pointer; } .info_r { position: relative; .circle_r { position: absolute; left: 0%; } .pic_r { width: 17px; height: 17px; margin: 8px; } .con_r { display: inline-block; /* max-width: 253px; */ width: 55%; min-height: 55px; /* min-height: 20px; */ background-color: #e2e2e2; border-radius: 6px; padding: 10px; margin-left: 40px; } .time_r { margin-left: 45px; color: #999999; font-size: 12px; } } .info_l { text-align: right; // margin-right: 20px; color: #ffffff; color: #3163C5; margin-top: 10px; // .circle_l { // // vertical-align: -10px; // } .pic_l { width: 13px; height: 17px; margin: 8px 10px; } .time_l { margin-right: 45px; color: #999999; font-size: 12px; margin-top: 5px; } .con_l { display: inline-block; width: 220px; min-height: 51px; background-color: #3163C5; border-radius: 6px; padding: 10px; text-align: left; color: #fff; margin-right: 5px; } } #question { cursor: pointer; } } } } .setproblem { width: 100%; height: 68px; background-color: #ffffff; position: relative; margin-top: 3.75rem; } .setproblem textarea { color: #999999; padding: 10px; box-sizing: border-box; } .setproblem button { width: 5.875rem; height: 2.5rem; line-height: 2.5rem; background: #3163C5; opacity: 1; border-radius: 4px; font-size: 10px; color: #ffffff; position: absolute; right: 5%; top: 30%; cursor: pointer; border: none; } .czkj-item-title { line-height: 25px; border-bottom: 1px solid #ccc; padding-bottom: 5px; margin-bottom: 5px; } .czkj-item-question { cursor: pointer; display: block; padding: 8px; position: relative; border-bottom: 1px dashed #ccc; line-height: 20px; min-height: 20px; overflow: hidden; } .czkj-question-msg { float: left; font-size: 14px; color: #3163C5; }

原生部分。对比之下还是vue更舒服一点。

您好,欢迎使用网上营业厅,为保证账户安全, 在会话过程中请勿透露您的账号、密码等信息。 1 2 3 发 送 nHnSite.subServiceInit(); // nHnSite.initAbout(); window.onload = setWelcomeTime; // 打开页面默认展示问候语添加时间 function setWelcomeTime() { showTimer(); // 获取当前时间 var day = new Date(); let time = getTodayTime(); var commentLi = '' + time + ''; $('.info_default').append(commentLi); // 获取当前登录的用户名称 let city = $('#cityChoice').text(); // 拆分为简称 let cityJc = city.split('销售'); console.log(cityJc) cityShow = cityJc[0] + '销售智能客服'; $('.title-hn').text(cityShow); } // 发送消息 function sentMsg() { clearTimeout(timer); showTimer(); let time = getTodayTime(); let text = $('#text').val(); console.log(text) var commentLi = ''; commentLi = ''; commentLi += '' + text + ''; commentLi += ''; commentLi += '' + time + ''; commentLi += ''; $('.content').append(commentLi); $.ajax({ url: '/api/admin/reg/getNetInfo', type: 'get', contentType: "application/json; charset=utf-8", success: function (res) { console.log(res,'0000') // checkNetInfo(resultData); }, error: function (data) { // var resultData = { "status": 200, "data": { "id": "1", "province": "BJ", "url": "://www.chngyx.com.cn", "provinceName": "北京" }, "rel": true } // checkNetInfo(resultData); } }); // 发送过消息后将页面滚动到底部 var contentHeight = document.getElementById('content'); contentHeight.scrollTop = contentHeight.scrollHeight; appendRobotMsg(text) } // 选中问题后再次发送消息 function selMsg(text) { clearTimeout(timer); showTimer(); let time = getTodayTime(); var commentLi = ''; commentLi = ''; commentLi += '' + text + ''; commentLi += ''; commentLi += '' + time + ''; commentLi += ''; $('.content').append(commentLi); appendRobotMsg(text) // 发送过消息后将页面滚动到底部 var contentHeight = document.getElementById('content'); contentHeight.scrollTop = contentHeight.scrollHeight; } // 键盘回车事件 $(document).keypress(function (e) { if (e.keyCode == '13') { sentMsg(); } }); // 结束语 function endMsg() { let time = getTodayTime(); var commentLi = ''; commentLi = ''; commentLi += ''; commentLi += '' + '借宿了' + ''; commentLi += '' + time + ''; commentLi += ''; $('.content').append(commentLi); // 发送过消息后将页面滚动到底部 var contentHeight = document.getElementById('content'); contentHeight.scrollTop = contentHeight.scrollHeight; } function showTimer() { console.log(000) timer = setTimeout(endMsg, 5000 * 60); } //点击单个问答 $('.content').on('click', '.con_que', function (event) { let text = $(event)[0].currentTarget.innerText; console.log($(event)) selectAnswer(text) // var dataSet = $(event)[0].currentTarget.dataset; // var parent = $($(event)[0].currentTarget).parents('.czkj-msg'); // var flag = $(parent).data('choosed') // selectAnswer(parent, dataSet.index, dataSet.questionid, flag) }) //多个答案选择一个 function selectAnswer(text) { // parent, index, questionId, flag var questionObj = {}, parent = {} // for (var i = 0; i < questionArr.length; i++) { // for (var j = 0; j < questionArr[i].questionList.length; j++) { // if (questionArr[i].questionList[j].questionId === questionId) { // questionObj = questionArr[i].questionList[j] // parent = questionArr[i] // } // } // } selMsg(text) // sentMsg(questionObj.question) // appendRobotMsg(questionObj, questionObj.answer, parent) } // 获取当前时间 function getTodayTime() { // 获取当前时间 var day = new Date(); let seconds = day.getSeconds(); if (seconds < 10) { seconds = "0" + seconds; } else { seconds = seconds; } let minutes = day.getMinutes(); if (minutes < 10) { minutes = "0" + minutes; } else { minutes = minutes; } let time = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate() + " " + day.getHours() + ":" + minutes + ":" + seconds; return time } // 新增机器人回复 function appendRobotMsg(text) { console.log(text) // text = text.replace(/\n/g, '') text = text.trim(); let time = getTodayTime(); // 输入内容为空时提示 var questionList = [{ msg: '问题1,好嘞啊啊啊啊啊', questionId: '1', }, { msg: '问题2:我真的好嘞啊', questionId: '2', }]; let title = '嘎嘎嘎嘎嘎过过嘎嘎嘎嘎嘎' let commentLi; if (text == '1') { let time = getTodayTime() var questions = '' for (var i = 0; i < questionList.length; i++) { questions += '' + questionList[i].msg + '' } commentLi = '' + '' + '' + title + '' + questions + '' + '' + time + '' + '' }else if (text == ''){ commentLi = ''; commentLi += ''; commentLi += '' + '说人话' + ''; commentLi += '' + time + ''; commentLi += ''; }else { commentLi = ''; commentLi += ''; commentLi += '' + '这道题我不会换一个' + ''; commentLi += '' + time + ''; commentLi += ''; } $('.content').append(commentLi); $('#text').val(''); // 发送过消息后将页面滚动到底部 var contentHeight = document.getElementById('content'); contentHeight.scrollTop = contentHeight.scrollHeight; } 样式 .main { width: 100vw; height: 100vh; background:linear-gradient(180deg,rgba(149,179,212,1) 0%,rgba(74,131,194,1) 100%); overflow: hidden; } .box { width: 58%; /* width: 680px; */ height: 500px; background-color: #FAFAFA; border-radius:10px; margin:10vh auto; position: relative; } .title{ width: 100%; height: 10vh; background-color: #FFFFFF; border-radius:10px 10px 0 0 ; } .logo{ width: 50px; height: 40px; margin: 11px 0 0 25px; } .title .title-hn { font-size: 25px; color: #3F5267; vertical-align: 10px; } .setproblem{ width: 80%; height: 68px; background-color: #FFFFFF; border:1px solid rgba(221,221,221,1); border-radius:2px; position: absolute; bottom: 10%; left: 10%; } .setproblem textarea { color:#999999; padding: 10px; box-sizing: border-box; } .setproblem button { width:53px; height:23px; line-height: 23px; background:rgba(28,136,255,1); opacity:1; border-radius:2px; font-size: 10px; color: #FFFFFF; position: absolute; right: 5%; top: 30%; cursor: pointer; } .content { height: 300px; overflow-y:scroll; font-size: 14px; } .content .circle { display: inline-block; width: 34px; height: 34px; border-radius: 50%; background-color: #EFF1F3; } .content .circle_r { position: absolute; left: 0%; } .content .circle_l { vertical-align: -10px; } .content .info_r .pic_r{ width: 17px; height: 17px; margin: 8px; } .content .info_l .pic_l{ width: 13px; height: 17px; margin: 8px 10px; } .content .info_r{ margin-left: 20px; margin-top: 15px; position: relative; } .content .info_l{ text-align: right; margin-right: 20px; color: #FFFFFF; margin-top: 10px; } .content .con_r{ display: inline-block; /* max-width: 253px; */ width: 253px; min-height: 55px; /* min-height: 20px; */ background-color: #E3EAF2; border-radius: 6px; padding: 10px; margin-left: 40px; } .content .con_text { color: #333; margin-bottom: 5px; } .content .con_que { color: #1C88FF; height: 30px; line-height: 30px; cursor: pointer; } .content .con_l { display: inline-block; width: 253px; /* min-height: 20px; */ min-height: 51px; background-color: #1C88FF; border-radius: 6px; padding: 10px; text-align: left; color: #fff; margin-right: 5px; } .content .time_r { margin-left: 45px; color: #999999; font-size: 12px; } .content .time_l { margin-right: 45px; color: #999999; font-size: 12px; margin-top: 5px; } #question { cursor: pointer; } .czkj-item-title { line-height: 25px; border-bottom: 1px solid #ccc; padding-bottom: 5px; margin-bottom: 5px; } .czkj-item-question { cursor: pointer; display: block; padding: 8px; position: relative; border-bottom: 1px dashed #ccc; line-height: 20px; min-height: 20px; overflow: hidden; } .czkj-question-msg { float: left; font-size: 14px; color: #4aa4eb; }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3