基於uni-app技術開發的仿微信界面聊天室uniapp-chatroom實例項目|uniapp仿微信朋友圈,實現了發送圖文消息、表情(gif動圖),圖片預覽、地圖位置、長按菜單、紅包/錢包、仿微信朋友圈等功能。
在H5 / 小程序 / App端測試效果如下,實測多端效果均爲一致。(後續大圖均展示App端)
技術實現
- 編輯器:HBuilder X
- 技術框架:uni-app + vue
- 狀態管理:Vuex
- iconfont圖標:阿里字體圖標庫
- 自定義導航欄 + 底部Tabbar
- 彈窗組件:uniPop(基於uni-app封裝模態彈窗)
- 測試環境:H5端 + 小程序 + App端(三端均兼容)
- 高德地圖:vue-amap
uniapp自定義頂部導航欄headerBar
頂部導航欄採用的是自定義模式,具體可參看這篇文章:uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條
在pages.json裏面配置globalStyle,將navigationStyle設爲custom時,原生頂部導航欄不顯示,這時就能自定義導航欄
uniapp自定義模態彈窗組件
項目中用到的各種彈窗組件是基於uni-app實現的自定義Modal對話框|alert彈窗|Toast弱提示
具體參看:uni-app自定義Modal彈窗組件|仿ios、微信彈窗效果
在main.js裏面引入全局uniPop彈窗組件,通過this.$refs.uniPop.show({...})調用即可
import uniPop from './components/uniPop/uniPop.vue'
Vue.component('uni-pop', uniPop)
<template>
<view class="uni__container flexbox flex_col bg_fbfbfb">
...
<!-- 引入彈窗模板 -->
<uni-pop ref="uniPop"></uni-pop>
</view>
</template>
this.$refs.uniPop.show({content: '手機號不能爲空', time: 2})
引入公共樣式及組件main.js
項目中用到的字體圖標:阿里巴巴iconfont圖標庫,將下載的文件放到assets目錄下,然後引入iconfont.css即可。
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()
仿微信朋友圈透明導航欄
通過onPageScroll函數實現自定義導航上下滑動自動調整導航欄的透明度,滑動到距離頂部200 效果如下圖二
/**
* @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的值來動態實現
<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開發仿微信聊天室就分享完了,希望大家能喜歡??~~
◆ 最後附上基於uni-app開發的自定義底部TabBar及模態彈窗組件
uniapp自定義Tabbar:https://blog.csdn.net/yanxinyun1990/article/details/101219095
uniapp自定義Modal:https://blog.csdn.net/yanxinyun1990/article/details/101594213