最近一直在捣鼓uniapp技术开发,前段时间就有使用uniapp实现了自定义导航栏及模态弹窗组件,想着后续开发个仿微信聊天项目,今天就给你们分享基于uniapp开发的聊天室。css
uniapp自定义导航栏:www.cnblogs.com/xiaoyan2017…html
uniapp自定义模态框:www.cnblogs.com/xiaoyan2017…vue
H5端 / 小程序 / App端测试效果以下 (后续大图统一展现App端)git
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函数实现自定义导航上下滑动自动调整导航栏的透明度
vuex
/**
* @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>
复制代码
在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
}
})
},
...
}
}
复制代码
这样每次发送消息,都会触发updated函数,调用scrollToBottom方法,自动将聊天信息置于聊天页面底部展现了。bash
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开发聊天室的介绍,后续也会继续为你们分享实战项目~~ 😏😏
微信
◆ vue仿微信网页版聊天:www.cnblogs.com/xiaoyan2017…app
◆ NG聊天室IM实例:www.cnblogs.com/xiaoyan2017…框架