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

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: {
@@ -69,6 +72,20 @@
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>
@@ -46,7 +48,9 @@
name: '', name: '',
userId: '', userId: '',
portraitUrl: '' portraitUrl: ''
} },
placeHeight: uni.getSystemInfoSync().windowHeight,
bottomHeihgt: 56
} }
}, },
computed: { computed: {
@@ -66,6 +70,20 @@
}) })
}, },
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.userInfo = this.$store.getters.contactInfo(this.targetId) this.userInfo = this.$store.getters.contactInfo(this.targetId)
// 获取消息列表 // 获取消息列表
@@ -106,6 +124,9 @@
}) })
}, },
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)
@@ -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

@@ -73,6 +73,7 @@ const connect = (token, userInfo, callback) => {
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)
}) })