index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <div ref="conversationListInnerDomRef" class="tui-conversation-list">
  3. <ActionsMenu
  4. v-if="isShowOverlay"
  5. :selectedConversation="currentConversation"
  6. :actionsMenuPosition="actionsMenuPosition"
  7. :selectedConversationDomRect="currentConversationDomRect"
  8. @closeConversationActionMenu="closeConversationActionMenu"
  9. />
  10. <div
  11. v-for="(conversation, index) in conversationList"
  12. :id="`convlistitem-${index}`"
  13. :key="index"
  14. :class="[
  15. 'tui-conversation-content',
  16. isMobile && 'tui-conversation-content-h5 disable-select',
  17. ]"
  18. >
  19. <div
  20. :class="[
  21. isPC && 'isPC',
  22. 'tui-conversation-item',
  23. currentConversationID === conversation.conversationID &&
  24. 'tui-conversation-item-selected',
  25. conversation.isPinned && 'tui-conversation-item-pinned',
  26. ]"
  27. @click="enterConversationChat(conversation.conversationID, conversation)"
  28. @longpress="showConversationActionMenu($event, conversation, index)"
  29. @contextmenu="showConversationActionMenu($event, conversation, index, true)"
  30. >
  31. <aside class="left">
  32. <Avatar useSkeletonAnimation :url="conversation.getAvatar()" size="60px" />
  33. <div
  34. v-if="userOnlineStatusMap && isShowUserOnlineStatus(conversation)"
  35. :class="[
  36. 'online-status',
  37. Object.keys(userOnlineStatusMap).length > 0 &&
  38. Object.keys(userOnlineStatusMap).includes(
  39. conversation.userProfile.userID
  40. ) &&
  41. userOnlineStatusMap[conversation.userProfile.userID].statusType === 1
  42. ? 'online-status-online'
  43. : 'online-status-offline',
  44. ]"
  45. />
  46. <span v-if="conversation.unreadCount > 0 && !conversation.isMuted" class="num">
  47. {{ conversation.unreadCount > 99 ? "99+" : conversation.unreadCount }}
  48. </span>
  49. <span
  50. v-if="conversation.unreadCount > 0 && conversation.isMuted"
  51. class="num-notify"
  52. />
  53. </aside>
  54. <div class="content">
  55. <div class="content-header">
  56. <label class="content-header-label">
  57. <p class="name">{{ conversation.getShowName() }}</p>
  58. </label>
  59. <div class="middle-box">
  60. <span
  61. v-if="
  62. conversation.draftText &&
  63. conversation.conversationID !== currentConversationID
  64. "
  65. class="middle-box-draft"
  66. >{{ TUITranslateService.t("TUIChat.[草稿]") }}</span
  67. >
  68. <span
  69. v-else-if="
  70. conversation.type === 'GROUP' &&
  71. conversation.groupAtInfoList &&
  72. conversation.groupAtInfoList.length > 0
  73. "
  74. class="middle-box-at"
  75. >{{ conversation.getGroupAtInfo() }}</span
  76. >
  77. <div class="middle-box-content">
  78. {{ conversation.getLastMessage("text") }}
  79. </div>
  80. </div>
  81. </div>
  82. <div class="content-footer">
  83. <span class="time">{{ conversation.getLastMessage("time") }}</span>
  84. <Icon v-if="conversation.isMuted" :file="muteIcon" />
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. </template>
  91. <script lang="ts" setup>
  92. interface IUserStatus {
  93. statusType: number;
  94. customStatus: string;
  95. }
  96. interface IUserStatusMap {
  97. [userID: string]: IUserStatus;
  98. }
  99. import { ref, onMounted, onUnmounted } from "../../../adapter-vue";
  100. import TUIChatEngine, {
  101. TUIStore,
  102. StoreName,
  103. TUIConversationService,
  104. TUITranslateService,
  105. IConversationModel,
  106. } from "@tencentcloud/chat-uikit-engine";
  107. import { TUIGlobal, isIOS, addLongPressListener } from "@tencentcloud/universal-api";
  108. import Icon from "../../common/Icon.vue";
  109. import Avatar from "../../common/Avatar/index.vue";
  110. import ActionsMenu from "../actions-menu/index.vue";
  111. import muteIcon from "../../../assets/icon/mute.svg";
  112. import { isPC, isH5, isUniFrameWork, isMobile } from "../../../utils/env";
  113. const emits = defineEmits(["handleSwitchConversation", "getPassingRef"]);
  114. const currentConversation = ref<IConversationModel>();
  115. const currentConversationID = ref<string>();
  116. const currentConversationDomRect = ref<DOMRect>();
  117. const isShowOverlay = ref<boolean>(false);
  118. const conversationList = ref<IConversationModel[]>([]);
  119. const conversationListDomRef = ref<HTMLElement | undefined>();
  120. const conversationListInnerDomRef = ref<HTMLElement | undefined>();
  121. const actionsMenuPosition = ref<{
  122. top: number;
  123. left: number | undefined;
  124. conversationHeight: number | undefined;
  125. }>({
  126. top: 0,
  127. left: undefined,
  128. conversationHeight: undefined,
  129. });
  130. const displayOnlineStatus = ref(false);
  131. const userOnlineStatusMap = ref<IUserStatusMap>();
  132. let lastestOpenActionsMenuTime: number | null = null;
  133. onMounted(() => {
  134. TUIStore.watch(StoreName.CONV, {
  135. currentConversationID: onCurrentConversationIDUpdated,
  136. conversationList: onConversationListUpdated,
  137. currentConversation: onCurrentConversationUpdated,
  138. });
  139. TUIStore.watch(StoreName.USER, {
  140. displayOnlineStatus: onDisplayOnlineStatusUpdated,
  141. userStatusList: onUserStatusListUpdated,
  142. });
  143. if (!isUniFrameWork && isIOS && !isPC) {
  144. addLongPressHandler();
  145. }
  146. });
  147. onUnmounted(() => {
  148. TUIStore.unwatch(StoreName.CONV, {
  149. currentConversationID: onCurrentConversationIDUpdated,
  150. conversationList: onConversationListUpdated,
  151. currentConversation: onCurrentConversationUpdated,
  152. });
  153. TUIStore.unwatch(StoreName.USER, {
  154. displayOnlineStatus: onDisplayOnlineStatusUpdated,
  155. userStatusList: onUserStatusListUpdated,
  156. });
  157. });
  158. const isShowUserOnlineStatus = (conversation: IConversationModel): boolean => {
  159. return displayOnlineStatus.value && conversation.type === TUIChatEngine.TYPES.CONV_C2C;
  160. };
  161. const showConversationActionMenu = (
  162. event: Event,
  163. conversation: IConversationModel,
  164. index: number,
  165. isContextMenuEvent?: boolean
  166. ) => {
  167. if (isContextMenuEvent) {
  168. event.preventDefault();
  169. if (isUniFrameWork) {
  170. return;
  171. }
  172. }
  173. currentConversation.value = conversation;
  174. lastestOpenActionsMenuTime = Date.now();
  175. getActionsMenuPosition(event, index);
  176. };
  177. const closeConversationActionMenu = () => {
  178. // Prevent continuous triggering of overlay tap events
  179. if (lastestOpenActionsMenuTime && Date.now() - lastestOpenActionsMenuTime > 300) {
  180. currentConversation.value = undefined;
  181. isShowOverlay.value = false;
  182. }
  183. };
  184. const getActionsMenuPosition = (event: Event, index: number) => {
  185. if (isUniFrameWork) {
  186. if (typeof conversationListDomRef.value === "undefined") {
  187. emits("getPassingRef", conversationListDomRef);
  188. }
  189. const query = TUIGlobal?.createSelectorQuery().in(conversationListDomRef.value);
  190. query
  191. .select(`#convlistitem-${index}`)
  192. .boundingClientRect((data) => {
  193. if (data) {
  194. actionsMenuPosition.value = {
  195. // The uni-page-head of uni-h5 is not considered a member of the viewport, so the height of the head is manually increased.
  196. top: data.bottom + (isH5 ? 44 : 0),
  197. // @ts-expect-error in uniapp event has touches property
  198. left: event.touches[0].pageX,
  199. conversationHeight: data.height,
  200. };
  201. isShowOverlay.value = true;
  202. }
  203. })
  204. .exec();
  205. } else {
  206. const rect =
  207. ((event.currentTarget || event.target) as HTMLElement)?.getBoundingClientRect() ||
  208. {};
  209. if (rect) {
  210. actionsMenuPosition.value = {
  211. top: rect.bottom,
  212. left: isPC ? (event as MouseEvent).clientX : undefined,
  213. conversationHeight: rect.height,
  214. };
  215. }
  216. isShowOverlay.value = true;
  217. }
  218. };
  219. const enterConversationChat = (conversationID: string, a) => {
  220. emits("handleSwitchConversation", conversationID);
  221. TUIConversationService.switchConversation(conversationID);
  222. };
  223. function addLongPressHandler() {
  224. if (!conversationListInnerDomRef.value) {
  225. return;
  226. }
  227. addLongPressListener({
  228. element: conversationListInnerDomRef.value,
  229. onLongPress: (event, target) => {
  230. const index = (Array.from(
  231. conversationListInnerDomRef.value!.children
  232. ) as HTMLElement[]).indexOf(target!);
  233. showConversationActionMenu(event, conversationList.value[index], index);
  234. },
  235. options: {
  236. eventDelegation: {
  237. subSelector: ".tui-conversation-content",
  238. },
  239. },
  240. });
  241. }
  242. function onCurrentConversationUpdated(conversation: IConversationModel) {
  243. currentConversation.value = conversation;
  244. }
  245. function onConversationListUpdated(list: IConversationModel[]) {
  246. conversationList.value = list;
  247. console.log(conversationList.value);
  248. }
  249. function onCurrentConversationIDUpdated(id: string) {
  250. currentConversationID.value = id;
  251. }
  252. function onDisplayOnlineStatusUpdated(status: boolean) {
  253. displayOnlineStatus.value = status;
  254. }
  255. function onUserStatusListUpdated(list: Map<string, IUserStatus>) {
  256. if (list.size !== 0) {
  257. userOnlineStatusMap.value = [...list.entries()].reduce((obj, [key, value]) => {
  258. obj[key] = value;
  259. return obj;
  260. }, {} as IUserStatusMap);
  261. }
  262. }
  263. // Expose to the parent component and close actionsMenu when a sliding event is detected
  264. defineExpose({ closeChildren: closeConversationActionMenu });
  265. </script>
  266. <style lang="scss" scoped src="./style/index.scss"></style>
  267. <style lang="scss" scoped>
  268. .disable-select {
  269. -webkit-touch-callout: none;
  270. -webkit-user-select: none;
  271. -khtml-user-select: none;
  272. -moz-user-select: none;
  273. -ms-user-select: none;
  274. user-select: none;
  275. }
  276. </style>