index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view class="page_container">
  3. <custom-nav-bar :title="conversationData.showName" />
  4. <scroll-view id="scroll_view" :style="{ height: `1px` }" :scroll-top="scrollTop" scroll-y
  5. :scroll-into-view="scrollIntoView" upper-threshold="250" @scrolltoupper="fetchTopMessage"
  6. @scrolltolower="fetchBottomMessage">
  7. <view id="scroll_wrap">
  8. <u-loadmore nomoreText="" :status="topLoadMoreStatus" />
  9. <message-item-render v-for="(item, index) in chatLogs" :key="item.clientMsgID"
  10. :isActive="item.clientMsgID === jumpMessage.clientMsgID" isPreview :source="item"
  11. :preMessage="chatLogs[index - 1]" :isSender="item.sendID === storeCurrentUserID"
  12. @messageItemRender="messageItemRender" />
  13. </view>
  14. </scroll-view>
  15. <u-loading-page :loading="initLoading"></u-loading-page>
  16. </view>
  17. </template>
  18. <script>
  19. import IMSDK from "openim-uniapp-polyfill";
  20. import CustomNavBar from "../../../components/CustomNavBar/index.vue";
  21. import MessageItemRender from "../../../pages/conversation/chating/components/MessageItem/index.vue";
  22. import {
  23. mapGetters
  24. } from "vuex";
  25. export default {
  26. name: "",
  27. components: {
  28. CustomNavBar,
  29. MessageItemRender,
  30. },
  31. data() {
  32. return {
  33. conversationData: {},
  34. jumpMessage: {},
  35. chatLogs: [],
  36. scrollTop: 0,
  37. scrollIntoView: "",
  38. initLoading: true,
  39. topHasMore: true,
  40. topLoading: false,
  41. bottomHasMore: true,
  42. bottomLoading: false,
  43. };
  44. },
  45. computed: {
  46. ...mapGetters(["storeCurrentUserID"]),
  47. topLoadMoreStatus() {
  48. if (!this.topHasMore) {
  49. return "nomore";
  50. }
  51. return this.topLoading ? "loading" : "loadmore";
  52. },
  53. bottomLoadMoreStatus() {
  54. if (!this.bottomHasMore) {
  55. return "nomore";
  56. }
  57. return this.bottomLoading ? "loading" : "loadmore";
  58. },
  59. },
  60. onLoad(options) {
  61. if (options.conversationData) {
  62. this.conversationData = JSON.parse(options.conversationData);
  63. }
  64. if (options.jumpMessage) {
  65. this.jumpMessage = JSON.parse(options.jumpMessage);
  66. }
  67. this.initMessageData();
  68. },
  69. methods: {
  70. messageItemRender(clientMsgID) {
  71. if (clientMsgID === this.jumpMessage.clientMsgID) {
  72. this.$nextTick(() => {
  73. this.scrollIntoView = `auchor${this.jumpMessage.clientMsgID}`;
  74. this.initLoading = false;
  75. });
  76. }
  77. },
  78. async initMessageData() {
  79. const options = {
  80. startMessage: this.jumpMessage,
  81. viewType: 1,
  82. before: 20,
  83. after: 20
  84. };
  85. try {
  86. const {
  87. data
  88. } = await IMSDK.asyncApi(
  89. 'fetchSurroundingMessages',
  90. IMSDK.uuid(),
  91. options,
  92. )
  93. const targetIndex = data.messageList.findIndex((msg) => msg.clientMsgID === this.jumpMessage
  94. .clientMsgID);
  95. this.topHasMore = targetIndex >= 20;
  96. this.bottomHasMore = data.messageList.length - targetIndex - 1 >= 20;
  97. this.chatLogs = data.messageList;
  98. } catch (error) {
  99. console.log(error)
  100. }
  101. this.initLoading = false;
  102. },
  103. fetchTopMessage() {
  104. if (this.topLoading || !this.topHasMore) {
  105. return;
  106. }
  107. this.topLoading = true;
  108. const options = {
  109. conversationID: this.conversationData.conversationID,
  110. userID: "",
  111. groupID: "",
  112. count: 20,
  113. startClientMsgID: this.chatLogs[0].clientMsgID,
  114. viewType: 1,
  115. };
  116. IMSDK.asyncApi(
  117. IMSDK.IMMethods.GetAdvancedHistoryMessageList,
  118. IMSDK.uuid(),
  119. options,
  120. )
  121. .then(({
  122. data
  123. }) => {
  124. this.chatLogs = [...data.messageList, ...this.chatLogs];
  125. this.topHasMore = !data.isEnd;
  126. })
  127. .finally(() => (this.topLoading = false));
  128. },
  129. fetchBottomMessage() {
  130. if (this.bottomLoading || !this.bottomHasMore) {
  131. return;
  132. }
  133. this.bottomLoading = true;
  134. const options = {
  135. conversationID: this.conversationData.conversationID,
  136. userID: "",
  137. groupID: "",
  138. count: 20,
  139. startClientMsgID: this.chatLogs[this.chatLogs.length - 1].clientMsgID,
  140. viewType: 1,
  141. };
  142. IMSDK.asyncApi(
  143. IMSDK.IMMethods.GetAdvancedHistoryMessageListReverse,
  144. IMSDK.uuid(),
  145. options,
  146. )
  147. .then(({
  148. data
  149. }) => {
  150. this.chatLogs = [...this.chatLogs, ...data.messageList];
  151. this.bottomHasMore = !data.isEnd;
  152. })
  153. .finally(() => (this.bottomLoading = false));
  154. },
  155. },
  156. };
  157. </script>
  158. <style lang="scss" scoped>
  159. .page_container {
  160. #scroll_view {
  161. flex: 1;
  162. }
  163. }
  164. </style>