经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue+web端仿微信网页版聊天室功能
来源:jb51  时间:2019/4/30 10:12:11  对本文有异议

一、项目介绍

基于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

  1. /*
  2. 引入公共及全局组件配置
  3. */
  4. // 引入侧边栏及联系人
  5. import winBar from './components/winbar'
  6. import sideBar from './components/sidebar'
  7. import recordList from './components/recordList'
  8. import contactList from './components/contact'
  9. // 引入jquery
  10. import $ from 'jquery'
  11. // 引入wcPop弹窗插件
  12. import wcPop from './assets/js/wcPop/wcPop'
  13. import './assets/js/wcPop/skin/wcPop.css'
  14. // 引入饿了么pc端UI库
  15. import elementUI from 'element-ui'
  16. import 'element-ui/lib/theme-chalk/index.css'
  17. // 引入图片预览插件
  18. import photoPreview from 'vue-photo-preview'
  19. import 'vue-photo-preview/dist/skin.css'
  20. // 引入自定义滚动条插件
  21. import geminiScrollbar from 'vue-gemini-scrollbar'
  22. // 引入加载更多插件
  23. import infiniteLoading from 'vue-infinite-scroll'
  24. // 引入高德地图
  25. import vueAMap from 'vue-amap'
  26. const install = Vue => {
  27. // 注册组件
  28. Vue.component('win-bar', winBar)
  29. Vue.component('side-bar', sideBar)
  30. Vue.component('record-list', recordList)
  31. Vue.component('contact-list', contactList)
  32. // 应用实例
  33. Vue.use(elementUI)
  34. Vue.use(photoPreview, {
  35. loop: false,
  36. fullscreenEl: true, //是否全屏
  37. arrowEl: true, //左右按钮
  38. });
  39. Vue.use(geminiScrollbar)
  40. Vue.use(infiniteLoading)
  41. Vue.use(vueAMap)
  42. vueAMap.initAMapApiLoader({
  43. key: "e1dedc6bdd765d46693986ff7ff969f4",
  44. plugin: [
  45. "AMap.Autocomplete", //输入提示插件
  46. "AMap.PlaceSearch", //POI搜索插件
  47. "AMap.Scale", //右下角缩略图插件 比例尺
  48. "AMap.OverView", //地图鹰眼插件
  49. "AMap.ToolBar", //地图工具条
  50. "AMap.MapType", //类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
  51. "AMap.PolyEditor", //编辑 折线多,边形
  52. "AMap.CircleEditor", //圆形编辑器插件
  53. "AMap.Geolocation" //定位控件,用来获取和展示用户主机所在的经纬度位置
  54. ],
  55. uiVersion: "1.0"
  56. });
  57. }
  58. export default install

