Im.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <script setup lang="ts">
  2. import dayjs from 'dayjs'
  3. import { computed, ref, watch } from 'vue'
  4. import type { Conversation } from '@tencentcloud/lite-chat'
  5. import { useImPopup } from '~/composables/useImPopup'
  6. import TitleIcon from '~/assets/icons/tab-active.svg'
  7. import CloseListIcon from '~/assets/icons/im/close-list.svg'
  8. const { state, close } = useImPopup()
  9. const { ready, conversationList, conversationLoading, conversationError, getConversationList, bizUserInfoMap } = useChat()
  10. type ViewMode = 'list' | 'detail'
  11. const view = ref<ViewMode>('list')
  12. const activeConversationId = ref<string | null>(null)
  13. const list = computed(() => conversationList.value
  14. ? conversationList.value.filter((conv) => {
  15. return conv.userProfile?.userID !== '10000'
  16. })
  17. : [])
  18. const activeConversation = computed(() => {
  19. const id = activeConversationId.value
  20. if (!id) return null
  21. return list.value.find(c => c.conversationID === id) ?? null
  22. })
  23. const getConversationName = (conv: Conversation) => {
  24. const userNo = conv?.userProfile?.userID
  25. const biz = userNo ? bizUserInfoMap.value?.[userNo] : undefined
  26. return biz?.nickname || conv?.remark || conv?.userProfile?.nick || conv?.groupProfile?.name || conv?.conversationID || ''
  27. }
  28. const getConversationAvatar = (conv: Conversation) => {
  29. const userNo = conv?.userProfile?.userID
  30. const biz = userNo ? bizUserInfoMap.value?.[userNo] : undefined
  31. return biz?.avatar || conv?.userProfile?.avatar || conv?.groupProfile?.avatar || '/avatar.png'
  32. }
  33. const getConversationLastText = (conv: Conversation) => {
  34. const msg = conv?.lastMessage
  35. if (!msg) return ''
  36. const text = msg?.payload?.text
  37. if (typeof text === 'string' && text.trim()) return text
  38. const type = String(msg?.type || '').toLowerCase()
  39. if (type.includes('image')) return '[Image]'
  40. if (type.includes('video')) return '[Video]'
  41. if (type.includes('file')) return '[File]'
  42. if (type.includes('audio')) return '[Audio]'
  43. return '[Message]'
  44. }
  45. const getConversationTimeLabel = (conv: Conversation) => {
  46. const sec = Number(conv?.lastMessage?.lastTime)
  47. if (!Number.isFinite(sec) || sec <= 0) return ''
  48. const t = dayjs.unix(sec)
  49. if (dayjs().diff(t, 'second') < 60) return '现在'
  50. if (dayjs().isSame(t, 'day')) return t.format('HH:mm')
  51. return t.format('MM/DD')
  52. }
  53. const fetchConversations = async () => {
  54. if (!state.visible) return
  55. if (!ready.value) return
  56. await getConversationList()
  57. }
  58. const activeName = computed(() => activeConversation.value ? getConversationName(activeConversation.value) : '')
  59. const activeAvatar = computed(() => activeConversation.value ? getConversationAvatar(activeConversation.value) : '/avatar.png')
  60. const activeBizUserInfo = computed(() => {
  61. const userNo = activeConversation.value?.userProfile?.userID
  62. if (!userNo) return undefined
  63. return bizUserInfoMap.value?.[userNo]
  64. })
  65. const openConversationDetail = (conv: Conversation) => {
  66. activeConversationId.value = conv.conversationID
  67. view.value = 'detail'
  68. }
  69. const backToList = () => {
  70. view.value = 'list'
  71. activeConversationId.value = null
  72. }
  73. watch(
  74. () => [state.visible, ready.value] as const,
  75. ([visible, isReady]) => {
  76. if (visible && isReady) fetchConversations()
  77. },
  78. { immediate: true },
  79. )
  80. const handleUpdateShow = (value: boolean) => {
  81. if (!value) close()
  82. }
  83. watch(
  84. () => state.visible,
  85. (visible) => {
  86. if (!visible) {
  87. view.value = 'list'
  88. activeConversationId.value = null
  89. }
  90. },
  91. )
  92. </script>
  93. <template>
  94. <van-popup
  95. :show="state.visible"
  96. position="bottom"
  97. teleport="body"
  98. @update:show="handleUpdateShow"
  99. >
  100. <ImConversationDetail
  101. v-if="view === 'detail'"
  102. :conversation="activeConversation"
  103. :name="activeName"
  104. :avatar="activeAvatar"
  105. :biz-user-info="activeBizUserInfo"
  106. @back="backToList"
  107. @close="close"
  108. />
  109. <div
  110. v-else
  111. class="im-popup"
  112. >
  113. <div class="im-popup__bg" />
  114. <div class="im-popup__header">
  115. <div class="im-popup__title">
  116. <TitleIcon class="im-popup__title-icon" />
  117. <span class="im-popup__title-text">
  118. Message
  119. </span>
  120. </div>
  121. <button
  122. class="im-popup__close"
  123. type="button"
  124. @click="close"
  125. >
  126. <CloseListIcon />
  127. </button>
  128. </div>
  129. <div class="im-popup__content">
  130. <div
  131. v-if="!ready"
  132. class="im-popup__placeholder"
  133. >
  134. IM is not ready.
  135. </div>
  136. <div
  137. v-else-if="conversationLoading"
  138. class="im-popup__placeholder"
  139. >
  140. Loading...
  141. </div>
  142. <div
  143. v-else-if="conversationError"
  144. class="im-popup__placeholder"
  145. >
  146. {{ conversationError }}
  147. </div>
  148. <div
  149. v-else-if="list.length === 0"
  150. class="im-popup__placeholder"
  151. >
  152. <img
  153. src="~/assets/images/common/empty.png"
  154. alt="empty"
  155. >
  156. No conversations.
  157. </div>
  158. <div
  159. v-else
  160. class="im-popup__list"
  161. >
  162. <ImChatItem
  163. v-for="conv in list"
  164. :key="conv.conversationID"
  165. :avatar="getConversationAvatar(conv)"
  166. :name="getConversationName(conv)"
  167. :time="getConversationTimeLabel(conv)"
  168. :message="getConversationLastText(conv)"
  169. :unread="conv.unreadCount"
  170. @click="openConversationDetail(conv)"
  171. />
  172. </div>
  173. </div>
  174. </div>
  175. </van-popup>
  176. </template>
  177. <style scoped lang="scss">
  178. .im-popup {
  179. height: 100vh;
  180. padding: 16px 16px 20px;
  181. position: relative;
  182. display: flex;
  183. flex-direction: column;
  184. &__bg {
  185. @include size(100%, 173px);
  186. @include bg('~/assets/images/home/header-bg.png', 100% 173px);
  187. position: absolute;
  188. inset: 0;
  189. z-index: 0;
  190. }
  191. &__header {
  192. display: flex;
  193. align-items: center;
  194. justify-content: space-between;
  195. position: relative;
  196. z-index: 1;
  197. }
  198. &__title {
  199. font-size: 24px;
  200. font-family: var(--font-title);
  201. font-weight: 600;
  202. color: #000;
  203. display: flex;
  204. align-items: center;
  205. justify-content: center;
  206. min-width: 0;
  207. &-icon {
  208. position: absolute;
  209. z-index: 0;
  210. }
  211. &-text {
  212. position: relative;
  213. z-index: 1;
  214. }
  215. }
  216. &__close {
  217. border: none;
  218. background: transparent;
  219. }
  220. &__content {
  221. display: flex;
  222. flex-direction: column;
  223. gap: 10px;
  224. position: relative;
  225. z-index: 1;
  226. flex: 1 1 auto;
  227. min-height: 0;
  228. overflow: auto;
  229. }
  230. &__list {
  231. margin-top: 10px;
  232. display: flex;
  233. flex-direction: column;
  234. gap: 14px;
  235. }
  236. &__placeholder {
  237. margin-top: 40px;
  238. display: flex;
  239. flex-direction: column;
  240. align-items: center;
  241. justify-content: center;
  242. gap: 12px;
  243. font-size: 12px;
  244. color: rgba(0, 0, 0, 0.70);
  245. img {
  246. @include size(100px);
  247. }
  248. }
  249. }
  250. </style>