index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view class="box">
  3. <view class="box-top">
  4. {{ info.name }}
  5. <text class="subText">
  6. {{ info.postName }}
  7. <text v-if="info.postName && info.enterpriseName" class="gun">|</text>
  8. {{ info.enterpriseName }}
  9. </text>
  10. </view>
  11. <view class="box-main" ref="chatRef">
  12. <view v-for="val in items" :key="val.id">
  13. <view class="box-main-time">{{ timesTampChange(+(val.timestamp.padEnd(13, '0'))) }}</view>
  14. <view :class="['message-view_item', val.from_uid === IM.uid ? 'is-self' : 'is-other']">
  15. <view class="image">
  16. <image
  17. class="header"
  18. :src="(val.from_uid === IM.uid ? mAvatar : getUserAvatar(info.avatar, info.sex)) || 'https://minio.citupro.com/dev/menduner/7.png'"
  19. ></image>
  20. </view>
  21. <view class="message-text" :class="{ active: val.from_uid === IM.uid}">
  22. {{ val.payload?.content }}
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="box-bottom">
  28. <textarea
  29. auto-height
  30. confirm-type="send"
  31. @confirm="handleSend"
  32. />
  33. </view>
  34. </view>
  35. </template>
  36. <script setup>
  37. import { ref, nextTick } from 'vue'
  38. import { useIMStore } from '@/store/im'
  39. import { userStore } from '@/store/user'
  40. import { timesTampChange } from '@/utils/date'
  41. import { getUserAvatar } from '@/utils/avatar'
  42. import { initConnect, send, initChart, getMoreMessages, checkConversation } from '@/hooks/useIM'
  43. import { onLoad } from '@dcloudio/uni-app'
  44. const useUserStore = userStore()
  45. const IM = useIMStore()
  46. const mAvatar = getUserAvatar(useUserStore.baseInfo?.avatar, useUserStore.baseInfo?.sex)
  47. const info = ref({})
  48. const chatRef = ref()
  49. const items = ref([])
  50. async function init(userId, enterpriseId) {
  51. const { channel, list, more } = await initChart(userId, enterpriseId)
  52. console.log(channel.value, list.value, more)
  53. items.value = list.value
  54. setTimeout(() => {
  55. scrollBottom()
  56. console.log('滚动')
  57. }, 1500)
  58. }
  59. function handleSend () {
  60. console.log('发送')
  61. }
  62. function scrollBottom () {
  63. const chat = chatRef.value
  64. if (chat) {
  65. nextTick(function () {
  66. console.log(chat.scrollHeight)
  67. chat.scrollTop = chat.scrollHeight
  68. uni.pageScrollTo({
  69. scrollTop: 0,
  70. duration: 300
  71. })
  72. })
  73. }
  74. }
  75. onLoad((options) => {
  76. info.value = options
  77. init(options.id, options.enterpriseId)
  78. })
  79. </script>
  80. <style lang="scss" scoped>
  81. .box {
  82. width: 100%;
  83. height: calc(100vh - 88rpx);
  84. display: flex;
  85. flex-direction: column;
  86. &-top {
  87. padding: 0 60rpx;
  88. box-sizing: border-box;
  89. width: 100%;
  90. height: 80rpx;
  91. line-height: 80rpx;
  92. // text-align: center;
  93. border-bottom: 2rpx solid #EEE;
  94. overflow: hidden;
  95. white-space: nowrap;
  96. text-overflow: ellipsis;
  97. .subText {
  98. font-size: .85em;
  99. color: #999;
  100. .gun {
  101. padding: 0 10rpx;
  102. }
  103. }
  104. }
  105. &-main {
  106. flex: 1;
  107. height: 0;
  108. padding: 40rpx;
  109. overflow-y: auto;
  110. &-time {
  111. user-select: none;
  112. position: relative;
  113. top: 16rpx;
  114. margin: 40rpx 0;
  115. text-align: center;
  116. max-height: 40rpx;
  117. text-align: center;
  118. font-weight: 400;
  119. font-size: .85em;
  120. color: #999;
  121. }
  122. .message-view_item {
  123. display: flex;
  124. flex-direction: row;
  125. align-items: flex-start;
  126. margin: 16rpx 0;
  127. position: relative;
  128. .image {
  129. width: 60rpx;
  130. height: 60rpx;
  131. border-radius: 180rpx;
  132. // flex-grow: 1;
  133. // flex-shrink: 0;
  134. overflow: hidden;
  135. .header {
  136. width: 60rpx;
  137. height: 60rpx;
  138. }
  139. }
  140. .message-text {
  141. overflow-wrap: break-word;
  142. background-color: #f0f2f5;
  143. border-radius: 12rpx;
  144. max-width: 80%;
  145. padding: 20rpx;
  146. &.active {
  147. background: #d5e6e8;
  148. }
  149. }
  150. }
  151. .is-self {
  152. flex-direction: row-reverse;
  153. display: flex;
  154. .message-text {
  155. margin-right: 20rpx;
  156. }
  157. }
  158. .is-other {
  159. .message-text {
  160. margin-left: 20rpx;
  161. }
  162. }
  163. }
  164. &-bottom {
  165. max-height: 300rpx;
  166. border-top: 2rpx solid #EEE;
  167. textarea {
  168. width: 100%;
  169. min-height: 40rpx;
  170. padding: 20rpx;
  171. background: rgba(211, 211, 211, 0.05);
  172. }
  173. }
  174. }
  175. </style>