◆ 主页面模板

  1. <template>
  2. <div id="app">
  3. <div class="vChat-wrapper flexbox flex-alignc">
  4. <div class="vChat-panel" style="background-image: url(src/assets/img/placeholder/vchat__panel-bg01.jpg);">
  5. <div class="vChat-inner flexbox">
  6. <!-- //顶部按钮(最大、最小、关闭) -->
  7. <win-bar></win-bar>
  8. <!-- //侧边栏 -->
  9. <side-bar></side-bar>
  10. <keep-alive>
  11. <router-view class="flex1 flexbox"></router-view>
  12. </keep-alive>
  13. </div>
  14. </div>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. name: 'app',
  21. data () {
  22. return {
  23. }
  24. },
  25. methods: {
  26. },
  27. }
  28. </script>
  29. <style>
  30. /* 引入公共样式 */
  31. @import './assets/fonts/iconfont.css';
  32. @import './assets/css/reset.css';
  33. @import './assets/css/layout.css';
  34. </style>

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

  1. // 【截图粘贴图片】
  2. document.getElementById('J__wcEditor').addEventListener('paste',function(e){
  3. var cbd = e.clipboardData;
  4. var ua = window.navigator.userAgent;
  5. // 没有数据
  6. if (!(e.clipboardData && e.clipboardData.items)) {
  7. return;
  8. }
  9. // Mac平台下Chrome49版本以下 复制Finder中的文件的Bug Hack掉
  10. if(cbd.items && cbd.items.length === 2 && cbd.items[0].kind === "string" && cbd.items[1].kind === "file" &&
  11. cbd.types && cbd.types.length === 2 && cbd.types[0] === "text/plain" && cbd.types[1] === "Files" &&
  12. ua.match(/Macintosh/i) && Number(ua.match(/Chrome\/(\d{2})/i)[1]) < 49){
  13. return;
  14. }
  15. for(var i = 0; i < cbd.items.length; i++){
  16. var item = cbd.items[i];
  17. console.log(item);
  18. console.log(item.kind);
  19. if(item.kind == "file"){
  20. var blob = item.getAsFile();
  21. if(blob.size === 0){
  22. return;
  23. }
  24. // 插入图片记录
  25. var reader = new FileReader();
  26. reader.readAsDataURL(blob);
  27. reader.onload = function(){
  28. var _img = this.result;
  29. var _tpl = [
  30. '<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>'
  31. ].join("");
  32. $("#J__chatMsgList").append(_tpl);
  33. setTimeout(() => {
  34. $("#J__geminiScrollbar .gm-scroll-view").animate({ scrollTop: $("#J__chatMsgList").height() }, 0);
  35. $(".fixGeminiscrollHeight").show();
  36. setTimeout(() => { $(".fixGeminiscrollHeight").hide();}, 300);
  37. }, 17);
  38. }
  39. }
  40. }
  41. });

◆ 表情处理及视频预览:

  1. // >>> 【表情、动图swiper切换模块】--------------------------
  2. var emotionSwiper;
  3. function setEmotionSwiper(tmpl) {
  4. var _tmpl = tmpl ? tmpl : $("#J__emotionFootTab ul li.cur").attr("tmpl");
  5. $("#J__swiperEmotion .swiper-container").attr("id", _tmpl);
  6. $("#J__swiperEmotion .swiper-wrapper").html($("." + _tmpl).html());
  7. emotionSwiper = new Swiper('#' + _tmpl, {
  8. // loop: true,
  9. // autoplay: true,
  10. // 分页器
  11. pagination: {
  12. el: '.pagination-emotion', clickable: true,
  13. },
  14. });
  15. }
  16. // 表情模板切换
  17. $("body").on("click", "#J__emotionFootTab ul li.swiperTmpl", function () {
  18. // 先销毁swiper
  19. emotionSwiper && emotionSwiper.destroy(true, true);
  20. var _tmpl = $(this).attr("tmpl");
  21. $(this).addClass("cur").siblings().removeClass("cur");
  22. setEmotionSwiper(_tmpl);
  23. });
  24. // >>> 【视频预览模块】--------------------------
  25. $("body").on("click", "#J__chatMsgList li .video", function () {
  26. var _src = $(this).find("img").attr("videoUrl"), _video;
  27. var videoIdx = wcPop({
  28. id: 'wc__previewVideo',
  29. skin: 'fullscreen',
  30. // 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>',
  31. content: '<video id="J__videoPreview" width="100%" height="100%" controls="controls" preload="auto"></video>',
  32. shade: false,
  33. xclose: true,
  34. style: 'background: #000;padding-top:48px;',
  35. anim: 'scaleIn',
  36. show: function(){
  37. _video = document.getElementById("J__videoPreview");
  38. _video.src = _src;
  39. if (_video.paused) {
  40. _video.play();
  41. } else {
  42. _video.pause();
  43. }
  44. // 播放结束
  45. _video.addEventListener("ended", function(){
  46. _video.currentTime = 0;
  47. });
  48. // 退出全屏
  49. _video.addEventListener("x5videoexitfullscreen", function(){
  50. wcPop.close(videoIdx);
  51. })
  52. }
  53. });
  54. });

总结

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号