import React from "react"; import {Button, Col, Container, FormControl, Image, ListGroup, Row,} from "react-bootstrap"; import {Chat, PageProps, User} from "../entity"; import {Tooltip} from "@material-ui/core"; import moment from "moment"; import {MyInfo} from "./MyInfo"; import {MyDialog} from "../ui/MyDialog"; import {userObj} from "../ui/TestData"; import {JSONResponse, Method, request} from "../interface"; import {EmptyBodyTransform, PageDataMessage, SimpleMessage, UserRes, UserTransform} from "../result"; import {loadMyFriend, scrollBottom} from "../public"; import {Api} from "../api"; import {SimpleSnackbar} from "../ui/toast"; const maxLength=150 /** * 我的好友 */ export class MyFriend extends React.Component< { user:string }, { //好友列表 friendList?:Array; //查找好友关键字 queryFriend:string; //查找用户关键字 queryUser:string; //用户列表 userList?:Array; //聊天记录 chatList:Array; //当前选中好友 friendIndex:number|null; //发送内容 sendContent:String; //查看用户ID userId:string|null; //操作反馈 result:JSX.Element|null; //分页信息 page?:PageProps; //滚动提示 scrollTip:string|null, } >{ constructor(props: Readonly) { super(props); this.state={ //好友列表 friendList:[], //查找好友关键字 queryFriend:"", queryUser:"", //聊天记录 chatList:[], //当前选中好友 friendIndex:null, //发送内容 sendContent:'', userId:null, result:null, scrollTip:null } } componentDidMount() { loadMyFriend('',this,1) this.joinChat() } componentWillUnmount() { this.leaveChat() } /** * 进入聊天室 */ joinChat(){ this.refreshStatus(true) } /** * 刷新聊天室状态 */ refreshStatus(chatStatus:boolean){ let that=this request(Api.account.refreshChat,Method.POST, {chatStatus:String(chatStatus)},new EmptyBodyTransform(),function (res:JSONResponse) { switch (res.customResult) { case SimpleMessage.fail: that.setState({ result:

{chatStatus?"进入":"退出"}聊天室失败,请联系管理员

}) break } }) } /** * 离开聊天室 */ leaveChat(){ this.refreshStatus(false) } /** * 查找用户 */ queryUser(name:string){ if(!name){ this.setState({ userList:[] }) }else { let that = this request(Api.account.findUser, Method.GET, {name:name}, new UserTransform(), function (res: UserRes) { switch (res.customResult) { case PageDataMessage.ok: that.setState({ userList: res.dataList }) break } }) } } //加载聊天记录 loadMyChat(user:User){ this.setState({ chatList:[userObj,userObj,userObj,userObj] }) } render() { const that=this return (
scrollBottom(e,that,function () { loadMyFriend(that.state.queryFriend,that,(that.state.page?.currentPage||1)+1) }) }> 我的好友 { this.setState({ queryFriend:e.target.value }) loadMyFriend(e.target.value,this,1) }}/> {this.state.friendList?this.state.friendList.map((friend:User,index:number)=> { this.setState({ friendIndex:index }) this.loadMyChat(friend) }}>{friend.name} 查看用户信息this.setState({userId:friend.userId})}/> ):null} {this.state.scrollTip!==null?this.setState({scrollTip:null})} duration={1000}/>:null} 添加好友 { this.setState({ queryUser:e.target.value }) this.queryUser(e.target.value) }}/> {this.state.userList?this.state.userList.length>0?this.state.userList.map((user:User,index:number)=> {user.name} 查看用户信息this.setState({userId:user.userId})}/> ):没有匹配任何用户:null}
{this.state.chatList.map((chat:Chat,index:number)=> this.props.user===chat.userId? {chat.name}
{moment(chat.time).format("YYYY-MM-DD HH:mm:ss")}

{chat.content}

:
{moment(chat.time).format("YYYY-MM-DD HH:mm:ss")}

{chat.content}

{chat.name}
)}
{ if(e.target.innerText.length>=maxLength){ e.preventDefault() } }} onKeyUp={(e:any)=> { this.setState({sendContent:e.target.innerText}) }}>

{"您还可以输入"+(maxLength-this.state.sendContent.length)+"个字符"}

this.setState({ result:null })}/> } open={this.state.userId!==null} onClose={()=>this.setState({userId:null})}/>
); } }