main.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!--
  2. - Copyright (C) 2018-2019
  3. - All rights reserved, Designed By www.joolun.com
  4. 芋道源码:
  5. ① 移除暂时用不到的 websocket
  6. ② 代码优化,补充注释,提升阅读性
  7. -->
  8. <template>
  9. <ContentWrap>
  10. <div class="msg-div" ref="msgDivRef">
  11. <!-- 加载更多 -->
  12. <div v-loading="loading"></div>
  13. <div v-if="!loading">
  14. <div class="el-table__empty-block" v-if="hasMore" @click="loadMore"
  15. ><span class="el-table__empty-text">点击加载更多</span></div
  16. >
  17. <div class="el-table__empty-block" v-if="!hasMore"
  18. ><span class="el-table__empty-text">没有更多了</span></div
  19. >
  20. </div>
  21. <!-- 消息列表 -->
  22. <MsgList :list="list" :account-id="accountId" :user="user" />
  23. </div>
  24. <div class="msg-send" v-loading="sendLoading">
  25. <WxReplySelect ref="replySelectRef" v-model="reply" />
  26. <el-button type="success" class="send-but" @click="sendMsg">发送(S)</el-button>
  27. </div>
  28. </ContentWrap>
  29. </template>
  30. <script setup lang="ts" name="WxMsg">
  31. import WxReplySelect, { Reply, ReplyType } from '@/views/mp/components/wx-reply'
  32. import MsgList from './components/MsgList.vue'
  33. import { getMessagePage, sendMessage } from '@/api/mp/message'
  34. import { getUser } from '@/api/mp/user'
  35. import profile from '@/assets/imgs/profile.jpg'
  36. import { User } from './types'
  37. const message = useMessage() // 消息弹窗
  38. const props = defineProps({
  39. userId: {
  40. type: Number,
  41. required: true
  42. }
  43. })
  44. const accountId = ref(-1) // 公众号ID,需要通过userId初始化
  45. const loading = ref(false) // 消息列表是否正在加载中
  46. const hasMore = ref(true) // 是否可以加载更多
  47. const list = ref<any[]>([]) // 消息列表
  48. const queryParams = reactive({
  49. pageNo: 1, // 当前页数
  50. pageSize: 14, // 每页显示多少条
  51. accountId: accountId
  52. })
  53. // 由于微信不再提供昵称,直接使用“用户”展示
  54. const user: User = reactive({
  55. nickname: '用户',
  56. avatar: profile,
  57. accountId: accountId // 公众号账号编号
  58. })
  59. // ========= 消息发送 =========
  60. const sendLoading = ref(false) // 发送消息是否加载中
  61. // 微信发送消息
  62. const reply = ref<Reply>({
  63. type: ReplyType.Text,
  64. accountId: -1,
  65. articles: []
  66. })
  67. const replySelectRef = ref<InstanceType<typeof WxReplySelect> | null>(null) // WxReplySelect组件ref,用于消息发送成功后清除内容
  68. const msgDivRef = ref<HTMLDivElement | null>(null) // 消息显示窗口ref,用于滚动到底部
  69. /** 完成加载 */
  70. onMounted(async () => {
  71. const data = await getUser(props.userId)
  72. user.nickname = data.nickname?.length > 0 ? data.nickname : user.nickname
  73. user.avatar = user.avatar?.length > 0 ? data.avatar : user.avatar
  74. accountId.value = data.accountId
  75. reply.value.accountId = data.accountId
  76. refreshChange()
  77. })
  78. // 执行发送
  79. const sendMsg = async () => {
  80. if (!unref(reply)) {
  81. return
  82. }
  83. // 公众号限制:客服消息,公众号只允许发送一条
  84. if (
  85. reply.value.type === ReplyType.News &&
  86. reply.value.articles &&
  87. reply.value.articles.length > 1
  88. ) {
  89. reply.value.articles = [reply.value.articles[0]]
  90. message.success('图文消息条数限制在 1 条以内,已默认发送第一条')
  91. }
  92. const data = await sendMessage({ userId: props.userId, ...reply.value })
  93. sendLoading.value = false
  94. list.value = [...list.value, ...[data]]
  95. await scrollToBottom()
  96. // 发送后清空数据
  97. replySelectRef.value?.clear()
  98. }
  99. const loadMore = () => {
  100. queryParams.pageNo++
  101. getPage(queryParams, null)
  102. }
  103. const getPage = async (page: any, params: any = null) => {
  104. loading.value = true
  105. let dataTemp = await getMessagePage(
  106. Object.assign(
  107. {
  108. pageNo: page.pageNo,
  109. pageSize: page.pageSize,
  110. userId: props.userId,
  111. accountId: page.accountId
  112. },
  113. params
  114. )
  115. )
  116. const scrollHeight = msgDivRef.value?.scrollHeight ?? 0
  117. // 处理数据
  118. const data = dataTemp.list.reverse()
  119. list.value = [...data, ...list.value]
  120. loading.value = false
  121. if (data.length < queryParams.pageSize || data.length === 0) {
  122. hasMore.value = false
  123. }
  124. queryParams.pageNo = page.pageNo
  125. queryParams.pageSize = page.pageSize
  126. // 滚动到原来的位置
  127. if (queryParams.pageNo === 1) {
  128. // 定位到消息底部
  129. await scrollToBottom()
  130. } else if (data.length !== 0) {
  131. // 定位滚动条
  132. await nextTick()
  133. if (scrollHeight !== 0) {
  134. if (msgDivRef.value) {
  135. msgDivRef.value.scrollTop = msgDivRef.value.scrollHeight - scrollHeight - 100
  136. }
  137. }
  138. }
  139. }
  140. const refreshChange = () => {
  141. getPage(queryParams)
  142. }
  143. /** 定位到消息底部 */
  144. const scrollToBottom = async () => {
  145. await nextTick()
  146. if (msgDivRef.value) {
  147. msgDivRef.value.scrollTop = msgDivRef.value.scrollHeight
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. .msg-div {
  153. height: 50vh;
  154. overflow: auto;
  155. background-color: #eaeaea;
  156. margin-left: 10px;
  157. margin-right: 10px;
  158. }
  159. .msg-send {
  160. padding: 10px;
  161. }
  162. .send-but {
  163. float: right;
  164. margin-top: 8px;
  165. margin-bottom: 8px;
  166. }
  167. </style>