溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue怎么實現(xiàn)微信聊天窗口展示組件功能

發(fā)布時間:2022-11-01 10:07:12 來源:億速云 閱讀:293 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Vue怎么實現(xiàn)微信聊天窗口展示組件功能”,在日常操作中,相信很多人在Vue怎么實現(xiàn)微信聊天窗口展示組件功能問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Vue怎么實現(xiàn)微信聊天窗口展示組件功能”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

運行

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

介紹

  • 支持文本和圖片的展示(后續(xù)將支持對語音類的展示)。

  • 支持滾動加載數(shù)據(jù),其中滾動加載依賴我另外一個組件scrollLoader.vue(《Vue.js上下滾動加載組件》)。

  • 支持QQ表情,為了能使用表情請全局注冊指令v-emotion,我在main.js做了實現(xiàn);代碼如下:

function toEmotion(text, isNoGif){
 var list = ['微笑', '撇嘴', '色', '發(fā)呆', '得意', '流淚', '害羞', '閉嘴', '睡', '大哭', '尷尬', '發(fā)怒', '調(diào)皮', '呲牙', '驚訝', '難過', '酷', '冷汗', '抓狂', '吐', '偷笑', '愉快', '白眼', '傲慢', '饑餓', '困', '驚恐', '流汗', '憨笑', '大兵', '奮斗', '咒罵', '疑問', '噓', '暈', '折磨', '衰', '骷髏', '敲打', '再見', '擦汗', '摳鼻', '鼓掌', '糗大了', '壞笑', '左哼哼', '右哼哼', '哈欠', '鄙視', '委屈', '快哭了', '陰險', '親親', '嚇', '可憐', '菜刀', '西瓜', '啤酒', '籃球', '乒乓', '咖啡', '飯', '豬頭', '玫瑰', '凋謝', '示愛', '愛心', '心碎', '蛋糕', '閃電', '炸彈', '刀', '足球', '瓢蟲', '便便', '月亮', '太陽', '禮物', '擁抱', '強(qiáng)', '弱', '握手', '勝利', '抱拳', '勾引', '拳頭', '差勁', '愛你', 'NO', 'OK', '愛情', '飛吻', '跳跳', '發(fā)抖', '慪火', '轉(zhuǎn)圈', '磕頭', '回頭', '跳繩', '揮手', '激動', '街舞', '獻(xiàn)吻', '左太極', '右太極', '嘿哈', '捂臉', '奸笑', '機(jī)智', '皺眉', '耶', '紅包', '雞'];
 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" ></i>`
  } else {
   var path = index>104 ? '/img' : 'https://res.wx.qq.com/mpres/htmledition/images/icon';
   imgHTML = `![](${path}/emotion/${index}.gif)`
  }
  return imgHTML;
 });
 return text;
}
Vue.directive('emotion', {
 bind: function (el, binding) {
  el.innerHTML = toEmotion(binding.value)
 }
});

如何使用?

參數(shù)和說明:

微信聊天可視化組件

依賴scrollLoader組件, 依賴指令v-emotion(實現(xiàn)請查看main.js)

參數(shù):

width 組件寬度,默認(rèn)450

wrapBg 外層父元素背景顏色,默認(rèn)#efefef

maxHeight 展示窗口最高高度, 默認(rèn)900

contactAvatarUrl 好友頭像url

ownerAvatarUrl 微信主人頭像url

ownerNickname 微信主人昵稱

getUpperData (必需)當(dāng)滾動到上方時加載數(shù)據(jù)的方法,返回值要為Promise對象,resolve的結(jié)構(gòu)同data

getUnderData (必需)當(dāng)滾動到下方時加載數(shù)據(jù)的方法,返回值同上

data (必需)傳入初始化數(shù)據(jù), 結(jié)構(gòu)如下:

[{
 direction: 2, //為2表示微信主人發(fā)出的消息,1表示聯(lián)系人
 id: 1, //根據(jù)這個來排序消息
 type: 1, //1為文本,2為圖片
 content: '你好!![呲牙]', //當(dāng)type為1時這里是文本消息,當(dāng)type2為2時這里要存放圖片地址;后續(xù)會支持語音的顯示
 ctime: new Date().toLocaleString() //顯示當(dāng)前消息的發(fā)送時間
},
{
 direction: 1,
 id: 2,
 type: 1,
 content: '你也好。[害羞]',
 ctime: new Date().toLocaleString()
}]

