/** * 当前会话内容 */ import React, { Component } from 'react'; import { connect } from 'dva'; import tim from '../../../utils/imInitialize'; import { message, Button, Avatar } from 'antd'; import './im.less'; import MessageSendBox from './MessageSendBox'; import TextElement from './Message/TextElement'; import ImageElement from './Message/ImageElement'; import SoundElement from './Message/SoundElement'; import FileElement from './Message/FileElement'; import GroupSystemNoticeElement from './Message/GroupSystemNoticeElement'; import CustomSoundElement from './Message/CustomSoundElement'; import { getFullDate } from '../../../utils/utils'; import Close from './Close'; import Record from '../Record'; @connect(state => ({ isCompleted: state.im.isCompleted, nextReqMessageID: state.im.nextReqMessageID, currentUserProfile: state.im.currentUserProfile, currentMessageList: state.im.currentMessageList, currentConversation: state.im.currentConversation, ownName: state.user.currentUser.name, ownAvatar: state.user.currentUser.avatar })) export default class CurrentConversation extends Component { constructor(props) { super(props); this.state = { visible: props.visible, currentMessageList: this.props.currentMessageList, //这个不用渲染数据,只用来记录是否有新数据的传入 // preScrollHeight: 0 startRecord: false, //是否开始录音(用来判断显示录音的页面是否出现) sendSoundURL: null, //录完音要发送的录音路径 sendSoundTime: null, //录完音的时长 key: props.key, isSeeMore: false }; } componentDidMount() { this.scrollMessageListToButtom(); } componentWillReceiveProps(nextProps) { this.setState({ currentMessageList: nextProps.currentMessageList, key: nextProps.key }); // if ( // this.props.currentMessageList.length !== // this.state.currentMessageList.length // ) { // //当前的会话打开的时候就要进行已读上传 // tim.setMessageRead({ // conversationID: this.props.currentConversation.conversationID // }); // this.scrollMessageListToButtom(); // } if ( nextProps.currentMessageList.length > this.state.currentMessageList.length ) { tim.setMessageRead({ conversationID: this.props.currentConversation.conversationID }); if (this.state.isSeeMore) {//是查看更多的时候,就不需要滑动到底部 this.setState({ isSeeMore: false }); } else { this.scrollMessageListToButtom(); } } } // 直接滚到底部 scrollMessageListToButtom() { const that = this; let t2 = window.setInterval(function() { // console.log('每隔1秒钟执行一次'); if (that.refs.messageList) { that.refs.messageList.scrollIntoView(true); window.clearInterval(t2); } }, 50); } //查看更多 getMessageList() { if (this.props.isCompleted) { message.info('没有更多的历史消息了'); return; } const { nextReqMessageID, currentMessageList } = this.props; tim .getMessageList({ conversationID: this.props.currentConversation.conversationID, nextReqMessageID, count: 15 }) .then(imReponse => { this.setState({ isSeeMore: true }); // 更新messageID,续拉时要用到 this.props.dispatch({ type: 'im/updateCurrentConversationCont', payload: { nextReqMessageID: imReponse.data.nextReqMessageID, isCompleted: imReponse.data.isCompleted, currentMessageList: [ ...imReponse.data.messageList, ...currentMessageList ] // 更新当前消息列表,从头部插入 } }); }); } render() { let name = ''; //会话名称 let avatar = ''; //会话头像 if (this.props.currentConversation.type === 'C2C') { name = this.props.currentConversation.userProfile.nick; avatar = this.props.currentConversation.userProfile.avatar; } else if (this.props.currentConversation.type === 'GROUP') { name = this.props.currentConversation.groupProfile.name; avatar = require('../../../assets/images/prevention/group.png'); } return (