vue+web端仿微信网页版聊天室功能

所属分类: 网络编程 / JavaScript 阅读数: 1765
收藏 0 赞 0 分享

一、项目介绍

基于Vue2.5.6+Vuex+vue-cli+vue-router+vue-gemini-scrollbar+swiper+elementUI等技术混合架构开发的仿微信web端聊天室——vueWebChat,实现了发送消息、表情(动图),图片、视频预览,右键菜单、截屏、截图可直接粘贴至文本框进行发送。

二、技术框架

•MVVM框架:Vue2.5.6
•状态管理:Vuex
•页面路由:Vue-router
•iconfont图标:阿里巴巴字体图标库
•自定义滚动条:vue-gemini-scrollbar
•弹窗组件:element-ui(饿了么前端UI库)
•环境配置:node.js + cnpm + webpack
•高德地图:vue-amap
•图片预览:vue-photo-preview

◆ 点击右上角最大化按钮,可以进行全屏切换

◆ 引入公共及全局组件配置components.js

/*
 引入公共及全局组件配置
*/ 
// 引入侧边栏及联系人
import winBar from './components/winbar'
import sideBar from './components/sidebar'
import recordList from './components/recordList'
import contactList from './components/contact'
// 引入jquery
import $ from 'jquery'
// 引入wcPop弹窗插件
import wcPop from './assets/js/wcPop/wcPop'
import './assets/js/wcPop/skin/wcPop.css'
// 引入饿了么pc端UI库
import elementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 引入图片预览插件
import photoPreview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
// 引入自定义滚动条插件
import geminiScrollbar from 'vue-gemini-scrollbar'
// 引入加载更多插件
import infiniteLoading from 'vue-infinite-scroll'
// 引入高德地图
import vueAMap from 'vue-amap'
const install = Vue => {
 // 注册组件
 Vue.component('win-bar', winBar)
 Vue.component('side-bar', sideBar)
 Vue.component('record-list', recordList)
 Vue.component('contact-list', contactList)
 // 应用实例
 Vue.use(elementUI)
 Vue.use(photoPreview, {
  loop: false,
  fullscreenEl: true, //是否全屏
  arrowEl: true, //左右按钮
 });
 Vue.use(geminiScrollbar)
 Vue.use(infiniteLoading)
 Vue.use(vueAMap)
 vueAMap.initAMapApiLoader({
  key: "e1dedc6bdd765d46693986ff7ff969f4",
  plugin: [
   "AMap.Autocomplete", //输入提示插件
   "AMap.PlaceSearch", //POI搜索插件
   "AMap.Scale", //右下角缩略图插件 比例尺
   "AMap.OverView", //地图鹰眼插件
   "AMap.ToolBar", //地图工具条
   "AMap.MapType", //类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
   "AMap.PolyEditor", //编辑 折线多,边形
   "AMap.CircleEditor", //圆形编辑器插件
   "AMap.Geolocation" //定位控件,用来获取和展示用户主机所在的经纬度位置
  ],
  uiVersion: "1.0"
 });
}
export default install

◆ 主页面模板

<template>
 <div id="app">
 <div class="vChat-wrapper flexbox flex-alignc">
  <div class="vChat-panel" style="background-image: url(src/assets/img/placeholder/vchat__panel-bg01.jpg);">
  <div class="vChat-inner flexbox">
   <!-- //顶部按钮(最大、最小、关闭) -->
   <win-bar></win-bar>
   <!-- //侧边栏 -->
   <side-bar></side-bar>
   <keep-alive>
   <router-view class="flex1 flexbox"></router-view>
   </keep-alive>
  </div>
  </div>
 </div>
 </div>
</template>
<script>
export default {
 name: 'app',
 data () {
 return {
 }
 },
 methods: {
 },
}
</script>
<style>
/* 引入公共样式 */
@import './assets/fonts/iconfont.css';
@import './assets/css/reset.css';
@import './assets/css/layout.css';
</style>

◆ vue文本框实现截图粘贴发送图片:

// 【截图粘贴图片】
document.getElementById('J__wcEditor').addEventListener('paste',function(e){
 var cbd = e.clipboardData;
 var ua = window.navigator.userAgent;
 // 没有数据
 if (!(e.clipboardData && e.clipboardData.items)) {
  return;
 }
 // Mac平台下Chrome49版本以下 复制Finder中的文件的Bug Hack掉
 if(cbd.items && cbd.items.length === 2 && cbd.items[0].kind === "string" && cbd.items[1].kind === "file" &&
  cbd.types && cbd.types.length === 2 && cbd.types[0] === "text/plain" && cbd.types[1] === "Files" &&
  ua.match(/Macintosh/i) && Number(ua.match(/Chrome\/(\d{2})/i)[1]) < 49){
  return;
 }
 for(var i = 0; i < cbd.items.length; i++){
  var item = cbd.items[i];
  console.log(item);
  console.log(item.kind);
  if(item.kind == "file"){
   var blob = item.getAsFile();
   if(blob.size === 0){
    return;
   }
   // 插入图片记录
   var reader = new FileReader();
   reader.readAsDataURL(blob);
   reader.onload = function(){
    var _img = this.result;
    var _tpl = [
     '<li class="me">\
      <div class="content">\
       <p class="author">王梅(Fine)</p>\
       <div class="msg picture"><img class="img__pic" src="'+ _img + '" preview="1" /></div>\
      </div>\
      <a class="avatar" href="/contact/uinfo"><img src="src/assets/img/uimg/u__chat-img11.jpg" /></a>\
     </li>'
    ].join("");
    $("#J__chatMsgList").append(_tpl);
    setTimeout(() => {
     $("#J__geminiScrollbar .gm-scroll-view").animate({ scrollTop: $("#J__chatMsgList").height() }, 0);
     $(".fixGeminiscrollHeight").show();
     setTimeout(() => { $(".fixGeminiscrollHeight").hide();}, 300);
    }, 17);
   }
  }
 }
});

