首頁  >  文章  >  web前端  >  vuejs怎麼實作聊天介面

vuejs怎麼實作聊天介面

藏色散人
藏色散人原創
2021-09-24 16:43:053860瀏覽

vuejs實作聊天介面的方法:1、透過執行「npm install」安裝dependencies;2、透過「scrollLoader.vue」實作滾動載入資料;3、修改main.js;4、設定App.vue中的參數即可。

vuejs怎麼實作聊天介面

本文操作環境:Windows7系統、vue2.9.6版,DELL G3電腦。

vuejs怎麼實作聊天介面?

Vue.js仿微信聊天視窗展示元件功能

#原始碼:https://github.com/doterlin/vue-wxChat

#示範網址:https://doterlin.github.io/vue-wxChat/

執行

##

# install dependencies
npm install
# serve with hot reload at localhost:8080
npm run dev
# build for production with minification
npm run build

介紹

支援文字和圖片的展示(後續將支援對語音類別的展示)。 支援滾動載入數據,其中捲動載入依賴我另外一個元件scrollLoader.vue(《Vue.js上下滾動載入元件》)。

支援QQ表情,為了能使用表情請全域註冊指令v-emotion,我在main.js做了實作;程式碼如下:

function toEmotion(text, isNoGif){
 var list = ['微笑', '撇嘴', '色', '发呆', '得意', '流泪', '害羞', '闭嘴', '睡', '大哭', '尴尬', '发怒', '调皮', '呲牙', '惊讶', '难过', '酷', '冷汗', '抓狂', '吐', '偷笑', '愉快', '白眼', '傲慢', '饥饿', '困', '惊恐', '流汗', '憨笑', '大兵', '奋斗', '咒骂', '疑问', '嘘', '晕', '折磨', '衰', '骷髅', '敲打', '再见', '擦汗', '抠鼻', '鼓掌', '糗大了', '坏笑', '左哼哼', '右哼哼', '哈欠', '鄙视', '委屈', '快哭了', '阴险', '亲亲', '吓', '可怜', '菜刀', '西瓜', '啤酒', '篮球', '乒乓', '咖啡', '饭', '猪头', '玫瑰', '凋谢', '示爱', '爱心', '心碎', '蛋糕', '闪电', '炸弹', '刀', '足球', '瓢虫', '便便', '月亮', '太阳', '礼物', '拥抱', '强', '弱', '握手', '胜利', '抱拳', '勾引', '拳头', '差劲', '爱你', 'NO', 'OK', '爱情', '飞吻', '跳跳', '发抖', '怄火', '转圈', '磕头', '回头', '跳绳', '挥手', '激动', '街舞', '献吻', '左太极', '右太极', '嘿哈', '捂脸', '奸笑', '机智', '皱眉', '耶', '红包', '鸡'];
 if (!text) {
  return text;
 }
 text = text.replace(/\[[\u4E00-\u9FA5]{1,3}\]/gi, function(word){
  var newWord = word.replace(/\[|\]/gi,'');
  var index = list.indexOf(newWord);
  var backgroundPositionX = -index * 24
  var imgHTML = '';
  if(index<0){
   return word;
  }
  if (isNoGif) {
   if(index>104){
    return word;
   }
   imgHTML = `<i class="static-emotion" style="background:url(https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/default218877.gif) ${backgroundPositionX}px 0;"></i>`
  } else {
   var path = index>104 ? &#39;/img&#39; : &#39;https://res.wx.qq.com/mpres/htmledition/images/icon&#39;;
   imgHTML = `![](${path}/emotion/${index}.gif)`
  }
  return imgHTML;
 });
 return text;
}
Vue.directive(&#39;emotion&#39;, {
 bind: function (el, binding) {
  el.innerHTML = toEmotion(binding.value)
 }
});

如何使用?

參數已經在元件中做了說明,並在

App.vue

中做了示範:

參數和說明:

微信聊天視覺化元件

依賴scrollLoader元件, 依賴指令v-emotion(實作請查看main.js)

##參數:

width 元件寬度,預設450

wrapBg 外層父元素背景顏色,預設#efefef

# maxHeight 展示視窗最高高度, 預設900

contactAvatarUrl 好友頭像urlownerAvatarUrl 微信主人頭像url

