很多小伙伴在学习vue的时候,会遇到很多问题,所以,我用vue2写了一个类似于客服聊天的机器人自动聊天,以下是全部代码,文中的提示音频和图片在我的博客里面找
<style>
body {
margin: 0 auto;
}
.chat {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
background-color: #f5f5f5;
}
input {
border: none;
outline: none;
background-color: transparent;
padding: 0;
display: block;
width: 100%;
height: 100%;
color: #444;
}
.ipt {
height: 30px;
flex: 1;
background-color: #f5f5f5;
padding: 0 5px;
margin-right: 10px;
}
.chat-send {
position: absolute;
right: 0;
left: 0;
bottom: 0;
height: 50px;
background-color: #fff;
padding: 0 10px;
display: flex;
align-items: center;
}
.send-btn {
width: 60px;
text-align: center;
border-radius: 5px;
height: 30px;
line-height: 30px;
background-color: rgb(33, 184, 19);
color: #fff;
font-size: 14px;
}
.chat-content {
margin-bottom: 40px;
}
.chat-content::after {
display: block;
content: "";
clear: both;
}
.left-box {
float: left;
}
.user-img {
float: left;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #ddd;
margin-right: 10px;
position: relative;
top: 15px;
overflow: hidden;
}
.user-msg {
float: left;
padding: 10px;
background-color: #e8e8e8;
font-size: 14px;
border-radius: 4px;
max-width: 200px;
}
.chat-box {
padding: 10px 10px 0;
}
.caret {
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
width: 0;
height: 0;
position: absolute;
top: 8px;
}
.caret-left {
border-left: 8px solid transparent;
border-right: 8px solid #e8e8e8;
left: -16px;
}
.caret-right {
border-left: 8px solid rgb(110, 216, 110);
border-right: 8px solid transparent;
right: -16px;
}
.right-box {
float: right;
}
.user-img-right {
margin-right: 0;
margin-left: 10px;
}
.my-msg {
background-color: rgb(110, 216, 110);
float: right;
}
.content-box {
float: left;
margin-top: 10px;
}
.user {
color: #666;
font-size: 12px;
clear: right;
margin-bottom: 2px;
}
.chat-box {
height: calc(100% - 50px);
overflow-y: auto;
}
.user-my{
text-align: right;
}
.auto-img{
width: 100%;
display: block;
}
.chat-data::after{
content: '';
display: block;
clear: both;
}
</style>
</head>
<body>
<div class="chat" id="app">
<audio ref="au" src="./audio/微信收到消息提示音.mp3"></audio>
<div class="chat-box">
<div class="chat-data" v-for="item in chatRecord">
<!-- 机器人 -->
<div v-if="item.user === 'robot'" class="left-box">
<div class="user-img">
<img class="auto-img" src="./images/robot.jpeg" alt="">
</div>
<div class="content-box">
<div class="user user-robot">小爱同学</div>
<div class="user-msg">
<i class="caret caret-left"></i>
<div>{{item.content}}</div>
</div>
</div>
</div>
<!-- 我 -->
<div v-else-if="item.user === 'my'" class="right-box">
<div class="content-box">
<div class="user user-my">我</div>
<div class="user-msg my-msg">
<i class="caret caret-right"></i>
<div>{{item.content}}</div>
</div>
</div>
<div class="user-img user-img-right">
<img class="auto-img" src="./images/my.webp" alt="">
</div>
</div>
</div>
</div>
<div class="chat-send">
<div class="ipt">
<input type="text" placeholder="输入发送内容" v-model="msg" />
</div>
<div class="send-btn" @click="sendMsg()">发送</div>
</div>
</div>
<script src="./js/vue.js"></script>
<script>
new Vue({
el: '#app',
data: {
//用户输入的信息
msg: '',
//辅助监听发送消息
count: 0,
//聊天记录
chatRecord: [],
//机器人回复样本
robotData: {
'F1': '我在广州从化',
'F2': '可在我的->设置->个人中心->修改密码'
},
//音频实例
audio: null
},
//created生命周期钩子: 当data数据生成后触发执行
created() {
console.log('当data数据生成后触发执行');
//获取audio
//$nextTick: 用于首次渲染页面获取DOM节点
this.$nextTick(() => {
// console.log('$nextTick方法执行');
// console.log('this.$refs.au ==> ', this.$refs.au);
//记录音频实例
this.audio = this.$refs.au;
})
},
methods: {
//用户主动发送的消息
sendMsg() {
//获取用户输入的内容
console.log('this.msg ==> ', this.msg);
//保存用户发送的消息
this.chatRecord.push({
content: this.msg,
user: 'my'
})
this.count++;
console.log('this.chatRecord ==> ', this.chatRecord);
}
},
watch: {
count() {
console.log('this.msg ==> ', this.msg);
//获取样本回复
let currentContent = this.robotData[this.msg];
if (!currentContent) {
currentContent = '此问题不在小爱同学回答的范围内';
}
this.msg = '';
//小爱同学发送的消息
setTimeout(() => {
this.chatRecord.push({content: currentContent, user: 'robot'});
this.audio.play();
}, 600)
}
}
})
</script>