vue聊天室|h5+vue仿微信聊天界面|vue仿微信

来源:https://www.cnblogs.com/xiaoyan2017/archive/2019/04/05/10657781.html
-Advertisement-
Play Games

一、項目簡介 基於Vue2.0+Vuex+vue-router+webpack2.0+es6+vuePhotoPreview+wcPop等技術架構開發的仿微信界面聊天室——vueChatRoom,實現了微信聊天下拉刷新、發送消息、表情(動圖),圖片、視頻預覽,打賞、紅包等功能。 二、技術棧 MVVM ...


一、項目簡介

基於Vue2.0+Vuex+vue-router+webpack2.0+es6+vuePhotoPreview+wcPop等技術架構開發的仿微信界面聊天室——vueChatRoom,實現了微信聊天下拉刷新、發送消息、表情(動圖),圖片、視頻預覽,打賞、紅包等功能。

二、技術棧

  • MVVM框架:Vue.js 2.0
  • 狀態管理:Vuex
  • 頁面路由:Vue-router
  • 彈窗插件:wcPop
  • 打包工具:webpack 2.0
  • 環境配置:node.js + cnpm
  • 圖片插件:vue-photo-preview

<!--頂部模板-->
<template>
    <div class="wcim__topBar" v-show="$route.meta.showHeader">
        <div class="inner flexbox flex-alignc">
            <!-- <a class="linkico wcim__ripple-fff" href="javascript:;" @click="$router.back(-1)"><i class="iconfont icon-back"></i></a> -->
            <h4 class="barTxt flex1">
                <div class="barCell flexbox flex__direction-column"><em class="clamp1">Vue聊天室</em></div>
            </h4>
            <a class="linkico wcim__ripple-fff" href="javascript:;"><i class="iconfont icon-search"></i></a>
        </div>
    </div>
</template>

<!--底部tabBar模板-->
<template>
    <div class="wcim__tabBar" v-show="$route.meta.showTabBar">
        <div class="bottomfixed wcim__borT">
            <ul class="flexbox flex-alignc">
                <router-link class="flex1" active-class="on" tag="li" to="/" exact><span class="ico"><i class="iconfont icon-tabbar_xiaoxi"></i><em class="wcim__badge">15</em></span><span class="txt">消息</span></router-link>
                <router-link class="flex1" active-class="on" tag="li" to="/contact"><span class="ico"><i class="iconfont icon-tabbar_tongxunlu"></i></span><span class="txt">通訊錄</span></router-link>
                <router-link class="flex1" active-class="on" tag="li" to="/ucenter"><span class="ico"><i class="iconfont icon-tabbar_wo"></i></span><span class="txt"></span></router-link>
            </ul>
        </div>
    </div>
</template>

◆ vue-router頁面地址路由、vue鉤子攔截登錄狀態:

/*
 *  頁面地址路由js
 */ 
import Vue from 'vue'
import _router from 'vue-router'
import store from '../vuex'

Vue.use(_router) //應用路由

const router = new _router({
    routes: [
        // 登錄、註冊
        {
            path: '/login',
            component: resolve => require(['../views/auth/login'], resolve),
        },
        {
            path: '/register',
            component: resolve => require(['../views/auth/register'], resolve),
        },

        // 首頁、通訊錄、我
        {
            path: '/',
            component: resolve => require(['../views/index'], resolve),
            meta: { showHeader: true, showTabBar: true, requireAuth: true }
        },
        {
            path: '/contact',
            component: resolve => require(['../views/contact'], resolve),
            meta: { showHeader: true, showTabBar: true, requireAuth: true },
        },
        {
            path: '/contact/uinfo',
            component: resolve => require(['../views/contact/uinfo'], resolve),
        },
        {
            path: '/ucenter',
            component: resolve => require(['../views/ucenter'], resolve),
            meta: { showHeader: true, showTabBar: true, requireAuth: true }
        },
        // 聊天頁面
        {
            path: '/chat/group-chat',
            component: resolve => require(['../views/chat/group-chat'], resolve),
            meta: { requireAuth: true }
        },
        {
            path: '/chat/single-chat',
            component: resolve => require(['../views/chat/single-chat'], resolve),
            meta: { requireAuth: true }
        },
        {
            path: '/chat/group-info',
            component: resolve => require(['../views/chat/group-info'], resolve),
            meta: { requireAuth: true }
        }

        // ...
    ]
})

