uni-app仿微信App界面|vue+uniapp聊天室|仿微信朋友圈

項目簡介

基於uni-app+vue+vuex+uniPop+swiper等技術開發的仿微信聊天室uniapp-chatroom項目,相似vue及小程序api語法使開發更加方便,實現了發送圖文消息、表情(gif動圖),圖片預覽、地圖位置、紅包、仿微信朋友圈等功能css

效果圖

在H5 / 小程序 / App端測試效果以下 (後續大圖統一展現App端)
未標題-1.pnghtml

技術實現

  • 編輯器:HBuilder X
  • 技術框架:uni-app + vue
  • 狀態管理:Vuex
  • iconfont圖標:阿里字體圖標庫
  • 自定義導航欄 + 底部Tabbar
  • 彈窗組件:uniPop(基於uni-app封裝模態彈窗)
  • 測試環境:H5端 + 小程序 + App端(三端均兼容)
  • 高德地圖:vue-amap

001360截圖20191009170834767.png

004360截圖20191009171029165.png

006360截圖20191009171125303.png

007360截圖20191009171142646.png

009360截圖20191009171303741.png

010360截圖20191009171331558.png

012360截圖20191009171425392.png

013360截圖20191009171504766.png

015360截圖20191009171700229.png

022360截圖20191009173308974.png

020360截圖20191009172918501.png

024360截圖20191009173418452.png

026360截圖20191009173557597.png

027360截圖20191009173618772.png

029360截圖20191009174056931.png

引入公共組件及樣式

import Vue from 'vue'
import App from './App'

// >>>引入css
import './assets/fonts/iconfont.css'
import './assets/css/reset.css'
import './assets/css/layout.css'

// >>>引入狀態管理
import store from './store'
Vue.prototype.$store = store

// >>>引入公共組件
import headerBar from './components/header/header.vue'
import tabBar from './components/tabbar/tabbar.vue'
import popupWindow from './components/popupWindow.vue'
Vue.component('header-bar', headerBar)
Vue.component('tab-bar', tabBar)
Vue.component('popup-window', popupWindow)

// >>>引入uniPop彈窗組件
import uniPop from './components/uniPop/uniPop.vue'
Vue.component('uni-pop', uniPop)

Vue.config.productionTip = false
App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()

uniapp+vuex實現登陸攔截

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        user: uni.getStorageSync('user'),
        token: uni.getStorageSync('token'),
    },
    mutations: {
        // 存儲token
        SET_TOKEN(state, data) {
            state.token = data
            uni.setStorageSync('token', data)
        },
        // 存儲用戶名
        SET_USER(state, data) {
            state.user = data
            uni.setStorageSync('user', data)
        },
        ...
    },
})
<script>
    import { mapState, mapMutations } from 'vuex'
    import util from '../../utils/util.js'
    
    export default {
        data() {
            return {
                formObj: {},
            }
        },
        computed: {
            ...mapState(['user', 'token'])
        },
        mounted() {
            // 判斷是否有登陸
            if(this.user){
                uni.redirectTo({url: '/pages/index/index'})
            }
        },
        methods: {
            // 提交表單
            handleSubmit(e) {
                ...
            }
        }
    }
</script>

uniapp實現朋友圈功能

如何實現微信朋友圈頁面向下滾動,頂部導航欄由透明變背景色,經過onPageScroll函數實現自定義導航上下滑動自動調整導航欄的透明度
360截圖20191010014039187.png
360截圖20191010014114837.pngvue

/**
 * @tpl 朋友圈模板
 */

<template>
    <view class="flexbox flex_col">
        <header-bar :isBack="true" title="朋友圈" :bgColor="{background: headerBarBackground}" transparent>
            <text slot="back" class="uni_btnIco iconfont icon-arrL"></text>
            <text slot="iconfont" class="uni_btnIco iconfont icon-publish mr_5" @tap="handlePublish"></text>
        </header-bar>
        
        <view class="uni__scrollview flex1">
            <view class="uni-friendZone">
                ...
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                headerBarBackground: 'transparent'
            }
        },
        onPageScroll : function(e) {
            // console.log("滾動距離爲:" + e.scrollTop);
            this.headerBarBackground = 'rgba(65,168,99,'+e.scrollTop / 200+')'
        },
        methods: {
            ...
        }
    }
</script>

<style scoped>

</style>

uniapp滾動至聊天頁面底部

在uni-app將聊天信息滾動到底部有些很差實現,能夠藉助scroll-view組件scroll-into-view屬性,不過只能設置一次,不能動態設置。
只能經過動態改變scroll-top來實現
360截圖20191010020527193.png
uni-app經過判斷聊天內容高度和scroll-view高度的大小設置滾動距離ios

<scroll-view id="scrollview" scroll-y="true" :scroll-top="scrollTop" style="height: 100%;">
    <view class="uni-chatMsgCnt" id="msglistview">
        <view class="msgitem">xxx</view>
        <view class="msgitem">xxx</view>
        <view class="msgitem">xxx</view>
        ...
    </view>
</scroll-view>
export default {
    data() {
        return {
            scrollTop: 0,
            ...
        }
    },
    mounted() {
        this.scrollToBottom()
    },
    updated() {
        this.scrollToBottom()
    },
    methods: {
        // 滾動至聊天底部
        scrollToBottom(t) {
            let that = this
            let query = uni.createSelectorQuery()
            query.select('#scrollview').boundingClientRect()
            query.select('#msglistview').boundingClientRect()
            query.exec((res) => {
                // console.log(res)
                if(res[1].height > res[0].height){
                    that.scrollTop = res[1].height - res[0].height
                }
            })
        },
        ...
    }
}

基於uniapp開發聊天室的介紹就到這裏,後續繼續爲你們分享實戰項目,但願多多支持。😏😏git

項目中用到的自定義彈窗、自定義導航欄可參看下面連接。
uni-app自定義Modal彈窗組件|仿ios、微信彈窗效果
uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條vuex

相關文章
相關標籤/搜索