7
7
-->
8
8
<template >
9
9
<ContentWrap >
10
- <div class =" msg-div" :id = " msgDivId " >
10
+ <div class =" msg-div" ref = " msgDivRef " >
11
11
<!-- 加载更多 -->
12
12
<div v-loading =" loading" ></div >
13
13
<div v-if =" !loading" >
@@ -47,8 +47,7 @@ const props = defineProps({
47
47
}
48
48
})
49
49
50
- const accountId = ref <number >(- 1 ) // 公众号ID,需要通过userId初始化
51
- const msgDivId = ` msg-div-{new Date().getTime()} ` // 当前的时间戳,用于每次消息加载后,回到原位置;具体见 :id="'msg-div' + nowStr" 处
50
+ const accountId = ref (- 1 ) // 公众号ID,需要通过userId初始化
52
51
const loading = ref (false ) // 消息列表是否正在加载中
53
52
const hasMore = ref (true ) // 是否可以加载更多
54
53
const list = ref <any []>([]) // 消息列表
@@ -74,7 +73,8 @@ const reply = ref<Reply>({
74
73
articles: []
75
74
})
76
75
77
- const replySelectRef = ref <InstanceType <typeof WxReplySelect > | null >(null )
76
+ const replySelectRef = ref <InstanceType <typeof WxReplySelect > | null >(null ) // WxReplySelect组件ref,用于消息发送成功后清除内容
77
+ const msgDivRef = ref () // 消息显示窗口ref,用于滚动到底部
78
78
79
79
/** 完成加载 */
80
80
onMounted (async () => {
@@ -89,7 +89,7 @@ onMounted(async () => {
89
89
90
90
// 执行发送
91
91
const sendMsg = async () => {
92
- if (! reply ) {
92
+ if (! unref ( reply ) ) {
93
93
return
94
94
}
95
95
// 公众号限制:客服消息,公众号只允许发送一条
@@ -117,7 +117,7 @@ const loadMore = () => {
117
117
getPage (queryParams , null )
118
118
}
119
119
120
- const getPage = async (page : any , params : any ) => {
120
+ const getPage = async (page : any , params : any = null ) => {
121
121
loading .value = true
122
122
let dataTemp = await getMessagePage (
123
123
Object .assign (
@@ -131,11 +131,7 @@ const getPage = async (page: any, params: any) => {
131
131
)
132
132
)
133
133
134
- const msgDiv = document .getElementById (msgDivId )
135
- let scrollHeight = 0
136
- if (msgDiv ) {
137
- scrollHeight = msgDiv .scrollHeight
138
- }
134
+ const scrollHeight = msgDivRef .value ?.scrollHeight ?? 0
139
135
// 处理数据
140
136
const data = dataTemp .list .reverse ()
141
137
list .value = [... data , ... list .value ]
@@ -153,24 +149,22 @@ const getPage = async (page: any, params: any) => {
153
149
// 定位滚动条
154
150
await nextTick ()
155
151
if (scrollHeight !== 0 ) {
156
- let div = document .getElementById (msgDivId )
157
- if (div && msgDiv ) {
158
- msgDiv .scrollTop = div .scrollHeight - scrollHeight - 100
152
+ if (msgDivRef .value ) {
153
+ msgDivRef .value .scrollTop = msgDivRef .value .scrollHeight - scrollHeight - 100
159
154
}
160
155
}
161
156
}
162
157
}
163
158
164
159
const refreshChange = () => {
165
- getPage (queryParams , null )
160
+ getPage (queryParams )
166
161
}
167
162
168
163
/** 定位到消息底部 */
169
164
const scrollToBottom = async () => {
170
165
await nextTick ()
171
- let div = document .getElementById (msgDivId )
172
- if (div ) {
173
- div .scrollTop = div .scrollHeight
166
+ if (msgDivRef .value ) {
167
+ msgDivRef .value .scrollTop = msgDivRef .value .scrollHeight
174
168
}
175
169
}
176
170
</script >
0 commit comments