// 註冊全局鉤子攔截登錄狀態
const that = this
router.beforeEach((to, from, next) => {
    const token = store.state.token
    // 判斷該路由地址是否需要登錄許可權
    if(to.meta.requireAuth){
        // 通過vuex state獲取當前token是否存在
        if(token){
            next()
        }else{
            // console.log('還未登錄授權!')
            next()
            wcPop({
                content: '還未登錄授權!', style: 'background:#e03b30;color:#fff;', time: 2,
                end: function(){
                    next({ path: '/login' })
                }
            });
        }
    }else{
        next()
    }
})

export default router

◆ 引入第三方組件庫、插件:

// >>>引入js
import $ from 'jquery'
import fontsize from './assets/js/fontsize'

// >>>引入彈窗插件
import wcPop from './assets/js/wcPop/wcPop'
import './assets/js/wcPop/skin/wcPop.css'

// >>>引入餓了麽移動端vue組件庫
import MintUI, { Loadmore } from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.component(Loadmore.name, Loadmore)
Vue.use(MintUI)

// >>>引入圖片預覽插件
import photoPreview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
Vue.use(photoPreview, {
  loop: false,
  fullscreenEl: false, //是否全屏
  arrowEl: false, //左右按鈕
})

// >>>引入地址路由
import router from './router'
import store from './vuex'

◆ 登錄、註冊模塊驗證:

import { setToken, checkTel } from '../../utils/filters'
export default {
    data () {
        return {
            formObj: {},

            vcodeText: '獲取驗證碼',
            tel: '',
            disabled: false,
            time: 0,
        }
    },
    methods: {
        handleSubmit(){
            // console.log(this.formObj)
            // console.log(JSON.stringify(this.formObj))

            var that = this;
            if(!this.formObj.tel){
                wcPop({ content: '手機號不能為空!', style: 'background:#e03b30;color:#fff;', time: 2 });
            }else if(!checkTel(this.formObj.tel)){
                wcPop({ content: '手機號格式不正確!', style: 'background:#e03b30;color:#fff;', time: 2 });
            }else if(!this.formObj.pwd){
                wcPop({ content: '密碼不能為空!', style: 'background:#e03b30;color:#fff;', time: 2 });
            }else if(!this.formObj.vcode){
                wcPop({ content: '驗證碼不能為空!', style: 'background:#e03b30;color:#fff;', time: 2 });
            }else{
                this.$store.commit('SET_TOKEN', setToken());
                this.$store.commit('SET_USER', this.formObj.tel);

                wcPop({
                    content: '註冊成功!', style: 'background:#41b883;color:#fff;', time: 2,
                    end: function(){
                        that.$router.push('/');
                    }
                });
            }
        },
        // 60s倒計時
        handleVcode(){
            if(!this.formObj.tel){
                wcPop({ content: '手機號不能為空!', style: 'background:#e03b30;color:#fff;', time: 2 });
            }else if(!checkTel(this.formObj.tel)){
                wcPop({ content: '手機號格式不正確!', style: 'background:#e03b30;color:#fff;', time: 2 });
            }else{
                this.time = 60;
                this.disabled = true;
                this.countDown();
            }
        },
        countDown(){
            if(this.time > 0){
                this.time--;
                this.vcodeText = '獲取驗證碼('+this.time+')';
                setTimeout(this.countDown, 1000);
            }else{
                this.time = 0;
                this.vcodeText = '獲取驗證碼';
                this.disabled = false;
            }
        }
    }
}

◆ 聊天頁面模塊:

// >>> 【表情、動圖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);
            })
        }
    });
});


// >>> 【編輯器+表情處理模塊】------------------------------------------
// ...處理編輯器信息
function surrounds() {
    setTimeout(function () { //chrome
        var sel = window.getSelection();
        var anchorNode = sel.anchorNode;
        if (!anchorNode) return;
        if (sel.anchorNode === $(".J__wcEditor")[0] ||
            (sel.anchorNode.nodeType === 3 && sel.anchorNode.parentNode === $(".J__wcEditor")[0])) {

            var range = sel.getRangeAt(0);
            var p = document.createElement("p");
            range.surroundContents(p);
            range.selectNodeContents(p);
            range.insertNode(document.createElement("br")); //chrome
            sel.collapse(p, 0);

            (function clearBr() {
                var elems = [].slice.call($(".J__wcEditor")[0].children);
                for (var i = 0, len = elems.length; i < len; i++) {
                    var el = elems[i];
                    if (el.tagName.toLowerCase() == "br") {
                        $(".J__wcEditor")[0].removeChild(el);
                    }
                }
                elems.length = 0;
            })();
        }
    }, 10);
}

