简单的前端客服聊天
html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>客服聊天</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="stylesheet" href="../layui-src/dist/css/layui.css" />
<script src="../js/library/hio.js"></script>
<style>
/*公共样式*/
html{font-family:"Helvetica Neue",Helvetica,STHeiTi,sans-serif;-webkit-text-size-adjust:100%;-moz-text-size-adjust:100%;-ms-text-size-adjust:100%;}
body{-webkit-overflow-scrolling:touch;margin:0;}
ul{margin:0;padding:0;list-style:none;outline:none;}
dl,dd{margin:0;}
a{display:inline-block;margin:0;padding:0;text-decoration:none;background:transparent;outline:none;color:#000;}
a:link,a:visited,a:hover,a:active{text-decoration:none;color:currentColor;}
a,dt,dd{-webkit-touch-callout:none;-webkit-tap-highlight-color:transparent;}
img{border:0;}
p{margin:0;}
input,button,select,textarea{margin:0;padding:0;border:0;outline:0;background-color:transparent;}
/*页面公共样式*/
body {
position: relative;
}
.dialogue-wrapper {
font-size: 14px;
color: #fff;
}
.dialogue-wrapper .dialogue-support-btn .dialogue-support-icon {
position: relative;
display: inline-block;
margin-bottom: -2px;
width: 20px;
height: 16px;
border-radius: 4px;
background-color: #fff;
}
.dialogue-wrapper .dialogue-support-btn .dialogue-support-icon:before {
content: '';
position: absolute;
left: 50%;
bottom: -6px;
margin-left: -3px;
width: 0;
height: 0;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 6px solid #fff;
}
.dialogue-wrapper .dialogue-support-btn .dialogue-support-line {
display: inline-block;
width: 100%;
height: 1px;
background-color: #ddd;
}
.dialogue-wrapper .dialogue-support-btn .dialogue-support-text {
padding: 5px 0;
letter-spacing: 4px;
writing-mode: vertical-rl;
-webkit-user-select: none;
}
/*底部客服对话框*/
.dialogue-wrapper .dialogue-main {
width: 100%;
height: 100%;
}
/*客服对话框头部*/
.dialogue-wrapper .dialogue-main .dialogue-header {
position: relative;
padding: 10px;
height: 80px;
background-color: #09853d;
}
.dialogue-wrapper .dialogue-main .dialogue-service-info {
margin-top: 8px;
height: 40px;
}
.dialogue-wrapper .dialogue-main .dialogue-service-img {
display: inline-block;
margin: 0 10px 0 20px;
width: 60px;
height: 60px;
text-align: center;
line-height: 40px;
vertical-align: middle;
color: #000;
border-radius: 50%;
box-shadow: 1px 1px 4px rgba(0, 0, 0, .2);
background-image: url("../img/publicImg/icon.png");
background-size: 200%;
background-position: 73% 96%;
background-repeat: no-repeat;
background-color: #fff;
}
.dialogue-wrapper .dialogue-main .dialogue-service-title {
display: inline-block;
vertical-align: middle;
}
.dialogue-wrapper .dialogue-main .dialogue-service-detail {
font-size: 12px;
}
/*客服对话框内容*/
.dialogue-wrapper .dialogue-main .dialogue-contain {
overflow-y: auto;
padding: 30px 10px;
height: 290px;
word-wrap: break-word;
background-color: #f9f9f9;
}
.dialogue-wrapper .dialogue-main .dialogue-text {
display: inline-block;
position: relative;
padding: 10px;
max-width: 50%;
line-height: 23px;
white-space: pre-wrap;
border: 1px solid #09853d;
border-radius: 4px;
background-color: #09853d;
box-sizing: border-box;
}
.dialogue-wrapper .dialogue-main .dialogue-text2 {
display: inline-block;
position: relative;
padding: 10px;
max-width: 50%;
line-height: 23px;
white-space: pre-wrap;
border: 1px solid #09853d;
border-radius: 4px;
background-color: #eee;
box-sizing: border-box;
color: #09853d;
}
.dialogue-wrapper .dialogue-main .dialogue-service-contain {
margin-bottom: 10px;
text-align: left;
}
.dialogue-wrapper .dialogue-main .dialogue-service-text {
margin-left: 20px;
}
.dialogue-wrapper .dialogue-main .dialogue-service-text:before {
content: '';
position: absolute;
top: 50%;
left: -10px;
width: 0;
height: 0;
border-top: 6px solid transparent;
border-bottom: 6px solid transparent;
border-right: 10px solid #09853d;
-webkit-transform: translate(0, -50%);
transform: translate(0, -50%);
}
.dialogue-wrapper .dialogue-main .dialogue-customer-contain {
margin-bottom: 10px;
text-align: right;
}
.dialogue-wrapper .dialogue-main .dialogue-customer-text {
margin-right: 20px;
}
.dialogue-wrapper .dialogue-main .dialogue-customer-text:after {
content: '';
position: absolute;
top: 50%;
right: -10px;
width: 0;
height: 0;
border-top: 6px solid transparent;
border-bottom: 6px solid transparent;
border-left: 10px solid #09853d;
-webkit-transform: translate(0, -50%);
transform: translate(0, -50%);
}
/*客服对话框底部与输入*/
.dialogue-wrapper .dialogue-main .dialogue-submit {
position: relative;
padding: 10px;
height: 80px;
color: #000;
word-wrap: break-word;
border-top: 1px solid #ddd;
box-sizing: border-box;
}
/*输入框*/
.dialogue-wrapper .dialogue-submit .dialogue-input-text {
overflow-y: auto;
display: inline-block;
padding: 5px 10px;
width: 100%;
height: 70px;
vertical-align: middle;
white-space: pre-wrap;
word-wrap: break-word;
resize: none;
/* border-right: 1px solid #ddd; */
box-sizing: border-box;
}
.dialogue-wrapper .dialogue-submit .dialogue-input-tools {
display: inline-block;
width: 80px;
height: 80px;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="dialogue-wrapper">
<!-- 对话框 -->
<div class="dialogue-main">
<div class="dialogue-header">
<div class="dialogue-service-info">
<i class="dialogue-service-img" sql="头像"></i>
<div class="dialogue-service-title">
<p class="dialogue-service-name">叮小咚 (在线)</p>
</div>
</div>
</div>
<div id="dialogue_contain" class="dialogue-contain">
<p class="dialogue-service-contain"><span class="dialogue-text dialogue-service-text">您好,我是您的人工智能客服叮小咚</span></p>
</div>
<div class="dialogue-submit">
<textarea id="dialogue_input" class="dialogue-input-text" placeholder="请输入您的问题,按Enter键提交(shift+Enter换行)"></textarea>
</div>
</div>
</div>
<script src="../layui-src/dist/layui.all.js"></script>
<script src="../js/qq.js"></script>
</body>
</html>
js
var urlVal = sessionStorage.getItem("urlVal");
// 模拟一些后端传输数据
// var serviceData = {
// 'robot': {
// 'name': 'robot001',
// 'dialogue': ['您好,我们官方联系电话023-68629315', '客服繁忙请留言', '您好,我是的您的专属客服'],
// 'welcome': '您好,机器人Odin客服为您服务'
// }
// };
// 获取到输入框dialogueInput;获取到聊天框dialogueContain;获取到发送内容不能为空dialogueHint;
var dialogueInput = document.getElementById('dialogue_input'),
dialogueContain = document.getElementById('dialogue_contain'),
btnOpen = document.getElementById('btn_open'),
btnClose = document.getElementById('btn_close'),
timer,
timerId,
shiftKeyOn = false; // 辅助判断shift键是否按住
// 输入框
dialogueInput.addEventListener('keydown', function(e) {
var e = e || window.event;
if (e.keyCode == 16) {
// shiftKeyOn = true;
shiftKeyOn = false;
}
if (shiftKeyOn) {
return true;
} else if (e.keyCode == 13 && dialogueInput.value == '') {
layer.tips('不能为空哦~', '#dialogue_input', {
tips: [1,"#09853d"],
anim: 6,
time:1000
});
return true;
} else if (e.keyCode == 13) {
var nodeP = document.createElement('p'),
nodeSpan = document.createElement('span');
nodeP.classList.add('dialogue-customer-contain');
nodeSpan.classList.add('dialogue-text2', 'dialogue-customer-text');
nodeSpan.innerHTML = dialogueInput.value;
nodeP.appendChild(nodeSpan);
dialogueContain.appendChild(nodeP);
// 多条聊天后增加高度
dialogueContain.scrollTop = dialogueContain.scrollHeight;
submitCustomerText(dialogueInput.value);
}
});
// 输入框内的事件
dialogueInput.addEventListener('keyup', function(e) {
var e = e || window.event;
if (e.keyCode == 16) {
shiftKeyOn = false;
return true;
}
if (!shiftKeyOn && e.keyCode == 13) {
// 输出内容,清空输入框
dialogueInput.value = null;
}
});
// 对话框事件
function submitCustomerText(text) {
console.log(text)
// AjaxGet("get", "/ask/?text="+text, reService)
Promise.all([
$.ajax({
url: urlVal + "/ask/?text="+text,
type: "get",
dataType: "json"
})
]).then(
resolve => { //成功的时候
reService(resolve);
},
reject => {
rejectTime(reject)
}
);
// code here 向后端发送text内容
// 模拟后端回复
// var num = Math.random() * 10;
// if (num <= 7) {
// getServiceText(serviceData);
// }
}
//前端模拟回复
// function getServiceText(data) {
// var serviceText = data.robot.dialogue,
// i = Math.floor(Math.random() * serviceText.length);
// // 回复区域
// var nodeP = document.createElement('p'),
// nodeSpan = document.createElement('span');
// // 给样式
// nodeP.classList.add('dialogue-service-contain');
// nodeSpan.classList.add('dialogue-text', 'dialogue-service-text');
// nodeSpan.innerHTML = serviceText[i];
// nodeP.appendChild(nodeSpan);
// dialogueContain.appendChild(nodeP);
// dialogueContain.scrollTop = dialogueContain.scrollHeight;
// }
function reService(data){
console.log(data);
let serviceText=data[0].response;
var nodeP = document.createElement('p'),
nodeSpan = document.createElement('span');
// 给样式
nodeP.classList.add('dialogue-service-contain');
nodeSpan.classList.add('dialogue-text', 'dialogue-service-text');
nodeSpan.innerHTML = serviceText;
nodeP.appendChild(nodeSpan);
dialogueContain.appendChild(nodeP);
dialogueContain.scrollTop = dialogueContain.scrollHeight;
}
// 渐隐
function fadeOut(obj) {
var n = 100;
var time = setInterval(function() {
if (n > 0) {
n -= 10;
obj.style.opacity = '0.' + n;
} else if (n <= 30) {
obj.style.opacity = '0';
clearInterval(time);
}
}, 10);
return true;
}
// 渐显
function fadeIn(obj) {
var n = 30;
var time = setInterval(function() {
if (n < 90) {
n += 10;
obj.style.opacity = '0.' + n;
} else if (n >= 80) {
obj.style.opacity = '1';
clearInterval(time);
}
}, 100);
return true;
}
js调用
// 在线客服
$(".icon-kefu").click (function(e) {
layui.use("layer", function() {
var layer = layui.layer;
layer.open({
type: 2,
title: "在线客服",
content: ["../../page/kefu.html", "no"],
skin: "kefu-class",
offset: "rb",
anim: 2,
area: ["400px", "600px"],
shade: 0,
maxmin: true,
});
});
})