完整的使用實例

代碼:

//主文件,對wxChat的用法做示例
<template>
<wxChat 
 :data="wxChatData"
 :showShade="false"
 contactNickname="簡叔"
 :getUpperData="getUpperData"
 :getUnderData="getUnderData"
 :ownerAvatarUrl="ownerAvatarUrl"
 :contactAvatarUrl="contactAvatarUrl"
 :width="420">
</wxChat>
</template>
<script>
import wxChat from './components/wxChat.vue'
export default {
 name: 'app',
 data () {
 return {
  upperTimes: 0,
  underTimes: 0,
  upperId: 0,
  underId: 6,
  ownerAvatarUrl: './src/assets/avatar1.png',
  contactAvatarUrl: './src/assets/avatar2.png',
  wxChatData: [{
  direction: 2,
  id: 1,
  type: 1,
  content: '你好!![呲牙]',
  ctime: new Date().toLocaleString()
  },
  {
  direction: 1,
  id: 2,
  type: 1,
  content: '你也好。[害羞]',
  ctime: new Date().toLocaleString()
  },
  {
  direction: 2,
  id: 3,
  type: 1,
  content: '這是我的簡歷頭像:',
  ctime: new Date().toLocaleString()
  },
  {
  direction: 2,
  id: 4,
  type: 2,
  content: './src/assets/wyz.jpg',
  ctime: new Date().toLocaleString()
  },
  {
  direction: 1,
  id: 5,
  type: 1,
  content: '你開心就好。[微笑]',
  ctime: new Date().toLocaleString()
  }]
 }
 },
 components:{wxChat},
 methods:{
 //向上滾動加載數(shù)據(jù)
 getUpperData(){
  var me = this;
  // 這里為模擬異步加載數(shù)據(jù)
  // 實際上你可能要這么寫:
  // return axios.get('xxx').then(function(result){
  //  return result; //result的格式需要類似下面resolve里面的數(shù)組
  // })
  return new Promise(function(resolve){
  setTimeout(function(){
   //模擬加載完畢
   if(me.upperTimes>3){
   return resolve([]);
   }
   //加載數(shù)據(jù)
   resolve([{
    direction: 2,
    id: me.upperId-1,
    type: 1,
    content: '向上滾動加載第 ' + me.upperTimes +' 條!',
    ctime: new Date().toLocaleString()
   },
   {
    direction: 1,
    id: me.upperId-2,
    type: 1,
    content: '向上滾動加載第 ' + me.upperTimes +' 條!',
    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([]);
   }
   //加載數(shù)據(jù)
   resolve(
   [{
    direction: 1,
    id: me.underId+1,
    type: 1,
    content: '向下滾動加載第 ' + me.underTimes +' 條!',
    ctime: new Date().toLocaleString()
   },
   {
    direction: 2,
    id: me.underId+2,
    type: 1,
    content: '向下滾動加載第 ' + me.underTimes +' 條!',
    ctime: new Date().toLocaleString()
   }]
   )
  }, 1000);
  me.underId = me.underId+2;
  me.underTimes++;
  })
 }
 }
}
</script>
<style>
*{
 margin: 0;
 padding: 0;
}
#app {
 font-family: 'Avenir', Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
}
h2, h3 {
 font-weight: normal;
}
ul {
 list-style-type: none;
 padding: 0;
}
li {
 display: inline-block;
}
</style>

為什么要使用Vue

Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創(chuàng)建可維護(hù)性和可測試性更強(qiáng)的代碼庫,Vue允許可以將一個網(wǎng)頁分割成可復(fù)用的組件,每個組件都包含屬于自己的HTML、CSS、JavaScript,以用來渲染網(wǎng)頁中相應(yīng)的地方,所以越來越多的前端開發(fā)者使用vue。

到此,關(guān)于“Vue怎么實現(xiàn)微信聊天窗口展示組件功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI