index.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. <template>
  2. <div
  3. :class="{
  4. 'tui-chat': true,
  5. 'tui-chat-h5': isMobile,
  6. }"
  7. @click="onMessageListBackgroundClick"
  8. >
  9. <!-- <JoinGroupCard /> -->
  10. <div class="tui-chat-main">
  11. <div
  12. v-if="isMerchantShow"
  13. class="tui-chat-safe-tips"
  14. style="text-align: right"
  15. @click="goGroup(currentConversationID)"
  16. >
  17. 进入店铺
  18. </div>
  19. <MessageGroupApplication
  20. v-if="isGroup"
  21. :key="props.groupID"
  22. :groupID="props.groupID"
  23. />
  24. <scroll-view
  25. id="messageScrollList"
  26. class="tui-message-list"
  27. style="background: #f1f1f1"
  28. scroll-y="true"
  29. :scroll-top="scrollTop"
  30. :scroll-into-view="`tui-${historyFirstMessageID}`"
  31. @scroll="handelScrollListScroll"
  32. >
  33. <p v-if="!isCompleted" class="message-more" @click="getHistoryMessageList">
  34. {{ TUITranslateService.t("TUIChat.查看更多") }}
  35. </p>
  36. <li
  37. v-for="(item, index) in messageList"
  38. :id="`tui-${item.ID}`"
  39. :key="item.vueForRenderKey"
  40. :class="'message-li ' + item.flow"
  41. >
  42. <MessageTimestamp
  43. :currTime="item.time"
  44. :prevTime="index > 0 ? messageList[index - 1].time : 0"
  45. />
  46. <div class="message-item" @click="toggleID = ''">
  47. <MessageTip
  48. v-if="item.type === TYPES.MSG_GRP_TIP || isCreateGroupCustomMessage(item)"
  49. :content="item.getMessageContent()"
  50. />
  51. <div
  52. v-else-if="!item.isRevoked && !isPluginMessage(item)"
  53. :id="`msg-bubble-${item.ID}`"
  54. class="message-bubble-container"
  55. @longpress="handleToggleMessageItem($event, item, index, true)"
  56. @touchstart="handleH5LongPress($event, item, index, 'touchstart')"
  57. @touchend="handleH5LongPress($event, item, index, 'touchend')"
  58. @mouseover="handleH5LongPress($event, item, index, 'touchend')"
  59. >
  60. <MessageBubble
  61. :messageItem="item"
  62. :content="item.getMessageContent()"
  63. :isAudioPlayed="audioPlayedMapping[item.ID]"
  64. :blinkMessageIDList="blinkMessageIDList"
  65. :isMultipleSelectMode="isMultipleSelectMode"
  66. :multipleSelectedMessageIDList="multipleSelectedMessageIDList"
  67. @resendMessage="resendMessage(item)"
  68. @blinkMessage="blinkMessage"
  69. @scrollTo="scrollTo"
  70. @changeSelectMessageIDList="changeSelectMessageIDList"
  71. @setReadReceiptPanelVisible="setReadReceiptPanelVisible"
  72. >
  73. <MessageText
  74. v-if="item.type === TYPES.MSG_TEXT"
  75. :content="item.getMessageContent()"
  76. />
  77. <ProgressMessage
  78. v-else-if="item.type === TYPES.MSG_IMAGE"
  79. :content="item.getMessageContent()"
  80. :messageItem="item"
  81. >
  82. <MessageImage
  83. :content="item.getMessageContent()"
  84. :messageItem="item"
  85. @previewImage="handleImagePreview(index)"
  86. />
  87. </ProgressMessage>
  88. <ProgressMessage
  89. v-else-if="item.type === TYPES.MSG_VIDEO"
  90. :content="item.getMessageContent()"
  91. :messageItem="item"
  92. >
  93. <MessageVideo :content="item.getMessageContent()" :messageItem="item" />
  94. </ProgressMessage>
  95. <MessageAudio
  96. v-else-if="item.type === TYPES.MSG_AUDIO"
  97. :content="item.getMessageContent()"
  98. :messageItem="item"
  99. :broadcastNewAudioSrc="broadcastNewAudioSrc"
  100. @setAudioPlayed="setAudioPlayed"
  101. @getGlobalAudioContext="getGlobalAudioContext"
  102. />
  103. <MessageRecord
  104. v-else-if="item.type === TYPES.MSG_MERGER"
  105. :renderData="item.payload"
  106. :messageItem="item"
  107. @assignMessageIDInUniapp="assignMessageIDInUniapp"
  108. />
  109. <MessageFile
  110. v-else-if="item.type === TYPES.MSG_FILE"
  111. :content="item.getMessageContent()"
  112. />
  113. <MessageFace
  114. v-else-if="item.type === TYPES.MSG_FACE"
  115. :content="item.getMessageContent()"
  116. />
  117. <MessageLocation
  118. v-else-if="item.type === TYPES.MSG_LOCATION"
  119. :content="item.getMessageContent()"
  120. />
  121. <MessageCustom
  122. class="MessageCustom"
  123. v-else-if="item.payload.data"
  124. :content="item.getMessageContent()"
  125. :messageItem="item"
  126. />
  127. </MessageBubble>
  128. </div>
  129. <MessagePlugin
  130. v-else-if="!item.isRevoked && isPluginMessage(item)"
  131. :message="item"
  132. @resendMessage="resendMessage"
  133. @handleToggleMessageItem="handleToggleMessageItem"
  134. @handleH5LongPress="handleH5LongPress"
  135. />
  136. <MessageRevoked
  137. v-else
  138. :isEdit="item.type === TYPES.MSG_TEXT"
  139. :messageItem="item"
  140. @messageEdit="handleEdit(item)"
  141. />
  142. <!-- message tool -->
  143. <MessageTool
  144. v-if="item.ID === toggleID"
  145. :class="{
  146. 'message-tool': true,
  147. 'message-tool-out': item.flow === 'out',
  148. 'message-tool-in': item.flow === 'in',
  149. }"
  150. :messageItem="item"
  151. :isMultipleSelectMode="isMultipleSelectMode"
  152. @toggleMultipleSelectMode="() => emits('toggleMultipleSelectMode')"
  153. />
  154. </div>
  155. </li>
  156. </scroll-view>
  157. <!-- scroll button -->
  158. <ScrollButton
  159. ref="scrollButtonInstanceRef"
  160. @scrollToLatestMessage="scrollToLatestMessage"
  161. />
  162. <Dialog
  163. v-if="reSendDialogShow"
  164. :show="reSendDialogShow"
  165. :isH5="!isPC"
  166. :center="true"
  167. :isHeaderShow="isPC"
  168. @submit="resendMessageConfirm()"
  169. @update:show="(e) => (reSendDialogShow = e)"
  170. >
  171. <p class="delDialog-title">
  172. {{ TUITranslateService.t("TUIChat.确认重发该消息?") }}
  173. </p>
  174. </Dialog>
  175. <!-- read receipt panel -->
  176. <ReadReceiptPanel
  177. v-if="isShowReadUserStatusPanel"
  178. :message="Object.assign({}, readStatusMessage)"
  179. @setReadReceiptPanelVisible="setReadReceiptPanelVisible"
  180. />
  181. <!-- simple message list -->
  182. <Drawer
  183. :visible="isShowSimpleMessageList"
  184. :overlayColor="'transparent'"
  185. :popDirection="'right'"
  186. >
  187. <SimpleMessageList
  188. :style="{ height: '100%' }"
  189. :isMounted="isShowSimpleMessageList"
  190. :messageID="simpleMessageListRenderMessageID"
  191. @closeOverlay="isShowSimpleMessageList = false"
  192. />
  193. </Drawer>
  194. </div>
  195. </div>
  196. </template>
  197. <script lang="ts" setup>
  198. import {
  199. ref,
  200. watch,
  201. nextTick,
  202. onMounted,
  203. onUnmounted,
  204. getCurrentInstance,
  205. } from "../../../adapter-vue";
  206. import TUIChatEngine, {
  207. IMessageModel,
  208. TUIStore,
  209. StoreName,
  210. TUITranslateService,
  211. TUIChatService,
  212. } from "@tencentcloud/chat-uikit-engine";
  213. import { throttle } from "lodash";
  214. import {
  215. setInstanceMapping,
  216. getBoundingClientRect,
  217. getScrollInfo,
  218. } from "@tencentcloud/universal-api";
  219. // import { JoinGroupCard } from '@tencentcloud/call-uikit-wechat';
  220. import Link from "./link";
  221. import SimpleMessageList from "./message-elements/simple-message-list/index.vue";
  222. import MessageGroupApplication from "./message-group-application/index.vue";
  223. import MessageText from "./message-elements/message-text.vue";
  224. import MessageImage from "./message-elements/message-image.vue";
  225. import MessageAudio from "./message-elements/message-audio.vue";
  226. import MessageRecord from "./message-elements/message-record/index.vue";
  227. import MessageFile from "./message-elements/message-file.vue";
  228. import MessageFace from "./message-elements/message-face.vue";
  229. import MessageCustom from "./message-elements/message-custom.vue";
  230. import MessageTip from "./message-elements/message-tip.vue";
  231. import MessageBubble from "./message-elements/message-bubble.vue";
  232. import MessageLocation from "./message-elements/message-location.vue";
  233. import MessageTimestamp from "./message-elements/message-timestamp.vue";
  234. import MessageVideo from "./message-elements/message-video.vue";
  235. import MessageTool from "./message-tool/index.vue";
  236. import MessageRevoked from "./message-tool/message-revoked.vue";
  237. import MessagePlugin from "../../../plugins/plugin-components/message-plugin.vue";
  238. import ReadReceiptPanel from "./read-receipt-panel/index.vue";
  239. import ScrollButton from "./scroll-button/index.vue";
  240. import { isPluginMessage } from "../../../plugins/plugin-components/index";
  241. import Dialog from "../../common/Dialog/index.vue";
  242. import Drawer from "../../common/Drawer/index.vue";
  243. import { Toast, TOAST_TYPE } from "../../common/Toast/index";
  244. import ProgressMessage from "../../common/ProgressMessage/index.vue";
  245. import { isCreateGroupCustomMessage } from "../utils/utils";
  246. import { isEnabledMessageReadReceiptGlobal } from "../utils/utils";
  247. import { isPC, isH5, isMobile } from "../../../utils/env";
  248. import chatStorage from "../utils/chatStorage";
  249. import { IAudioContext } from "../../../interface";
  250. import { requestConfig } from "../../../../app.config";
  251. import * as mesApi from "../../../../api/message/index";
  252. interface IEmits {
  253. (e: "closeInputToolBar"): void;
  254. (e: "handleEditor", message: IMessageModel, type: string): void;
  255. (key: "toggleMultipleSelectMode"): void;
  256. }
  257. interface IProps {
  258. isGroup: boolean;
  259. groupID: string;
  260. isNotInGroup: boolean;
  261. isMultipleSelectMode: boolean;
  262. }
  263. const emits = defineEmits<IEmits>();
  264. const props = withDefaults(defineProps<IProps>(), {
  265. isGroup: false,
  266. groupID: "",
  267. isNotInGroup: false,
  268. isMultipleSelectMode: false,
  269. });
  270. let selfAddValue = 0;
  271. let observer: any = null;
  272. let groupType: string | undefined;
  273. const sentReceiptMessageID = new Set<string>();
  274. const isOfficial = TUIStore.getData(StoreName.APP, "isOfficial");
  275. const thisInstance = getCurrentInstance()?.proxy || getCurrentInstance();
  276. const messageList = ref<IMessageModel[]>();
  277. const multipleSelectedMessageIDList = ref<string[]>([]);
  278. const isCompleted = ref(false);
  279. const currentConversationID = ref("");
  280. const toggleID = ref("");
  281. const scrollTop = ref(5000); // The initial number of messages is 15, and the maximum message height is 300.
  282. const TYPES = ref(TUIChatEngine.TYPES);
  283. const isLoadingMessage = ref(false);
  284. const isLongpressing = ref(false);
  285. const blinkMessageIDList = ref<string[]>([]);
  286. const messageTarget = ref<IMessageModel>();
  287. const scrollButtonInstanceRef = ref<InstanceType<typeof ScrollButton>>();
  288. const historyFirstMessageID = ref<string>("");
  289. const isShowSimpleMessageList = ref<boolean>(false);
  290. const simpleMessageListRenderMessageID = ref<string>();
  291. const audioPlayedMapping = ref<Record<string, boolean>>({});
  292. // audio control
  293. const broadcastNewAudioSrc = ref<string>("");
  294. const readStatusMessage = ref<IMessageModel>();
  295. const isShowReadUserStatusPanel = ref<boolean>(false);
  296. // Resend Message Dialog
  297. const reSendDialogShow = ref(false);
  298. const resendMessageData = ref();
  299. const scrollToBottom = () => {
  300. scrollTop.value += 300;
  301. // Solve the issue where swiping to the bottom for the first time after packaging Uniapp into an app has a delay,
  302. // which can be set to 300 ms.
  303. const timer = setTimeout(() => {
  304. scrollTop.value += 1;
  305. clearTimeout(timer);
  306. }, 300);
  307. };
  308. const onCurrentConversationIDUpdated = (conversationID: string) => {
  309. currentConversationID.value = conversationID;
  310. if (isEnabledMessageReadReceiptGlobal()) {
  311. const { groupProfile } = TUIStore.getConversationModel(conversationID) || {};
  312. groupType = groupProfile?.type;
  313. }
  314. if (Object.keys(audioPlayedMapping.value).length > 0) {
  315. // Synchronize storage about whether the audio has been played when converstaion switched
  316. chatStorage.setChatStorage("audioPlayedMapping", audioPlayedMapping.value);
  317. }
  318. };
  319. onMounted(() => {
  320. // Retrieve the information about whether the audio has been played from localStorage
  321. audioPlayedMapping.value = chatStorage.getChatStorage("audioPlayedMapping") || {};
  322. TUIStore.watch(StoreName.CHAT, {
  323. messageList: onMessageListUpdated,
  324. messageSource: onMessageSourceUpdated,
  325. isCompleted: onChatCompletedUpdated,
  326. });
  327. TUIStore.watch(StoreName.CONV, {
  328. currentConversationID: onCurrentConversationIDUpdated,
  329. });
  330. setInstanceMapping("messageList", thisInstance);
  331. uni.$on("scroll-to-bottom", scrollToLatestMessage);
  332. let personId = uni.getStorageSync("personId");
  333. if (personId != "001") {
  334. isMerchant();
  335. }
  336. });
  337. const goGroup = async () => {
  338. let id = currentConversationID.value.slice(3);
  339. let res = await mesApi.default.getByOwnerId(id);
  340. console.log("123123", res);
  341. if (res.code == 200) {
  342. webUni.webView.navigateTo({
  343. url: `/subpages/my/product-orders/in-store-look?shopId=` + res.data.id,
  344. });
  345. }
  346. };
  347. const GetQueryString = (name: string) => {
  348. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  349. var r = window.location.search.substr(1).match(reg);
  350. if (r != null) return unescape(r[2]);
  351. return null;
  352. };
  353. const isMerchantShow = ref(false);
  354. const isMerchant = async () => {
  355. if (currentConversationID.value.includes("GROUP")) {
  356. isMerchantShow.value = false;
  357. } else {
  358. // let id = currentConversationID.value.slice(3);
  359. let id = GetQueryString("id");
  360. let res = await mesApi.default.getUserInfo(id);
  361. if (res.data.userTypes.includes("2")) {
  362. isMerchantShow.value = true;
  363. } else {
  364. isMerchantShow.value = false;
  365. }
  366. }
  367. console.log("xxxxx");
  368. };
  369. onUnmounted(() => {
  370. TUIStore.unwatch(StoreName.CHAT, {
  371. messageList: onMessageListUpdated,
  372. isCompleted: onChatCompletedUpdated,
  373. });
  374. TUIStore.unwatch(StoreName.CONV, {
  375. currentConversationID: onCurrentConversationIDUpdated,
  376. });
  377. observer?.disconnect();
  378. observer = null;
  379. uni.$off("scroll-to-bottom");
  380. if (Object.keys(audioPlayedMapping.value).length > 0) {
  381. // Synchronize storage about whether the audio has been played when the component is unmounted
  382. chatStorage.setChatStorage("audioPlayedMapping", audioPlayedMapping.value);
  383. }
  384. });
  385. const handelScrollListScroll = throttle(
  386. function (e: Event) {
  387. scrollButtonInstanceRef.value?.judgeScrollOverOneScreen(e);
  388. },
  389. 500,
  390. { leading: true }
  391. );
  392. function getGlobalAudioContext(
  393. audioMap: Map<string, IAudioContext>,
  394. options?: { newAudioSrc: string }
  395. ) {
  396. if (options?.newAudioSrc) {
  397. broadcastNewAudioSrc.value = options.newAudioSrc;
  398. }
  399. }
  400. async function onMessageListUpdated(list: IMessageModel[]) {
  401. observer?.disconnect();
  402. messageList.value = list
  403. .filter((message) => !message.isDeleted)
  404. .map((message) => {
  405. message.vueForRenderKey = `${message.ID}`;
  406. return message;
  407. });
  408. const newLastMessage = messageList.value?.[messageList.value?.length - 1];
  409. if (messageTarget.value) {
  410. // scroll to target message
  411. scrollAndBlinkMessage(messageTarget.value);
  412. } else if (
  413. !isLoadingMessage.value &&
  414. !(
  415. scrollButtonInstanceRef.value?.isScrollButtonVisible &&
  416. newLastMessage?.flow === "in"
  417. )
  418. ) {
  419. // scroll to bottom
  420. nextTick(() => {
  421. scrollToBottom();
  422. });
  423. }
  424. if (isEnabledMessageReadReceiptGlobal()) {
  425. nextTick(() => bindIntersectionObserver());
  426. }
  427. }
  428. async function scrollToLatestMessage() {
  429. try {
  430. const { scrollHeight } = await getScrollInfo("#messageScrollList", "messageList");
  431. if (scrollHeight) {
  432. scrollTop.value === scrollHeight
  433. ? (scrollTop.value = scrollHeight + 1)
  434. : (scrollTop.value = scrollHeight);
  435. } else {
  436. scrollToBottom();
  437. }
  438. } catch (error) {
  439. scrollToBottom();
  440. }
  441. }
  442. async function onMessageSourceUpdated(message: IMessageModel) {
  443. messageTarget.value = message;
  444. scrollAndBlinkMessage(messageTarget.value);
  445. }
  446. function scrollAndBlinkMessage(message: IMessageModel) {
  447. if (messageList.value?.some((messageListItem) => messageListItem?.ID === message?.ID)) {
  448. nextTick(async () => {
  449. await scrollToTargetMessage(message);
  450. await blinkMessage(message?.ID);
  451. messageTarget.value = undefined;
  452. });
  453. }
  454. }
  455. function onChatCompletedUpdated(flag: boolean) {
  456. isCompleted.value = flag;
  457. }
  458. const getHistoryMessageList = () => {
  459. isLoadingMessage.value = true;
  460. const currentFirstMessageID = messageList.value?.[0]?.ID || "";
  461. TUIChatService.getMessageList().then(() => {
  462. nextTick(() => {
  463. historyFirstMessageID.value = currentFirstMessageID;
  464. const timer = setTimeout(() => {
  465. historyFirstMessageID.value = "";
  466. isLoadingMessage.value = false;
  467. clearTimeout(timer);
  468. }, 500);
  469. });
  470. });
  471. };
  472. const openComplaintLink = () => {};
  473. // toggle message
  474. const handleToggleMessageItem = (
  475. e: any,
  476. message: IMessageModel,
  477. index: number,
  478. isLongpress = false
  479. ) => {
  480. if (props.isMultipleSelectMode || props.isNotInGroup) {
  481. return;
  482. }
  483. if (isLongpress) {
  484. isLongpressing.value = true;
  485. }
  486. toggleID.value = message.ID;
  487. };
  488. // h5 long press
  489. let timer: number;
  490. const handleH5LongPress = (
  491. e: any,
  492. message: IMessageModel,
  493. index: number,
  494. type: string
  495. ) => {
  496. if (props.isMultipleSelectMode || props.isNotInGroup) {
  497. return;
  498. }
  499. if (!isH5) return;
  500. function longPressHandler() {
  501. clearTimeout(timer);
  502. handleToggleMessageItem(e, message, index, true);
  503. }
  504. function touchStartHandler() {
  505. timer = setTimeout(longPressHandler, 500);
  506. }
  507. function touchEndHandler() {
  508. clearTimeout(timer);
  509. }
  510. switch (type) {
  511. case "touchstart":
  512. touchStartHandler();
  513. break;
  514. case "touchend":
  515. touchEndHandler();
  516. setTimeout(() => {
  517. isLongpressing.value = false;
  518. }, 200);
  519. break;
  520. }
  521. };
  522. // reedit message
  523. const handleEdit = (message: IMessageModel) => {
  524. emits("handleEditor", message, "reedit");
  525. };
  526. const resendMessage = (message: IMessageModel) => {
  527. reSendDialogShow.value = true;
  528. resendMessageData.value = message;
  529. };
  530. const handleImagePreview = (index: number) => {
  531. if (!messageList.value) {
  532. return;
  533. }
  534. const imageMessageIndex: number[] = [];
  535. const imageMessageList: IMessageModel[] = messageList.value.filter((item, index) => {
  536. if (!item.isRevoked && !item.hasRiskContent && item.type === TYPES.value.MSG_IMAGE) {
  537. imageMessageIndex.push(index);
  538. return true;
  539. }
  540. return false;
  541. });
  542. uni.previewImage({
  543. current: imageMessageIndex.indexOf(index),
  544. urls: imageMessageList.map((message) => message.payload.imageInfoArray?.[2].url),
  545. // #ifdef APP-PLUS
  546. indicator: "number",
  547. // #endif
  548. });
  549. };
  550. const resendMessageConfirm = () => {
  551. reSendDialogShow.value = !reSendDialogShow.value;
  552. const messageModel = resendMessageData.value;
  553. messageModel.resendMessage();
  554. };
  555. function blinkMessage(messageID: string): Promise<void> {
  556. return new Promise((resolve) => {
  557. const index = blinkMessageIDList.value.indexOf(messageID);
  558. if (index < 0) {
  559. blinkMessageIDList.value.push(messageID);
  560. const timer = setTimeout(() => {
  561. blinkMessageIDList.value.splice(blinkMessageIDList.value.indexOf(messageID), 1);
  562. clearTimeout(timer);
  563. resolve();
  564. }, 3000);
  565. }
  566. });
  567. }
  568. function scrollTo(scrollHeight: number) {
  569. scrollTop.value = scrollHeight;
  570. }
  571. async function bindIntersectionObserver() {
  572. if (!messageList.value || messageList.value.length === 0) {
  573. return;
  574. }
  575. if (
  576. groupType === TYPES.value.GRP_AVCHATROOM ||
  577. groupType === TYPES.value.GRP_COMMUNITY
  578. ) {
  579. // AVCHATROOM and COMMUNITY chats do not monitor read receipts for messages.
  580. return;
  581. }
  582. observer?.disconnect();
  583. observer = uni
  584. .createIntersectionObserver(thisInstance, {
  585. threshold: [0.7],
  586. observeAll: true,
  587. // In Uni-app, the `safetip` is also included, so a negative margin is needed to exclude it.
  588. })
  589. .relativeTo("#messageScrollList", { top: -70 });
  590. observer?.observe(".message-li.in .message-bubble-container", (res: any) => {
  591. if (sentReceiptMessageID.has(res.id)) {
  592. return;
  593. }
  594. const matchingMessage = messageList.value.find((message: IMessageModel) => {
  595. return res.id.indexOf(message.ID) > -1;
  596. });
  597. if (
  598. matchingMessage &&
  599. matchingMessage.needReadReceipt &&
  600. matchingMessage.flow === "in" &&
  601. !matchingMessage.readReceiptInfo?.isPeerRead
  602. ) {
  603. TUIChatService.sendMessageReadReceipt([matchingMessage]);
  604. sentReceiptMessageID.add(res.id);
  605. }
  606. });
  607. }
  608. function setReadReceiptPanelVisible(visible: boolean, message?: IMessageModel) {
  609. if (visible && props.isNotInGroup) {
  610. return;
  611. }
  612. if (!visible) {
  613. readStatusMessage.value = undefined;
  614. } else {
  615. readStatusMessage.value = message;
  616. }
  617. isShowReadUserStatusPanel.value = visible;
  618. }
  619. async function scrollToTargetMessage(message: IMessageModel) {
  620. const targetMessageID = message.ID;
  621. const isTargetMessageInScreen =
  622. messageList.value && messageList.value.some((msg) => msg.ID === targetMessageID);
  623. if (targetMessageID && isTargetMessageInScreen) {
  624. const timer = setTimeout(async () => {
  625. try {
  626. const scrollViewRect = await getBoundingClientRect(
  627. "#messageScrollList",
  628. "messageList"
  629. );
  630. const originalMessageRect = await getBoundingClientRect(
  631. "#tui-" + targetMessageID,
  632. "messageList"
  633. );
  634. const { scrollTop } = await getScrollInfo("#messageScrollList", "messageList");
  635. const finalScrollTop =
  636. originalMessageRect.top + scrollTop - scrollViewRect.top - (selfAddValue++ % 2);
  637. scrollTo(finalScrollTop);
  638. clearTimeout(timer);
  639. } catch (error) {
  640. // todo
  641. }
  642. }, 500);
  643. } else {
  644. Toast({
  645. message: TUITranslateService.t("TUIChat.无法定位到原消息"),
  646. type: TOAST_TYPE.WARNING,
  647. });
  648. }
  649. }
  650. function onMessageListBackgroundClick() {
  651. emits("closeInputToolBar");
  652. }
  653. watch(
  654. () => props.isMultipleSelectMode,
  655. (newValue) => {
  656. if (!newValue) {
  657. changeSelectMessageIDList({
  658. type: "clearAll",
  659. messageID: "",
  660. });
  661. }
  662. }
  663. );
  664. function changeSelectMessageIDList({
  665. type,
  666. messageID,
  667. }: {
  668. type: "add" | "remove" | "clearAll";
  669. messageID: string;
  670. }) {
  671. // TODO need to delete this
  672. if (type === "clearAll") {
  673. multipleSelectedMessageIDList.value = [];
  674. } else if (type === "add" && !multipleSelectedMessageIDList.value.includes(messageID)) {
  675. multipleSelectedMessageIDList.value.push(messageID);
  676. } else if (type === "remove") {
  677. multipleSelectedMessageIDList.value = multipleSelectedMessageIDList.value.filter(
  678. (id) => id !== messageID
  679. );
  680. }
  681. }
  682. function mergeForwardMessage() {
  683. TUIStore.update(StoreName.CUSTOM, "multipleForwardMessageID", {
  684. isMergeForward: true,
  685. messageIDList: multipleSelectedMessageIDList.value,
  686. });
  687. }
  688. function oneByOneForwardMessage() {
  689. TUIStore.update(StoreName.CUSTOM, "multipleForwardMessageID", {
  690. isMergeForward: false,
  691. messageIDList: multipleSelectedMessageIDList.value,
  692. });
  693. }
  694. function assignMessageIDInUniapp(messageID: string) {
  695. simpleMessageListRenderMessageID.value = messageID;
  696. isShowSimpleMessageList.value = true;
  697. }
  698. function setAudioPlayed(messageID: string) {
  699. audioPlayedMapping.value[messageID] = true;
  700. }
  701. defineExpose({
  702. oneByOneForwardMessage,
  703. mergeForwardMessage,
  704. scrollToLatestMessage,
  705. });
  706. </script>
  707. <style lang="scss" scoped src="./style/index.scss"></style>