ChatingList.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <scroll-view
  3. :scroll-with-animation="withAnimation"
  4. @click="click"
  5. id="scroll_view"
  6. :style="{
  7. height: '1px',
  8. backgroundImage: `url(${bgUrl})`,
  9. backgroundSize: `100% 100%`
  10. }"
  11. @scroll="throttleScroll"
  12. :scroll-top="scrollTop"
  13. scroll-y
  14. :scroll-into-view="scrollIntoView"
  15. upper-threshold="250"
  16. @scrolltoupper="scrolltoupper">
  17. <view id="scroll_wrap">
  18. <u-loadmore nomoreText="" v-show="showLoading" :status="loadMoreStatus" />
  19. <view v-for="(item, index) in storeHistoryMessageList" :key="item.clientMsgID">
  20. <view v-if="getTimeLine" class="time_gap_line">
  21. {{ getTimeLine(item, storeHistoryMessageList[index - 1]) }}
  22. </view>
  23. <message-item-render
  24. :mutipleCheckVisible="mutipleCheckVisible"
  25. :menuOutsideFlag="menuOutsideFlag"
  26. @messageItemRender="messageItemRender"
  27. :source="item"
  28. :isSender="item.sendID === storeCurrentUserID"
  29. @closeMenu="closeMenu"/>
  30. <view v-if="sendFailedDesc" class="time_gap_line send_failed_tip">
  31. {{ sendFailedDesc(item) }}
  32. </view>
  33. </view>
  34. <view style="visibility: hidden; height: 12px" id="auchormessage_bottom_item"></view>
  35. </view>
  36. <!-- <transition name="fade">
  37. <view
  38. @click="scrollToBottom(false)"
  39. v-show="getNewMesageCount && !needScoll"
  40. class="new_message_flag fade"
  41. >
  42. <image
  43. style="height: 10px; width: 11px"
  44. src="@/static/images/common_db_arrow.png"
  45. />
  46. <text>{{ `${getNewMesageCount}条新消息` }}</text>
  47. </view>
  48. </transition> -->
  49. </scroll-view>
  50. </template>
  51. <script>
  52. import { mapGetters, mapActions } from 'vuex';
  53. import dayjs from 'dayjs';
  54. import { SendMessageFailedType } from '../../../../constant';
  55. import MessageItemRender from './MessageItem/index.vue';
  56. import { MessageStatus } from 'openim-uniapp-polyfill';
  57. export default {
  58. name: '',
  59. components: {
  60. MessageItemRender
  61. },
  62. props: {
  63. menuOutsideFlag: Number,
  64. mutipleCheckVisible: Boolean
  65. },
  66. data() {
  67. return {
  68. scrollIntoView: '',
  69. scrollWithAnimation: false,
  70. scrollTop: 0,
  71. old: {
  72. scrollTop: 0
  73. },
  74. initFlag: true,
  75. isOverflow: false,
  76. needScoll: true,
  77. withAnimation: false,
  78. messageLoadState: {
  79. loading: false
  80. },
  81. bgUrl: '',
  82. bgHeight: '',
  83. showLoading:false
  84. };
  85. },
  86. computed: {
  87. ...mapGetters(['storeCurrentConversation', 'storeHistoryMessageList', 'storeHasMoreMessage', 'storeCurrentUserID', 'storeSelfInfo']),
  88. loadMoreStatus() {
  89. if (!this.storeHasMoreMessage) {
  90. return 'nomore';
  91. }
  92. return this.messageLoadState.loading ? 'loading' : 'loadmore';
  93. },
  94. getNewMesageCount() {
  95. return this.storeHistoryMessageList.filter((message) => message.isAppend).length;
  96. },
  97. sendFailedDesc() {
  98. return (message) => {
  99. if (message.status === MessageStatus.Failed && message.errCode === SendMessageFailedType.Blacked) {
  100. return '消息已发出,但被对方拒收了';
  101. }
  102. if (message.status === MessageStatus.Failed && message.errCode === SendMessageFailedType.NotFriend) {
  103. return '对方开启了好友验证,你还不是他(她)好友。请先发送好友验证,对方验证通过后,才能聊天。';
  104. }
  105. return '';
  106. };
  107. },
  108. getTimeLine() {
  109. return (message, preMessage) => {
  110. const sendTime = message.sendTime;
  111. const preSendTime = preMessage?.sendTime;
  112. // 只在时间间隔大于10分钟时才显示时间线
  113. if (preSendTime && sendTime - preSendTime > 600000) {
  114. const now = dayjs();
  115. const messageDate = dayjs(sendTime);
  116. // 检查是否在今天
  117. if (messageDate.isSame(now, 'day')) {
  118. // 检查是否在几秒内
  119. if (now.diff(messageDate, 'second') < 60) {
  120. return '刚刚';
  121. }
  122. // 今天且大于一分钟,显示小时:分钟
  123. return messageDate.format('HH:mm');
  124. }
  125. // 检查是否是昨天
  126. if (messageDate.isSame(now.subtract(1, 'day'), 'day')) {
  127. return '昨天 ' + messageDate.format('HH:mm');
  128. }
  129. // 检查是否在今年
  130. if (messageDate.isSame(now, 'year')) {
  131. // 今年但不是今天或昨天,显示月日 小时:分钟
  132. return messageDate.format('MM-DD HH:mm');
  133. }
  134. // 今年以前,显示年月日 小时:分钟
  135. return messageDate.format('YYYY-MM-DD HH:mm');
  136. }
  137. return null;
  138. };
  139. }
  140. },
  141. beforeMount() {
  142. this.updateBgUrl();
  143. },
  144. mounted() {
  145. this.loadMessageList();
  146. },
  147. methods: {
  148. ...mapActions('message', ['getHistoryMesageList']),
  149. messageItemRender(clientMsgID) {
  150. if (this.initFlag && clientMsgID === this.storeHistoryMessageList[this.storeHistoryMessageList.length - 1].clientMsgID) {
  151. this.initFlag = false;
  152. setTimeout(() => this.scrollToBottom(true), 0);
  153. // setTimeout(() => this.scrollToAnchor(`auchor${clientMsgID}`, false, true), 200)
  154. // this.checkInitHeight();
  155. // this.scrollToAnchor('message_bottom_item',true)
  156. }
  157. },
  158. async loadMessageList(isLoadMore = false) {
  159. this.showLoading=isLoadMore;
  160. this.messageLoadState.loading = true;
  161. const lastMsgID = this.storeHistoryMessageList[0]?.clientMsgID;
  162. const options = {
  163. conversationID: this.storeCurrentConversation.conversationID,
  164. count: 20,
  165. startClientMsgID: this.storeHistoryMessageList[0]?.clientMsgID ?? '',
  166. viewType: 0
  167. };
  168. try {
  169. const { emptyFlag } = await this.getHistoryMesageList(options);
  170. if (emptyFlag) {
  171. this.$emit('initSuccess');
  172. }
  173. } catch (e) {
  174. console.log(e);
  175. //TODO handle the exception
  176. }
  177. this.$nextTick(function () {
  178. if (isLoadMore && lastMsgID) {
  179. this.scrollToAnchor(`auchor${lastMsgID}`);
  180. }
  181. this.messageLoadState.loading = false;
  182. });
  183. },
  184. click(e) {
  185. this.$emit('click', e);
  186. },
  187. onScroll(event) {
  188. const { scrollHeight, scrollTop } = event.target;
  189. this.old.scrollTop = scrollTop;
  190. this.needScoll = scrollHeight - scrollTop < uni.getWindowInfo().windowHeight * 1.2;
  191. this.$emit('closeMenu', event);
  192. },
  193. throttleScroll(event) {
  194. uni.$u.throttle(() => this.onScroll(event), 150);
  195. },
  196. scrolltoupper() {
  197. if (!this.messageLoadState.loading && this.storeHasMoreMessage) {
  198. this.loadMessageList(true);
  199. }
  200. },
  201. scrollToBottom(isInit = false, isRecv = false) {
  202. console.log("qxj scrollToBottom");
  203. if (isRecv && !this.needScoll) {
  204. return;
  205. }
  206. if (!isInit) {
  207. this.withAnimation = true;
  208. setTimeout(() => (this.withAnimation = false), 100);
  209. }
  210. this.$nextTick(() => {
  211. uni.createSelectorQuery()
  212. .in(this)
  213. .select('#scroll_wrap')
  214. .boundingClientRect((res) => {
  215. // let top = res.height - this.scrollViewHeight;
  216. // if (top > 0) {
  217. this.scrollTop = this.old.scrollTop;
  218. this.$nextTick(() => (this.scrollTop = res.height));
  219. if (isInit) {
  220. this.$emit('initSuccess');
  221. }
  222. // }
  223. })
  224. .exec();
  225. });
  226. },
  227. scrollToAnchor(auchor) {
  228. this.$nextTick(function () {
  229. this.scrollIntoView = auchor;
  230. });
  231. },
  232. checkInitHeight() {
  233. this.getEl('#scroll_view').then(({ height }) => {
  234. this.bgHeight = `${height}px`;
  235. });
  236. },
  237. getEl(el) {
  238. return new Promise((resolve) => {
  239. const query = uni.createSelectorQuery().in(this);
  240. query.select(el)
  241. .boundingClientRect((data) => {
  242. resolve(data);
  243. }).exec();
  244. });
  245. },
  246. closeMenu() {
  247. this.$emit('closeMenu');
  248. },
  249. updateBgUrl() {
  250. const bgMap = uni.getStorageSync('IMBgMap') || {};
  251. this.bgUrl = bgMap[this.$store.getters.storeCurrentConversation.conversationID] || '';
  252. }
  253. }
  254. };
  255. </script>
  256. <style lang="scss" scoped>
  257. #scroll_view {
  258. flex: 1;
  259. background-repeat: no-repeat;
  260. position: relative;
  261. }
  262. .watermark-view {
  263. width: 100%;
  264. height: 100%;
  265. position: fixed;
  266. }
  267. .watermark {
  268. font-size: 16px; /* 水印文字大小 */
  269. color: #f0f2f6; /* 水印文字颜色,使用透明度控制可见度 */
  270. position: absolute; /* 水印相对定位 */
  271. transform: rotate(-45deg);
  272. pointer-events: none; /* 防止水印文字干扰交互 */
  273. }
  274. .uni-scroll-view {
  275. position: relative;
  276. }
  277. .new_message_flag {
  278. position: sticky;
  279. background: #ffffff;
  280. box-shadow: 0px 3px 8px 0px rgba(0, 0, 0, 0.1);
  281. border-radius: 14px;
  282. padding: 4px 8px;
  283. display: flex;
  284. justify-content: center;
  285. align-items: center;
  286. bottom: 12px;
  287. left: 50%;
  288. transform: translateX(-50%);
  289. width: fit-content;
  290. font-size: 24rpx;
  291. color: #006aff;
  292. }
  293. .time_gap_line {
  294. position: relative;
  295. padding: 0 10vw 12rpx;
  296. text-align: center;
  297. font-size: 16px;
  298. color: #A5A5A5;
  299. }
  300. .fade-leave,
  301. .fade-enter-to {
  302. opacity: 1;
  303. }
  304. .fade-leave-active,
  305. .fade-enter-active {
  306. transition: all 0.3s;
  307. }
  308. .fade-leave-to,
  309. .fade-enter {
  310. opacity: 0;
  311. }
  312. </style>