vue實(shí)現(xiàn)的微信機(jī)器人聊天功能案例【附源碼下載】
本文實(shí)例講述了vue實(shí)現(xiàn)的微信機(jī)器人聊天功能。分享給大家供大家參考,具體如下:
先看效果:
實(shí)現(xiàn)過程:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HTML5模擬微信聊天界面</title> <style> /**重置標(biāo)簽?zāi)J(rèn)樣式*/ * { margin: 0; padding: 0; list-style: none; font-family: '微軟雅黑' } #container { width: 450px; height: 780px; background: #eee; margin: 80px auto 0; position: relative; box-shadow: 20px 20px 55px #777; } .header { background: #000; height: 40px; color: #fff; line-height: 34px; font-size: 20px; padding: 0 10px; } .footer { width: 430px; height: 50px; background: #666; position: absolute; bottom: 0; padding: 10px; } .footer input { width: 360px; height: 45px; outline: none; font-size: 20px; text-indent: 10px; position: absolute; border-radius: 6px; right: 80px; } .footer span { display: inline-block; width: 62px; height: 48px; background: #ccc; font-weight: 900; line-height: 45px; cursor: pointer; text-align: center; position: absolute; right: 10px; border-radius: 6px; } .footer span:hover { color: #fff; background: #999; } /* #user_face_icon { display: inline-block; background: red; width: 60px; height: 60px; border-radius: 30px; position: absolute; bottom: 6px; left: 14px; cursor: pointer; overflow: hidden; } */ img { width: 60px; height: 60px; } .content { font-size: 20px; width: 435px; height: 662px; overflow: auto; padding: 5px; } .content li { margin-top: 10px; padding-left: 10px; width: 412px; display: block; clear: both; overflow: hidden; } .content li img { float: left; } .content li span { background: #7cfc00; padding: 10px; border-radius: 10px; float: left; margin: 6px 10px 0 10px; max-width: 310px; border: 1px solid #ccc; box-shadow: 0 0 3px #ccc; } .content li img.imgleft { float: left; } .content li img.imgright { float: right; } .content li span.spanleft { float: left; background: #fff; } .content li span.spanright { float: right; background: #7cfc00; } </style> </head> <body> <div id="container"> <div class="header"> <span style="float: left;">微信聊天界面</span> <span style="float: right;">14:21</span> </div> <ul class="content"> <li v-for="(item, index) in messageList" > <img :src="'./img/'+(item.isSelf?'r.png':'l.png')" :class="'img'+(item.isSelf?'right':'left')"> <span :class="'span'+(item.isSelf?'right':'left')">{{item.message}}</span> </li> </ul> <div class="footer"> <!-- 添加輸入內(nèi)容 --> <input id="text" type="text" placeholder="說點(diǎn)什么吧..." v-model="inputValue" @keyup.enter="chat"> <!-- 給發(fā)送也綁定一個(gè)事件 --> <span id="btn" @click="chat">發(fā)送</span> </div> </div> <!-- 導(dǎo)入vue --> <script src="./lib/vue.js"></script> <!-- 導(dǎo)入jQuery --> <script src="./lib/jquery-1.12.4.min.js"></script> <!-- 開始代碼 --> <script> /* 思路分析: 一.定義聊天信息數(shù)組格式 [ { message:'', isSelf:true(自己)/false(機(jī)器人) } ] 二.獲取自己輸入內(nèi)容,將內(nèi)容渲染到頁面 三.獲取機(jī)器人接口內(nèi)容,也將內(nèi)容渲染到頁面 */ //一: let app = new Vue({ el: "#container", data: { //輸入內(nèi)容,雙向數(shù)據(jù)綁定 inputValue: '', //聊天窗口內(nèi)容 messageList: [] }, methods: { chat() { // console.log(this.inputValue); // console.log(this); // 二.獲取自己輸入內(nèi)容,將內(nèi)容渲染到頁面 this.messageList.push({ message: this.inputValue, isSelf: true }) // 三.獲取機(jī)器人接口內(nèi)容,也將內(nèi)容渲染到頁面 $.ajax({ url:'http://www.tuling123.com/openapi/api', data:{ userid:1,//添加id,實(shí)現(xiàn)上下文連貫 key:'b6ef78a0c1f24fee90d2317139b9c3d5', info:this.inputValue }, // 注意使用箭頭函數(shù),不然里面的this會發(fā)生變化 success:(obj)=>{ console.log(obj); // 三.獲取機(jī)器人接口內(nèi)容,也將內(nèi)容渲染到頁面 this.messageList.push({ message:obj.text, isSelf:false }) } }) this.inputValue=''; //最后清除文本框 } }, }) </script> </body> </html>
附:gethub源碼地址:https://github.com/huanggengzhong/jiqiren
還可以點(diǎn)擊此處本站下載。
希望本文所述對大家vue.js程序設(shè)計(jì)有所幫助。
- Vue+express+Socket實(shí)現(xiàn)聊天功能
- Vue實(shí)現(xiàn)聊天界面
- vue實(shí)現(xiàn)web在線聊天功能
- vue+web端仿微信網(wǎng)頁版聊天室功能
- Vue.js仿微信聊天窗口展示組件功能
- vue + socket.io實(shí)現(xiàn)一個(gè)簡易聊天室示例代碼
- 基于Vue2實(shí)現(xiàn)的仿手機(jī)QQ單頁面應(yīng)用功能(接入聊天機(jī)器人 )
- Vue Cli 3項(xiàng)目使用融云IM實(shí)現(xiàn)聊天功能的方法
- 基于vue和websocket的多人在線聊天室
- Vue+ssh框架實(shí)現(xiàn)在線聊天
相關(guān)文章
Vue之beforeEach非登錄不能訪問的實(shí)現(xiàn)(代碼親測)
這篇文章主要介紹了Vue之beforeEach非登錄不能訪問的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07vue實(shí)現(xiàn)調(diào)取手機(jī)攝像頭和相冊功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)調(diào)取手機(jī)攝像頭和相冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09cesium開發(fā)之如何在vue項(xiàng)目中使用cesium,使用離線地圖資源
這篇文章主要介紹了cesium開發(fā)之如何在vue項(xiàng)目中使用cesium,使用離線地圖資源問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04基于vue 動(dòng)態(tài)加載圖片src的解決方法
下面小編就為大家分享一篇基于vue 動(dòng)態(tài)加載圖片src的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue中數(shù)據(jù)字典dicts的簡單說明和用法介紹
這篇文章主要給大家介紹了關(guān)于vue中數(shù)據(jù)字典dicts的簡單說明和用法的相關(guān)資料,如果您想在Vue中使用字典查詢,您可以使用Vue的計(jì)算屬性和方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01