ownerNickname 微信主人暱稱######getUpperData (必需)當捲動到上方時載入資料的方法,傳回值要為Promise對象,resolve的結構同data######getUnderData (必要)當捲動到下方時載入資料的方法,傳回值同上####### data (必要)傳入初始化數據,結構如下:###
[{
 direction: 2, //为2表示微信主人发出的消息,1表示联系人
 id: 1, //根据这个来排序消息
 type: 1, //1为文本,2为图片
 content: &#39;你好!![呲牙]&#39;, //当type为1时这里是文本消息,当type2为2时这里要存放图片地址;后续会支持语音的显示
 ctime: new Date().toLocaleString() //显示当前消息的发送时间
},
{
 direction: 1,
 id: 2,
 type: 1,
 content: &#39;你也好。[害羞]&#39;,
 ctime: new Date().toLocaleString()
}]
######### 完整的使用實例 ############效果:https://doterlin.github .io/vue-wxChat/######程式碼:###
//主文件,对wxChat的用法做示例
<template>
<wxChat 
 :data="wxChatData"
 :showShade="false"
 contactNickname="简叔"
 :getUpperData="getUpperData"
 :getUnderData="getUnderData"
 :ownerAvatarUrl="ownerAvatarUrl"
 :contactAvatarUrl="contactAvatarUrl"
 :width="420">
</wxChat>
</template>
<script>
import wxChat from &#39;./components/wxChat.vue&#39;
export default {
 name: &#39;app&#39;,
 data () {
 return {
  upperTimes: 0,
  underTimes: 0,
  upperId: 0,
  underId: 6,
  ownerAvatarUrl: &#39;./src/assets/avatar1.png&#39;,
  contactAvatarUrl: &#39;./src/assets/avatar2.png&#39;,
  wxChatData: [{
  direction: 2,
  id: 1,
  type: 1,
  content: &#39;你好!![呲牙]&#39;,
  ctime: new Date().toLocaleString()
  },
  {
  direction: 1,
  id: 2,
  type: 1,
  content: &#39;你也好。[害羞]&#39;,
  ctime: new Date().toLocaleString()
  },
  {
  direction: 2,
  id: 3,
  type: 1,
  content: &#39;这是我的简历头像:&#39;,
  ctime: new Date().toLocaleString()
  },
  {
  direction: 2,
  id: 4,
  type: 2,
  content: &#39;./src/assets/wyz.jpg&#39;,
  ctime: new Date().toLocaleString()
  },
  {
  direction: 1,
  id: 5,
  type: 1,
  content: &#39;你开心就好。[微笑]&#39;,
  ctime: new Date().toLocaleString()
  }]
 }
 },
 components:{wxChat},
 methods:{
 //向上滚动加载数据
 getUpperData(){
  var me = this;
  // 这里为模拟异步加载数据
  // 实际上你可能要这么写:
  // return axios.get(&#39;xxx&#39;).then(function(result){
  //  return result; //result的格式需要类似下面resolve里面的数组
  // })
  return new Promise(function(resolve){
  setTimeout(function(){
   //模拟加载完毕
   if(me.upperTimes>3){
   return resolve([]);
   }
   //加载数据
   resolve([{
    direction: 2,
    id: me.upperId-1,
    type: 1,
    content: &#39;向上滚动加载第 &#39; + me.upperTimes +&#39; 条!&#39;,
    ctime: new Date().toLocaleString()
   },
   {
    direction: 1,
    id: me.upperId-2,
    type: 1,
    content: &#39;向上滚动加载第 &#39; + me.upperTimes +&#39; 条!&#39;,
    ctime: new Date().toLocaleString()
   }]
   )
  }, 1000);
  me.upperId= me.upperId+2;
  me.upperTimes++;
  })
 },
 getUnderData(){
  var me = this;
  //意义同getUpperData()
  return new Promise(function(resolve){
  setTimeout(function(){
   //模拟加载完毕
   if(me.underTimes>3){
   return resolve([]);
   }
   //加载数据
   resolve(
   [{
    direction: 1,
    id: me.underId+1,
    type: 1,
    content: &#39;向下滚动加载第 &#39; + me.underTimes +&#39; 条!&#39;,
    ctime: new Date().toLocaleString()
   },
   {
    direction: 2,
    id: me.underId+2,
    type: 1,
    content: &#39;向下滚动加载第 &#39; + me.underTimes +&#39; 条!&#39;,
    ctime: new Date().toLocaleString()
   }]
   )
  }, 1000);
  me.underId = me.underId+2;
  me.underTimes++;
  })
 }
 }
}
</script>
<style>
*{
 margin: 0;
 padding: 0;
}
#app {
 font-family: &#39;Avenir&#39;, Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
}
h1, h2 {
 font-weight: normal;
}
ul {
 list-style-type: none;
 padding: 0;
}
li {
 display: inline-block;
}
</style>
###歡迎start:######https://github.com/doterlin/vue-wxChat####### ######推薦:《###最新的5個vue.js影片教學精選###》#######

以上是vuejs怎麼實作聊天介面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn