Retired/pages/paper/chat.vue

108 lines
2.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<!-- 小纸条/聊天列表 -->
<view class="chat">
<!-- 聊天列表 -->
<!-- 方式一通过js计算高度控制scroll-view高度 -->
<!-- <scroll-view :style="`height:${scrollHeight}px;`" scroll-y="true"> -->
<!-- 方式二通过css布局控制scroll-view高度 -->
<scroll-view class="chat-scroll" scroll-y="true" :scroll-into-view="scrollInto" scroll-with-animation>
<!-- 消息列表绑定id用于滚动到该元素 -->
<chat-list v-for="(item,index) in list" :key="index" :item="item"
:pretime="index > 0 ? list[index-1].createTime : 0"></chat-list>
</scroll-view>
<!-- 底部操作栏 -->
<bottom-input @submit="submit"></bottom-input>
</view>
</template>
<script>
import {
chatList
} from "@/utils/data/data.js"
import ChatList from "@/pages/paper/cpns/chat-list.vue"
import BottomInput from "@/components/bottom-input/bottom-input.vue"
export default {
components: {
ChatList,
BottomInput
},
data() {
return {
scrollInto: '',
scrollHeight: 0,
list: chatList,
}
},
onLoad() {
// 设置scrollHeight
uni.getSystemInfo({
success: (res) => {
// 屏幕高度screenHeight) = 手机状态栏的高度(statusBarHeight) + 导航栏高度(44px) + 可使用窗口高度(windowHeight)
// 可使用窗口高度 = scroll-view高度 + 其他组件高度
this.scrollHeight = res.windowHeight - uni.upx2px(82)
}
})
},
// 页面加载完成
onReady() {
// 滚动到底部
this.scrollToBottom()
},
methods: {
// 发送消息
submit(data) {
const obj = {
userid: 1,
avatar: '/static/img/header/a.svg',
username: 'jay',
data,
type: 'text',
createTime: this.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss')
}
this.list.push(obj)
// 滚动到底部
setTimeout(() => {
this.scrollToBottom()
}, 500)
},
// 滚动到底部
scrollToBottom() {
const lastIndex = this.list.length - 1
if (lastIndex < 0) return
this.scrollInto = 'chat' + lastIndex
}
}
}
</script>
<style lang="scss" scoped>
.chat {
// 通过绝对定位控制scroll-view高度
.chat-scroll {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 85rpx;
}
.bottom-handle {
border-top: 2rpx solid $uni-border-color1;
position: fixed;
left: 0;
right: 0;
bottom: 0;
height: 85rpx;
background-color: #fff;
display: flex;
align-items: center;
.send-btn {
width: 80rpx;
text-align: center;
}
}
}
</style>