键盘高度计算,改善聊天页面内容少的时候推屏的问题,但是有点卡断

This commit is contained in:
2022-02-28 12:15:11 +08:00
parent e8e496cf37
commit bf94cca533
5 changed files with 67 additions and 23 deletions

View File

@@ -8,7 +8,7 @@
<view class="header"> <view class="header">
<view class="name"> <view class="name">
{{ contact(item.targetId).name }} {{ contact(item.targetId).name }}
<!-- <text v-if="item.conversationType === 3" class='group'>[]</text> --> <text v-if="item.conversationType === 3" class='group'>[]</text>
</view> </view>
<view class="time">{{ item.sentTime|timeCustomCN }}</view> <view class="time">{{ item.sentTime|timeCustomCN }}</view>
</view> </view>

View File

@@ -1,6 +1,6 @@
<template> <template>
<view class="chat"> <view class="chat">
<sent-message-bar :conversationType="conversationType" :targetId="targetId" @onSuccess="getNewMessage()" /> <sent-message-bar id="msgbar" :conversationType="conversationType" :targetId="targetId" @onSuccess="getNewMessage()" />
<view class="shade" @click="hidePop" v-show="showPop"> <view class="shade" @click="hidePop" v-show="showPop">
<view class="pop" :style="popStyle" :class="{'show':showPop}"> <view class="pop" :style="popStyle" :class="{'show':showPop}">
@@ -11,7 +11,8 @@
</view> </view>
<view class="body"> <view class="body">
<view class="scroll"> <view class="place" :style="{ height: placeHeight + 'px' }"></view>
<view class="scroll" id="scroll">
<view class="cell" v-for="(message, index) in messages" :key="index"> <view class="cell" v-for="(message, index) in messages" :key="index">
<show-message-cell :message="message" isGroup @messageAction="messageAction" /> <show-message-cell :message="message" isGroup @messageAction="messageAction" />
</view> </view>
@@ -51,7 +52,9 @@
messages: [], messages: [],
groupInfo: { groupInfo: {
name: '' name: ''
} },
placeHeight: uni.getSystemInfoSync().windowHeight,
bottomHeihgt: 56
} }
}, },
computed: { computed: {
@@ -68,7 +71,21 @@
onShow() { onShow() {
this.initGroupInfo() this.initGroupInfo()
}, },
onLoad(e) { onLoad(e) {
const query = uni.createSelectorQuery().in(this);
this.$nextTick(function() {
query.select('#msgbar').boundingClientRect(bottom => {
this.bottomHeihgt = bottom.height
}).exec();
})
// 监听键盘高度变化
uni.onKeyboardHeightChange(keyboard => {
query.select('#scroll').boundingClientRect(scroll => {
const placeHeight = this.windowHeight - scroll.height - keyboard.height - this
.bottomHeihgt
this.placeHeight = placeHeight > 0 ? placeHeight : 0
}).exec();
})
this.targetId = e.targetId this.targetId = e.targetId
// 获取历史消息列表 // 获取历史消息列表
this.getMessageList() this.getMessageList()

View File

@@ -12,6 +12,9 @@ export default {
}, },
sender() { sender() {
return this.$store.getters.sender return this.$store.getters.sender
},
windowHeight() {
return uni.getSystemInfoSync().windowHeight
} }
}, },
methods: { methods: {

View File

@@ -1,6 +1,7 @@
<template> <template>
<view class="chat"> <view class="chat">
<sent-message-bar :conversationType="conversationType" :targetId="targetId" @onSuccess="getNewMessage()" /> <sent-message-bar id="msgbar" :conversationType="conversationType" :targetId="targetId"
@onSuccess="getNewMessage()" />
<view class="shade" @click="hidePop" v-show="showPop"> <view class="shade" @click="hidePop" v-show="showPop">
<view class="pop" :style="popStyle" :class="{'show':showPop}"> <view class="pop" :style="popStyle" :class="{'show':showPop}">
<view v-for="(item, index) in popButton" :key="index" @click="pickerMenu(item)"> <view v-for="(item, index) in popButton" :key="index" @click="pickerMenu(item)">
@@ -10,7 +11,8 @@
</view> </view>
<!-- chat --> <!-- chat -->
<view class="body"> <view class="body">
<view class="scroll"> <view class="place" :style="{ height: placeHeight + 'px' }"></view>
<view class="scroll" id="scroll">
<view class="cell" v-for="(message, index) in messages" :key="index"> <view class="cell" v-for="(message, index) in messages" :key="index">
<show-message-cell :message="message" @messageAction="messageAction" /> <show-message-cell :message="message" @messageAction="messageAction" />
</view> </view>
@@ -26,11 +28,11 @@
import showMessageCell from '../components/showMessageCell' import showMessageCell from '../components/showMessageCell'
import utils from '@/utils/index.js' import utils from '@/utils/index.js'
import imBase from '../mixins/imBase.js' import imBase from '../mixins/imBase.js'
import messageActions from '../mixins/messageActions.js' import messageActions from '../mixins/messageActions.js'
export default { export default {
mixins: [ mixins: [
imBase, imBase,
messageActions messageActions
], ],
components: { components: {
@@ -46,7 +48,9 @@
name: '', name: '',
userId: '', userId: '',
portraitUrl: '' portraitUrl: ''
} },
placeHeight: uni.getSystemInfoSync().windowHeight,
bottomHeihgt: 56
} }
}, },
computed: { computed: {
@@ -59,13 +63,27 @@
} }
} }
} }
},
onShow() {
uni.setNavigationBarTitle({
title: this.contact(this.targetId).name
})
}, },
onLoad(e) { onShow() {
uni.setNavigationBarTitle({
title: this.contact(this.targetId).name
})
},
onLoad(e) {
const query = uni.createSelectorQuery().in(this);
this.$nextTick(function() {
query.select('#msgbar').boundingClientRect(bottom => {
this.bottomHeihgt = bottom.height
}).exec();
})
// 监听键盘高度变化
uni.onKeyboardHeightChange(keyboard => {
query.select('#scroll').boundingClientRect(scroll => {
const placeHeight = this.windowHeight - scroll.height - keyboard.height - this
.bottomHeihgt
this.placeHeight = placeHeight > 0 ? placeHeight : 0
}).exec();
})
this.targetId = e.targetId this.targetId = e.targetId
this.userInfo = this.$store.getters.contactInfo(this.targetId) this.userInfo = this.$store.getters.contactInfo(this.targetId)
// 获取消息列表 // 获取消息列表
@@ -106,16 +124,19 @@
}) })
}, },
onUnload() { onUnload() {
uni.offKeyboardHeightChange(res => {
console.log('卸载监听键盘高度变化', res);
})
uni.$off('onUserDelete_' + this.targetId) uni.$off('onUserDelete_' + this.targetId)
uni.$off('onReceiveMessage_' + this.targetId) uni.$off('onReceiveMessage_' + this.targetId)
uni.$off('onRecallMessage_' + this.targetId) uni.$off('onRecallMessage_' + this.targetId)
uni.$off('onRecallMessage') uni.$off('onRecallMessage')
uni.$off('onReadReceiptReceived') uni.$off('onReadReceiptReceived')
}, },
onNavigationBarButtonTap() { onNavigationBarButtonTap() {
uni.navigateTo({ uni.navigateTo({
url:'/pages/im/friends/info?targetId='+this.targetId url: '/pages/im/friends/info?targetId=' + this.targetId
}) })
}, },
methods: { methods: {
getNewMessage() { getNewMessage() {
@@ -160,6 +181,8 @@
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.place {}
/* 窗口 */ /* 窗口 */
.chat { .chat {
background: $window-color; background: $window-color;

View File

@@ -66,13 +66,14 @@ const connect = (token, userInfo, callback) => {
store.dispatch('setSenderInfo', userInfo) store.dispatch('setSenderInfo', userInfo)
// 设置未读消息数量 // 设置未读消息数量
setNotifyBadge() setNotifyBadge()
// 首次运行获取好友列表 // 首次运行获取好友列表
const FK = 'ZH_CONTACT_' + userInfo.targetId const FK = 'ZH_CONTACT_' + userInfo.targetId
uni.getStorage({ uni.getStorage({
key: FK, key: FK,
success: () => { success: () => {
contactModel.find((err, results) => { contactModel.find((err, results) => {
console.log('初始化联系人信息', results);
results.map(item => { results.map(item => {
store.dispatch('launchContact', item) store.dispatch('launchContact', item)
}) })