Vue_US/src/views/mp/components/wx-msg/main.vue

191 lines
5.0 KiB
Vue
Raw Normal View History

<!--
- Copyright (C) 2018-2019
- All rights reserved, Designed By www.joolun.com
芋道源码
移除暂时用不到的 websocket
代码优化补充注释提升阅读性
-->
<template>
<ContentWrap>
<div class="msg-div" ref="msgDivRef">
<!-- 加载更多 -->
<div v-loading="loading"></div>
<div v-if="!loading">
2023-04-22 20:51:25 +08:00
<div class="el-table__empty-block" v-if="hasMore" @click="loadMore"
><span class="el-table__empty-text">点击加载更多</span></div
>
2023-04-22 20:51:25 +08:00
<div class="el-table__empty-block" v-if="!hasMore"
><span class="el-table__empty-text">没有更多了</span></div
>
</div>
2023-04-22 20:51:25 +08:00
<!-- 消息列表 -->
2023-04-22 20:51:25 +08:00
<MsgList :list="list" :account-id="accountId" :user="user" />
</div>
2023-04-22 20:51:25 +08:00
<div class="msg-send" v-loading="sendLoading">
<WxReplySelect ref="replySelectRef" v-model="reply" />
<el-button type="success" class="send-but" @click="sendMsg">发送(S)</el-button>
</div>
</ContentWrap>
</template>
2023-04-12 14:37:59 +08:00
<script setup lang="ts" name="WxMsg">
2023-04-22 20:51:25 +08:00
import WxReplySelect, { Reply, ReplyType } from '@/views/mp/components/wx-reply'
import MsgList from './components/MsgList.vue'
2023-04-12 14:37:59 +08:00
import { getMessagePage, sendMessage } from '@/api/mp/message'
2023-04-08 22:49:51 +08:00
import { getUser } from '@/api/mp/user'
import profile from '@/assets/imgs/profile.jpg'
2023-04-22 20:51:25 +08:00
import { User } from './types'
2023-04-12 14:37:59 +08:00
const message = useMessage() // 消息弹窗
2023-04-12 14:37:59 +08:00
const props = defineProps({
userId: {
type: Number,
required: true
}
})
const accountId = ref(-1) // 公众号ID需要通过userId初始化
2023-04-12 14:37:59 +08:00
const loading = ref(false) // 消息列表是否正在加载中
2023-04-22 20:51:25 +08:00
const hasMore = ref(true) // 是否可以加载更多
2023-04-12 14:37:59 +08:00
const list = ref<any[]>([]) // 消息列表
const queryParams = reactive({
pageNo: 1, // 当前页数
pageSize: 14, // 每页显示多少条
2023-04-22 20:51:25 +08:00
accountId: accountId
2023-04-12 14:37:59 +08:00
})
// 由于微信不再提供昵称,直接使用“用户”展示
const user: User = reactive({
nickname: '用户',
avatar: profile,
2023-04-22 20:51:25 +08:00
accountId: accountId // 公众号账号编号
2023-04-12 14:37:59 +08:00
})
// ========= 消息发送 =========
const sendLoading = ref(false) // 发送消息是否加载中
// 微信发送消息
const reply = ref<Reply>({
2023-04-22 20:51:25 +08:00
type: ReplyType.Text,
accountId: -1,
2023-04-12 14:37:59 +08:00
articles: []
})
const replySelectRef = ref<InstanceType<typeof WxReplySelect> | null>(null) // WxReplySelect组件ref用于消息发送成功后清除内容
2023-05-04 22:45:12 +08:00
const msgDivRef = ref<HTMLDivElement | null>(null) // 消息显示窗口ref用于滚动到底部
2023-04-12 14:37:59 +08:00
/** 完成加载 */
onMounted(async () => {
const data = await getUser(props.userId)
user.nickname = data.nickname?.length > 0 ? data.nickname : user.nickname
user.avatar = user.avatar?.length > 0 ? data.avatar : user.avatar
2023-04-22 20:51:25 +08:00
accountId.value = data.accountId
reply.value.accountId = data.accountId
2023-04-12 14:37:59 +08:00
refreshChange()
})
// 执行发送
const sendMsg = async () => {
if (!unref(reply)) {
2023-04-12 14:37:59 +08:00
return
}
// 公众号限制:客服消息,公众号只允许发送一条
2023-04-22 20:51:25 +08:00
if (
reply.value.type === ReplyType.News &&
reply.value.articles &&
reply.value.articles.length > 1
) {
reply.value.articles = [reply.value.articles[0]]
2023-04-12 14:37:59 +08:00
message.success('图文消息条数限制在 1 条以内,已默认发送第一条')
}
const data = await sendMessage({ userId: props.userId, ...reply.value })
2023-04-12 14:37:59 +08:00
sendLoading.value = false
list.value = [...list.value, ...[data]]
2023-04-22 20:51:25 +08:00
await scrollToBottom()
2023-04-12 14:37:59 +08:00
// 发送后清空数据
replySelectRef.value?.clear()
2023-04-12 14:37:59 +08:00
}
2023-04-22 20:51:25 +08:00
const loadMore = () => {
2023-04-12 14:37:59 +08:00
queryParams.pageNo++
getPage(queryParams, null)
}
const getPage = async (page: any, params: any = null) => {
2023-04-12 14:37:59 +08:00
loading.value = true
let dataTemp = await getMessagePage(
Object.assign(
{
pageNo: page.pageNo,
pageSize: page.pageSize,
userId: props.userId,
accountId: page.accountId
},
params
)
)
const scrollHeight = msgDivRef.value?.scrollHeight ?? 0
2023-04-12 14:37:59 +08:00
// 处理数据
const data = dataTemp.list.reverse()
list.value = [...data, ...list.value]
loading.value = false
if (data.length < queryParams.pageSize || data.length === 0) {
2023-04-22 20:51:25 +08:00
hasMore.value = false
2023-04-12 14:37:59 +08:00
}
queryParams.pageNo = page.pageNo
queryParams.pageSize = page.pageSize
// 滚动到原来的位置
if (queryParams.pageNo === 1) {
// 定位到消息底部
2023-04-22 20:51:25 +08:00
await scrollToBottom()
2023-04-12 14:37:59 +08:00
} else if (data.length !== 0) {
// 定位滚动条
2023-04-22 20:51:25 +08:00
await nextTick()
if (scrollHeight !== 0) {
if (msgDivRef.value) {
msgDivRef.value.scrollTop = msgDivRef.value.scrollHeight - scrollHeight - 100
2023-04-12 14:37:59 +08:00
}
2023-04-22 20:51:25 +08:00
}
}
2023-04-12 14:37:59 +08:00
}
const refreshChange = () => {
getPage(queryParams)
2023-04-12 14:37:59 +08:00
}
/** 定位到消息底部 */
2023-04-22 20:51:25 +08:00
const scrollToBottom = async () => {
await nextTick()
if (msgDivRef.value) {
msgDivRef.value.scrollTop = msgDivRef.value.scrollHeight
2023-04-22 20:51:25 +08:00
}
2023-04-12 14:37:59 +08:00
}
</script>
2023-04-12 14:37:59 +08:00
<style lang="scss" scoped>
.msg-div {
height: 50vh;
overflow: auto;
background-color: #eaeaea;
margin-left: 10px;
margin-right: 10px;
}
.msg-send {
padding: 10px;
}
.send-but {
float: right;
margin-top: 8px;
margin-bottom: 8px;
}
</style>