// 定義最後游標位置
var _lastRange = null, _sel = window.getSelection && window.getSelection();
var _rng = {
    getRange: function () {
        if (_sel && _sel.rangeCount > 0) {
            return _sel.getRangeAt(0);
        }
    },
    addRange: function () {
        if (_lastRange) {
            _sel.removeAllRanges();
            _sel.addRange(_lastRange);
        }
    }
}

// 格式化編輯器包含標簽
$("body").on("click", ".J__wcEditor", function(){
    $(".wc__choose-panel").hide();
});
$("body").on("focus", ".J__wcEditor", function(){
    surrounds();
});
$("body").on("input", ".J__wcEditor", function(){
    surrounds();
});

// 點擊表情
$("body").on("click", "#J__swiperEmotion .face-list span img", function () {
    var that = $(this), range;

    if (that.hasClass("face")) { //小表情
        var img = that[0].cloneNode(true);
        if (!$(".J__wcEditor")[0].childNodes.length) {
            $(".J__wcEditor")[0].focus();
        }
        $(".J__wcEditor")[0].blur(); //輸入表情時禁止輸入法

        setTimeout(function () {
            if (document.selection && document.selection.createRange) {
                document.selection.createRange().pasteHTML(img);
            } else if (window.getSelection && window.getSelection().getRangeAt) {
                range = _rng.getRange();
                range.insertNode(img);
                range.collapse(false);

                _lastRange = range; //記錄當前游標位置 (否則游標會跑到表情前面)
                _rng.addRange();
            }
        }, 10);
    } else if (that.hasClass("del")) { //刪除
        // _editor.focus();
        $(".J__wcEditor")[0].blur(); //輸入表情時禁止輸入法

        setTimeout(function () {
            range = _rng.getRange();
            range.collapse(false);
            document.execCommand("delete");

            _lastRange = range;
            _rng.addRange();
        }, 10);
    } else if (that.hasClass("lg-face")) { //大表情
        var _img = that.parent().html();
        var _tpl = [
            '<li class="me">\
                <div class="content">\
                    <p class="author">王梅(Fine)</p>\
                    <div class="msg lgface">'+ _img + '</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);

        wchat_ToBottom();
    }
});

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 存儲過程 就是一組用於完成特定功能的PL/SQL 具名語句塊,該SQL語句集經過編譯後存儲在資料庫系統中。在使用時候,我們只需要通過指定已經定義的存儲過程名字並給出對應的參數來執行 存儲過程的定義語法 create or replace procedure 過程名(參數名 參數模式 參數類型,參數名 ...
  • 什麼是Elasticsearch的動態映射? 它有什麼作用和優點? 如何自定義使用動態模板? 本篇文章介紹這些內容. ...
  • 環境: 操作系統:Windows7(64位); 資料庫:Oracle 11g R2; 資料庫字元集:UTF-8 一、下載: (參考鏈接:https://blog.csdn.net/u011031430/article/details/76167934) 1.打開 https://edelivery. ...
  • MySQL 是如何解決幻讀的 一、什麼是幻讀 在一次事務裡面,多次查詢之後,結果集的個數不一致的情況叫做幻讀。 而多出來或者少的哪一行被叫做 二、為什麼要解決幻讀 在高併發資料庫系統中,需要保證事務與事務之間的隔離性,還有事務本身的一致性。 三、MySQL 是如何解決幻讀的 如果你看到了這篇文章,那 ...
  • 有關關係資料庫範式的講解,有一篇非常好的文章,推薦給大家,可以幫助大家理解範式的含義、作用。解釋一下關係資料庫的第一第二第三範式? - 知乎用戶的回答 - 知乎https://www.zhihu.com/question/24696366/answer/29189700 ...
  • 1.無論何時只要有多個查詢在同一時刻修改數據,都會產生併發控制的問題 2.討論mysql在兩個層面,伺服器層和存儲引擎層,如何併發控制讀寫 3.舉了個mbox郵箱文件的例子,說如果有多個進程同時對mbox文件寫東西,那麼在文件的末尾會,交叉混亂的添加,比如進程1寫了幾行,進程2也寫了幾行,互相交叉, ...
  • 筆記記錄自林曉斌(丁奇)老師的《MySQL實戰45講》 (本篇內圖片均來自丁奇老師的講解,如有侵權,請聯繫我刪除) 16) --“order by”是怎麼工作的? 在林老師的課程中,第15講是前面問題的答疑,我打算最後將答疑問題統一整理出來,所以就繼續這些內容的筆記了。 全欄位排序: 假設有一個表是 ...
  • pt table checksum Verify MySQL replication integrity. pt table checksum performs an online replication consistency check by executing checksum queries ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...