ChatingList1.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. if (preSendTime && sendTime - preSendTime > 600000) {
  113. const now = dayjs();
  114. const messageDate = dayjs(sendTime);
  115. // 检查是否在几秒内
  116. if (now.diff(messageDate, 'second') < 60) {
  117. return '刚刚';
  118. }
  119. // 根据年份决定显示格式
  120. return messageDate.isSame(now, 'year')
  121. ? messageDate.format('MM-DD') // 本年度
  122. : messageDate.format('YYYY-MM-DD'); // 非本年度
  123. }
  124. return null;
  125. };
  126. }
  127. },
  128. beforeMount() {
  129. this.updateBgUrl();
  130. },
  131. mounted() {
  132. this.loadMessageList();
  133. },
  134. methods: {
  135. ...mapActions('message', ['getHistoryMesageList']),
  136. messageItemRender(clientMsgID) {
  137. if (this.initFlag && clientMsgID === this.storeHistoryMessageList[this.storeHistoryMessageList.length - 1].clientMsgID) {
  138. this.initFlag = false;
  139. setTimeout(() => this.scrollToBottom(true), 0);
  140. // setTimeout(() => this.scrollToAnchor(`auchor${clientMsgID}`, false, true), 200)
  141. // this.checkInitHeight();
  142. // this.scrollToAnchor('message_bottom_item',true)
  143. }
  144. },
  145. async loadMessageList(isLoadMore = false) {
  146. this.showLoading=isLoadMore;
  147. this.messageLoadState.loading = true;
  148. const lastMsgID = this.storeHistoryMessageList[0]?.clientMsgID;
  149. const options = {
  150. conversationID: this.storeCurrentConversation.conversationID,
  151. count: 20,
  152. startClientMsgID: this.storeHistoryMessageList[0]?.clientMsgID ?? '',
  153. viewType: 0
  154. };
  155. try {
  156. const { emptyFlag } = await this.getHistoryMesageList(options);
  157. if (emptyFlag) {
  158. this.$emit('initSuccess');
  159. }
  160. } catch (e) {
  161. console.log(e);
  162. //TODO handle the exception
  163. }
  164. this.$nextTick(function () {
  165. if (isLoadMore && lastMsgID) {
  166. this.scrollToAnchor(`auchor${lastMsgID}`);
  167. }
  168. this.messageLoadState.loading = false;
  169. });
  170. },
  171. click(e) {
  172. this.$emit('click', e);
  173. },
  174. onScroll(event) {
  175. const { scrollHeight, scrollTop } = event.target;
  176. this.old.scrollTop = scrollTop;
  177. this.needScoll = scrollHeight - scrollTop < uni.getWindowInfo().windowHeight * 1.2;
  178. this.$emit('closeMenu', event);
  179. },
  180. throttleScroll(event) {
  181. uni.$u.throttle(() => this.onScroll(event), 150);
  182. },
  183. scrolltoupper() {
  184. if (!this.messageLoadState.loading && this.storeHasMoreMessage) {
  185. this.loadMessageList(true);
  186. }
  187. },
  188. scrollToBottom(isInit = false, isRecv = false) {
  189. console.log("qxj scrollToBottom");
  190. if (isRecv && !this.needScoll) {
  191. return;
  192. }
  193. if (!isInit) {
  194. this.withAnimation = true;
  195. setTimeout(() => (this.withAnimation = false), 100);
  196. }
  197. this.$nextTick(() => {
  198. uni.createSelectorQuery()
  199. .in(this)
  200. .select('#scroll_wrap')
  201. .boundingClientRect((res) => {
  202. // let top = res.height - this.scrollViewHeight;
  203. // if (top > 0) {
  204. this.scrollTop = this.old.scrollTop;
  205. this.$nextTick(() => (this.scrollTop = res.height));
  206. if (isInit) {
  207. this.$emit('initSuccess');
  208. }
  209. // }
  210. })
  211. .exec();
  212. });
  213. },
  214. scrollToAnchor(auchor) {
  215. this.$nextTick(function () {
  216. this.scrollIntoView = auchor;
  217. });
  218. },
  219. checkInitHeight() {
  220. this.getEl('#scroll_view').then(({ height }) => {
  221. this.bgHeight = `${height}px`;
  222. });
  223. },
  224. getEl(el) {
  225. return new Promise((resolve) => {
  226. const query = uni.createSelectorQuery().in(this);
  227. query.select(el)
  228. .boundingClientRect((data) => {
  229. resolve(data);
  230. }).exec();
  231. });
  232. },
  233. closeMenu() {
  234. this.$emit('closeMenu');
  235. },
  236. updateBgUrl() {
  237. const bgMap = uni.getStorageSync('IMBgMap') || {};
  238. this.bgUrl = bgMap[this.$store.getters.storeCurrentConversation.conversationID] || '';
  239. }
  240. }
  241. };
  242. </script>
  243. <style lang="scss" scoped>
  244. #scroll_view {
  245. flex: 1;
  246. background-repeat: no-repeat;
  247. position: relative;
  248. }
  249. .watermark-view {
  250. width: 100%;
  251. height: 100%;
  252. position: fixed;
  253. }
  254. .watermark {
  255. font-size: 16px; /* 水印文字大小 */
  256. color: #f0f2f6; /* 水印文字颜色,使用透明度控制可见度 */
  257. position: absolute; /* 水印相对定位 */
  258. transform: rotate(-45deg);
  259. pointer-events: none; /* 防止水印文字干扰交互 */
  260. }
  261. .uni-scroll-view {
  262. position: relative;
  263. }
  264. .new_message_flag {
  265. position: sticky;
  266. background: #ffffff;
  267. box-shadow: 0px 3px 8px 0px rgba(0, 0, 0, 0.1);
  268. border-radius: 14px;
  269. padding: 4px 8px;
  270. display: flex;
  271. justify-content: center;
  272. align-items: center;
  273. bottom: 12px;
  274. left: 50%;
  275. transform: translateX(-50%);
  276. width: fit-content;
  277. font-size: 24rpx;
  278. color: #006aff;
  279. }
  280. .time_gap_line {
  281. position: relative;
  282. padding: 0 10vw 12rpx;
  283. text-align: center;
  284. font-size: 16px;
  285. color: #A5A5A5;
  286. }
  287. .fade-leave,
  288. .fade-enter-to {
  289. opacity: 1;
  290. }
  291. .fade-leave-active,
  292. .fade-enter-active {
  293. transition: all 0.3s;
  294. }
  295. .fade-leave-to,
  296. .fade-enter {
  297. opacity: 0;
  298. }
  299. </style>