◆ 表情处理及视频预览:

// >>> 【表情、动图swiper切换模块】--------------------------
var emotionSwiper;
function setEmotionSwiper(tmpl) {
 var _tmpl = tmpl ? tmpl : $("#J__emotionFootTab ul li.cur").attr("tmpl");
 $("#J__swiperEmotion .swiper-container").attr("id", _tmpl);
 $("#J__swiperEmotion .swiper-wrapper").html($("." + _tmpl).html());
 emotionSwiper = new Swiper('#' + _tmpl, {
  // loop: true,
  // autoplay: true,
  // 分页器
  pagination: {
   el: '.pagination-emotion', clickable: true,
  },
 });
}
// 表情模板切换
$("body").on("click", "#J__emotionFootTab ul li.swiperTmpl", function () {
 // 先销毁swiper
 emotionSwiper && emotionSwiper.destroy(true, true);
 var _tmpl = $(this).attr("tmpl");
 $(this).addClass("cur").siblings().removeClass("cur");
 setEmotionSwiper(_tmpl);
});
// >>> 【视频预览模块】--------------------------
$("body").on("click", "#J__chatMsgList li .video", function () {
 var _src = $(this).find("img").attr("videoUrl"), _video;
 var videoIdx = wcPop({
  id: 'wc__previewVideo',
  skin: 'fullscreen',
  // content: '<video id="J__videoPreview" width="100%" height="100%" controls="controls" x5-video-player-type="h5" x5-video-player-fullscreen="true" webkit-playsinline preload="auto"></video>',
  content: '<video id="J__videoPreview" width="100%" height="100%" controls="controls" preload="auto"></video>',
  shade: false,
  xclose: true,
  style: 'background: #000;padding-top:48px;',
  anim: 'scaleIn',
  show: function(){
   _video = document.getElementById("J__videoPreview");
   _video.src = _src;
   if (_video.paused) {
    _video.play();
   } else {
    _video.pause();
   }
   // 播放结束
   _video.addEventListener("ended", function(){
    _video.currentTime = 0;
   });
   // 退出全屏
   _video.addEventListener("x5videoexitfullscreen", function(){
    wcPop.close(videoIdx);
   })
  }
 });
});

以上所述是小编给大家介绍的vue+web端仿微信网页版聊天室功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

更多精彩内容其他人还在看

AngularJs IE Compatibility 兼容老版本IE

本文主要介绍AngularJs IE Compatibility 兼容老版本IE的问题及解决办法,有兴趣的小伙伴可以参考下
收藏 0 赞 0 分享

AngularJs Modules详解及示例代码

本文主要介绍AngularJs Modules的相关知识,这里整理了详细的资料及简单示例代码,有兴趣的朋友可以参考下
收藏 0 赞 0 分享

AngularJs Scope详解及示例代码

本文主要介绍AngularJs Scope的知识,这里整理了详细资料及示例代码,有兴趣的小伙伴可以参考下
收藏 0 赞 0 分享

node.js中module.exports与exports用法上的区别

Node.js 引入了模块(Module)概念,一个模块可以通过module.exports 或 exports 将函数、变量等导出,以使其它 JavaScript 脚本通过require() 函数引入并使用。那么node.js中module.exports与exports有什么
收藏 0 赞 0 分享

基于JS实现发送短信验证码后的倒计时功能(无视页面刷新,页面关闭不进行倒计时功能)

这篇文章主要介绍了基于JS实现发送短信验证码后的倒计时功能(无视页面刷新,页面关闭不进行倒计时功能)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

基于jQuery实现发送短信验证码后的倒计时功能(无视页面关闭)

最近做了一个项目,其中有需求要求实现发送短信验证码后倒计时功能,其中有个难点:要求关闭页面也进行倒计时。好吧,下面小编把jquery 发送验证码倒计时的实现代码分享给大家,大家可以参考下
收藏 0 赞 0 分享

js绘制购物车抛物线动画

这篇文章主要为大家详细介绍了js绘制购物车抛物线动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

vue.js入门教程之绑定class和style样式

小编之前介绍了通过vue.js计算属性,不知道大家都学会了吗。那这篇文章中我们将一起学习vue.js实现绑定class和style样式,有需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

纯JS实现可拖拽表单的简单实例

下面小编就为大家带来一篇纯JS实现可拖拽表单的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

js实现StringBuffer的简单实例

下面小编就为大家带来一篇js实现StringBuffer的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多