index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <Overlay :useMask="false" @onOverlayClick="closeReadReceiptPanel">
  3. <div
  4. :class="{
  5. 'read-receipt-panel': true,
  6. 'read-receipt-panel-mobile': isMobile,
  7. 'read-receipt-panel-close-mobile': isMobile && isPanelClose,
  8. }"
  9. >
  10. <div class="header">
  11. <div class="header-text">
  12. {{ TUITranslateService.t("TUIChat.消息详情") }}
  13. </div>
  14. <div class="header-close-icon">
  15. <Icon
  16. size="12px"
  17. hotAreaSize="8"
  18. :file="closeIcon"
  19. @onClick="closeReadReceiptPanel"
  20. />
  21. </div>
  22. </div>
  23. <div class="read-status-counter-container">
  24. <div
  25. v-for="tabName in tabNameList"
  26. :key="tabName"
  27. :class="{
  28. 'read-status-counter': true,
  29. active: tabName === currentTabName,
  30. }"
  31. @click="toggleTabName(tabName)"
  32. >
  33. <div class="status-text">
  34. {{ tabInfo[tabName].tabName }}
  35. </div>
  36. <div class="status-count">
  37. {{
  38. tabInfo[tabName].count === undefined ? "" : tabInfo[tabName].count
  39. }}
  40. </div>
  41. </div>
  42. </div>
  43. <div class="read-status-member-list">
  44. <div
  45. v-if="tabInfo[currentTabName].count === 0 && isFirstLoadFinished"
  46. class="empty-list-tip"
  47. >
  48. - {{ TUITranslateService.t("TUIChat.空") }} -
  49. </div>
  50. <template v-else-if="isFirstLoadFinished">
  51. <template v-if="currentTabName === 'unread'">
  52. <div
  53. v-for="item in tabInfo[currentTabName].memberList"
  54. :key="item.userID"
  55. class="read-status-member-container"
  56. >
  57. <Avatar
  58. class="read-status-avatar"
  59. useSkeletonAnimation
  60. :url="item.avatar || ''"
  61. />
  62. <div class="username">
  63. {{ item.nick || item.userID }}
  64. </div>
  65. </div>
  66. </template>
  67. <template v-if="currentTabName === 'read'">
  68. <div
  69. v-for="item in tabInfo[currentTabName].memberList"
  70. :key="item.userID"
  71. class="read-status-member-container"
  72. >
  73. <Avatar
  74. class="read-status-avatar"
  75. useSkeletonAnimation
  76. :url="item.avatar"
  77. />
  78. <div class="username">
  79. {{ item.nick || item.userID }}
  80. </div>
  81. </div>
  82. </template>
  83. </template>
  84. <div v-if="isFirstLoadFinished" class="fetch-more-container">
  85. <FetchMore
  86. :isFetching="isPullDownFetching"
  87. :isTerminateObserve="isStopFetchMore"
  88. @onExposed="pullDownFetchMoreData"
  89. />
  90. </div>
  91. </div>
  92. </div>
  93. </Overlay>
  94. </template>
  95. <script setup lang="ts">
  96. import { ref, onMounted, watch, nextTick } from "../../../../adapter-vue";
  97. import {
  98. IMessageModel,
  99. TUIStore,
  100. TUIChatService,
  101. TUITranslateService,
  102. } from "@tencentcloud/chat-uikit-engine";
  103. import closeIcon from "../../../../assets/icon/icon-close.svg";
  104. import Icon from "../../../common/Icon.vue";
  105. import Overlay from "../../../common/Overlay/index.vue";
  106. import Avatar from "../../../common/Avatar/index.vue";
  107. import FetchMore from "../../../common/FetchMore/index.vue";
  108. import type {
  109. IGroupMessageReadMemberData,
  110. IMemberData,
  111. ITabInfo,
  112. TabName,
  113. } from "./interface";
  114. import { isMobile } from "../../../../utils/env";
  115. type ReadType = "unread" | "read" | "all";
  116. interface IProps {
  117. message: IMessageModel;
  118. }
  119. interface IEmits {
  120. (
  121. key: "setReadReceiptPanelVisible",
  122. visible: boolean,
  123. message?: IMessageModel
  124. ): void;
  125. }
  126. const emits = defineEmits<IEmits>();
  127. const props = withDefaults(defineProps<IProps>(), {
  128. message: () => ({} as IMessageModel),
  129. });
  130. let lastUnreadCursor: string = "";
  131. let lastReadCursor: string = "";
  132. const tabNameList: TabName[] = ["unread", "read"];
  133. const isListFetchCompleted: Record<TabName, boolean> = {
  134. unread: false,
  135. read: false,
  136. close: false,
  137. };
  138. const isPullDownFetching = ref<boolean>(false);
  139. const isPanelClose = ref<boolean>(false);
  140. const isFirstLoadFinished = ref<boolean>(false);
  141. const isStopFetchMore = ref<boolean>(false);
  142. const currentTabName = ref<TabName>("unread");
  143. const tabInfo = ref<ITabInfo>(generateInitalTabInfo());
  144. onMounted(async () => {
  145. await initAndRefetchReceiptInfomation();
  146. nextTick(() => {
  147. isFirstLoadFinished.value = true;
  148. });
  149. });
  150. watch(
  151. // uniapp下监听不到数据变化
  152. () => props.message.readReceiptInfo.readCount,
  153. () => {
  154. initAndRefetchReceiptInfomation();
  155. }
  156. );
  157. async function fetchGroupMessageRecriptMemberListByType(
  158. readType: ReadType = "all"
  159. ) {
  160. const message = TUIStore.getMessageModel(props.message.ID);
  161. let unreadResult = {} as IGroupMessageReadMemberData;
  162. let readResult = {} as IGroupMessageReadMemberData;
  163. if (readType === "all" || readType === "unread") {
  164. unreadResult = await TUIChatService.getGroupMessageReadMemberList({
  165. message,
  166. filter: 1,
  167. cursor: lastUnreadCursor,
  168. count: 100,
  169. });
  170. if (unreadResult) {
  171. lastUnreadCursor = unreadResult.data.cursor;
  172. if (unreadResult.data.isCompleted) {
  173. isListFetchCompleted.unread = true;
  174. }
  175. }
  176. }
  177. if (readType === "all" || readType === "read") {
  178. readResult = await TUIChatService.getGroupMessageReadMemberList({
  179. message,
  180. filter: 0,
  181. cursor: lastReadCursor,
  182. count: 100,
  183. });
  184. if (readResult) {
  185. lastReadCursor = readResult.data.cursor;
  186. if (readResult.data.isCompleted) {
  187. isListFetchCompleted.read = true;
  188. }
  189. }
  190. }
  191. // Fetch the total number of read and unread users
  192. const { unreadCount: totalUnreadCount, readCount: totalReadCount } =
  193. message.readReceiptInfo;
  194. return {
  195. unreadResult: {
  196. count: totalUnreadCount,
  197. ...unreadResult.data,
  198. },
  199. readResult: {
  200. count: totalReadCount,
  201. ...readResult.data,
  202. },
  203. };
  204. }
  205. async function pullDownFetchMoreData() {
  206. /**
  207. * 使用 isPullDownFetching 控制 FetchMore 组件的状态
  208. * 顺便同时做 uniapp 下的 intersectionObserver 的加锁
  209. * 因为 uniapp 下 没有 isIntersecting 无法判断被观察的元素进入还是退出观察区
  210. */
  211. if (isListFetchCompleted[currentTabName.value] || isPullDownFetching.value) {
  212. return;
  213. }
  214. isPullDownFetching.value = true;
  215. if (currentTabName.value === "unread" || currentTabName.value === "read") {
  216. const { unreadResult, readResult } =
  217. await fetchGroupMessageRecriptMemberListByType(currentTabName.value);
  218. checkStopFetchMore();
  219. try {
  220. tabInfo.value.unread.memberList = tabInfo.value.unread.memberList.concat(
  221. unreadResult.unreadUserInfoList || []
  222. );
  223. tabInfo.value.read.memberList = tabInfo.value.read.memberList.concat(
  224. readResult.readUserInfoList || []
  225. );
  226. } finally {
  227. isPullDownFetching.value = false;
  228. }
  229. }
  230. }
  231. /**
  232. * Initializes and refetches receipt information.
  233. *
  234. * @return {Promise<void>} A promise that resolves when the function has completed.
  235. */
  236. async function initAndRefetchReceiptInfomation(): Promise<void> {
  237. lastUnreadCursor = "";
  238. lastReadCursor = "";
  239. isStopFetchMore.value = false;
  240. isListFetchCompleted.unread = false;
  241. isListFetchCompleted.read = false;
  242. const { unreadResult, readResult } =
  243. await fetchGroupMessageRecriptMemberListByType("all");
  244. checkStopFetchMore();
  245. resetTabInfo("read", readResult.count, readResult.readUserInfoList);
  246. resetTabInfo("unread", unreadResult.count, unreadResult.unreadUserInfoList);
  247. resetTabInfo("close");
  248. }
  249. /**
  250. * Checks if the fetch more operation should be stopped
  251. * by IntersetctionObserver.disconnect().
  252. *
  253. * @return {void}
  254. */
  255. function checkStopFetchMore(): void {
  256. if (isListFetchCompleted.read && isListFetchCompleted.unread) {
  257. isStopFetchMore.value = true;
  258. }
  259. }
  260. /**
  261. * Resets the information of a specific tab.
  262. *
  263. * @param {TabName} tabName - The name of the tab to reset.
  264. * @param {number} [count] - The count to assign to the tab. Optional.
  265. * @param {IMemberData[]} [memberList] - The list of members to assign to the tab. Optional.
  266. * @return {void} - This function does not return anything.
  267. */
  268. function resetTabInfo(
  269. tabName: TabName,
  270. count?: number,
  271. memberList?: IMemberData[]
  272. ): void {
  273. tabInfo.value[tabName].count = count;
  274. tabInfo.value[tabName].memberList = memberList || [];
  275. }
  276. /**
  277. * Generates the initial tab information.
  278. *
  279. * @return {ITabInfo} The initial tab information.
  280. */
  281. function generateInitalTabInfo(): ITabInfo {
  282. return {
  283. read: {
  284. tabName: TUITranslateService.t("TUIChat.已读"),
  285. count: undefined,
  286. memberList: [],
  287. },
  288. unread: {
  289. tabName: TUITranslateService.t("TUIChat.未读"),
  290. count: undefined,
  291. memberList: [],
  292. },
  293. close: {
  294. tabName: TUITranslateService.t("TUIChat.关闭"),
  295. count: undefined,
  296. memberList: [],
  297. },
  298. };
  299. }
  300. /**
  301. * Toggles the tab name.
  302. *
  303. * @param {TabName} tabName - The name of the tab to toggle.
  304. * @return {void} This function does not return anything.
  305. */
  306. function toggleTabName(tabName: TabName): void {
  307. currentTabName.value = tabName;
  308. }
  309. function closeReadReceiptPanel(): void {
  310. isPanelClose.value = true;
  311. setTimeout(() => {
  312. emits("setReadReceiptPanelVisible", false);
  313. }, 200);
  314. }
  315. </script>
  316. <style scoped lang="scss">
  317. :not(not) {
  318. display: flex;
  319. flex-direction: column;
  320. box-sizing: border-box;
  321. min-width: 0;
  322. }
  323. .read-receipt-panel {
  324. background-color: #fff;
  325. box-shadow: 0 7px 20px rgba(0, 0, 0, 0.1);
  326. width: 368px;
  327. height: 510px;
  328. padding: 30px 20px;
  329. display: flex;
  330. flex-direction: column;
  331. border-radius: 8px;
  332. overflow: hidden;
  333. .header {
  334. flex-direction: row;
  335. justify-content: center;
  336. align-items: center;
  337. position: relative;
  338. .header-text {
  339. font-weight: bold;
  340. font-size: 16px;
  341. line-height: 30px;
  342. color: #333;
  343. }
  344. .header-close-icon {
  345. position: absolute;
  346. right: 0;
  347. margin-right: 10px;
  348. }
  349. }
  350. .read-status-counter-container {
  351. flex-direction: row;
  352. justify-content: space-between;
  353. align-items: flex-start;
  354. min-height: 59px;
  355. margin: 20px 40px 17.5px;
  356. .read-status-counter {
  357. justify-content: flex-start;
  358. align-items: center;
  359. cursor: pointer;
  360. -webkit-tap-highlight-color: transparent;
  361. .status-text {
  362. font-size: 14px;
  363. line-height: 20px;
  364. }
  365. .status-count {
  366. margin-top: 2px;
  367. font-size: 30px;
  368. font-weight: bolder;
  369. line-height: 37px;
  370. }
  371. &.active {
  372. color: #405eff;
  373. }
  374. }
  375. }
  376. .read-status-member-list {
  377. flex: 1 1 auto;
  378. overflow: hidden auto;
  379. padding: 20px 0 0;
  380. border-top: 0.5px solid #e8e8e9;
  381. font-size: 14px;
  382. .empty-list-tip {
  383. align-self: center;
  384. color: #b3b3b3;
  385. }
  386. .read-status-member-container {
  387. flex-direction: row;
  388. align-items: center;
  389. .read-status-avatar {
  390. flex: 0 0 auto;
  391. }
  392. .username {
  393. margin-left: 8px;
  394. line-height: 20px;
  395. flex: 0 1 auto;
  396. display: block;
  397. overflow: hidden;
  398. text-overflow: ellipsis;
  399. word-break: break-all;
  400. white-space: nowrap;
  401. }
  402. & + .read-status-member-container {
  403. margin-top: 20px;
  404. }
  405. }
  406. .fetch-more-container {
  407. justify-content: center;
  408. align-items: center;
  409. margin-top: auto;
  410. }
  411. }
  412. }
  413. .read-receipt-panel-mobile {
  414. @extend .read-receipt-panel;
  415. box-shadow: none;
  416. width: 100vw;
  417. height: 100vh;
  418. border-radius: 0;
  419. animation: slide-in-from-right 0.3s ease-out;
  420. transition: transform 0.2s ease-out;
  421. @keyframes slide-in-from-right {
  422. from {
  423. transform: translateX(100%);
  424. }
  425. }
  426. }
  427. .read-receipt-panel-close-mobile {
  428. transform: translateX(100%);
  429. }
  430. </style>