index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div style="height: 100%">
  3. <div class="sectionSearch">
  4. <n-input
  5. class="TheSearch"
  6. style="--n-font-size: 12px; --n-height: 32px; --n-caret-color: #198cfe; --n-border-hover: 1px solid #198cfe; --n-border-focus: 1px solid #198cfe; --n-loading-color: #198cfe; --n-box-shadow-focus: 0 0 0 2px rgba(25 140 254, 0.2)"
  7. round
  8. clearable
  9. placeholder="请输入名称"
  10. v-model:value="keyword"
  11. @clear="
  12. () => {
  13. debouncedRequest('');
  14. }
  15. "
  16. @keyup="onKeyup"
  17. >
  18. <template #prefix> <span class="icon-search-input"></span></template>
  19. </n-input>
  20. </div>
  21. <div class="TUI-conversation">
  22. <div class="network" v-if="isNetwork">
  23. <i class="icon icon-error">!</i>
  24. <p>️{{ $t("TUIConversation.网络异常,请您检查网络设置") }}</p>
  25. </div>
  26. <main class="TUI-conversation-list">
  27. <TUIConversationList :currentID="currentConversationID" :data="conversationData" @handleItem="handleCurrentConversation" :isH5="env.isH5" :displayOnlineStatus="displayOnlineStatus" :userStatusList="userStatusList" />
  28. </main>
  29. <!-- <the-empty v-if="!isNetwork && !loading && conversationData.list.length <= 0" style="height: 90%" /> -->
  30. <div class="theEmtpy" v-if="!loading && conversationData.list.length <= 0" style="height: 90%">
  31. <img class="emptyImg" src="../../assets/nomore.png" />
  32. <p>暂无数据</p>
  33. </div>
  34. </div>
  35. </div>
  36. </template>
  37. <script lang="ts">
  38. import { defineComponent, reactive, toRefs, computed, watch, onMounted, onBeforeMount } from "vue";
  39. import TUIConversationList from "./components/list";
  40. import { caculateTimeago } from "../utils";
  41. import { useThrottleFn } from "@vueuse/core";
  42. import { handleAvatar, handleName, handleShowLastMessage, handleAt } from "../TUIChat/utils/utils";
  43. const TUIConversation = defineComponent({
  44. name: "TUIConversation",
  45. components: {
  46. TUIConversationList,
  47. // TheSearch,
  48. // TheEmpty,
  49. },
  50. props: {
  51. displayOnlineStatus: {
  52. type: Boolean,
  53. default: false,
  54. },
  55. },
  56. setup(props: any, ctx: any) {
  57. const TUIServer: any = TUIConversation?.TUIServer;
  58. const data = reactive({
  59. currentConversationID: "",
  60. keyword: "",
  61. loading: false,
  62. conversationData: {
  63. list: [] as any,
  64. handleItemAvator: (item: any) => handleAvatar(item),
  65. handleItemName: (item: any) => handleName(item),
  66. handleShowAt: (item: any) => handleAt(item),
  67. handleShowMessage: (item: any) => handleShowLastMessage(item),
  68. handleItemTime: (time: number) => {
  69. if (time > 0) {
  70. return caculateTimeago(time * 1000);
  71. }
  72. return "";
  73. },
  74. },
  75. userIDList: [],
  76. netWork: "",
  77. env: TUIServer.TUICore.TUIEnv,
  78. displayOnlineStatus: false,
  79. userStatusList: new Map(),
  80. });
  81. try {
  82. data.loading = true;
  83. TUIServer.bind(data, () => {
  84. setTimeout(() => {
  85. data.loading = false;
  86. }, 500);
  87. });
  88. } catch {
  89. data.loading = false;
  90. }
  91. TUIConversationList.TUIServer = TUIServer;
  92. const onUpdateCount = (val: any) => {
  93. if (window.parent) {
  94. window.parent?.postMessage({
  95. api: "getNoReadMessageCount",
  96. count: val,
  97. });
  98. }
  99. };
  100. onMounted(() => {
  101. window.addEventListener("message", (params?: any) => {
  102. if (!params.data?.imUserId) return;
  103. TUIServer.getConversationProfile(`C2C${params?.data.imUserId}`)
  104. .then((imResponse: any) => {
  105. // 通知 TUIConversation 添加当前会话
  106. TUIServer.handleCurrentConversation(imResponse.data.conversation);
  107. })
  108. .catch((error: any) => {
  109. console.log(error, "error");
  110. });
  111. });
  112. TUIServer.TUICore.tim.on(TUIServer.TUICore.TIM.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, onUpdateCount);
  113. });
  114. onBeforeMount(() => {
  115. TUIServer.TUICore.tim.off(TUIServer.TUICore.TIM.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, onUpdateCount);
  116. });
  117. watch(
  118. () => data.currentConversationID,
  119. (newVal: any) => {
  120. ctx.emit("current", newVal);
  121. },
  122. {
  123. deep: true,
  124. }
  125. );
  126. const isNetwork = computed(() => {
  127. const disconnected = data.netWork === TUIServer.TUICore.TIM.TYPES.NET_STATE_DISCONNECTED;
  128. const connecting = data.netWork === TUIServer.TUICore.TIM.TYPES.NET_STATE_CONNECTING;
  129. return disconnected || connecting;
  130. });
  131. const handleCurrentConversation = (value: any) => {
  132. TUIServer.handleCurrentConversation(value);
  133. };
  134. //
  135. const noSearch = async (val: string) => {
  136. data.loading = true;
  137. try {
  138. data.conversationData.list = [];
  139. await TUIServer.getConversationListForName(val);
  140. } catch {
  141. //
  142. }
  143. data.loading = false;
  144. };
  145. const debouncedRequest = useThrottleFn((val: string) => {
  146. if (props.loading) return;
  147. noSearch(val);
  148. }, 500);
  149. const onKeyup = (e: any) => {
  150. e.stopPropagation();
  151. if (e.code === "Enter") {
  152. debouncedRequest(data.keyword);
  153. }
  154. };
  155. return {
  156. ...toRefs(data),
  157. handleCurrentConversation,
  158. isNetwork,
  159. noSearch,
  160. onKeyup,
  161. debouncedRequest,
  162. };
  163. },
  164. });
  165. export default TUIConversation;
  166. </script>
  167. <style scoped lang="scss" src="./style/index.scss"></style>