import { post } from '@/services/ajax'; import { DownOutlined, UpOutlined } from '@ant-design/icons'; import { PageContainer } from '@ant-design/pro-components'; import { Form, Input, Modal, Tabs } from 'antd'; import Spin from 'antd/lib/spin'; import { stringify } from 'qs'; import React, { useEffect, useRef, useState } from 'react'; import { IChat, ICustFollow, IGroup, IGroupMembers, IStaffsItem } from './ChatLogsType'; import { ChatBar } from './components/ChatBar'; import { ChatTime } from './components/ChatTime'; import styles from './index.module.scss'; interface ISearchWord { '0': string; '1': string; '2': string; } const ChatLogs: React.FC = () => { const [param] = useState({ curr_page: 1, page_count: 20, msg_from: '', msg_to_list: '', room_id: '', }); const [tabKey, setTabKey] = useState('0'); const tabKeyRef = useRef('0'); const [open, setOpen] = useState(false); // 员工 const [staffsList, setStaffsList] = useState([]); const [selectStaff, setSelectStaff] = useState(); const selectStaffRef = useRef(); // 内部联系人 const [innerStaffsList, setInnerStaffsList] = useState([]); const selectInnerStaffRef = useRef(); const [selectInnerStaff, setSelectInnerStaff] = useState(); // 外部联系人 const [custFollowsList, setCustFollowsList] = useState([]); const [selectCustFollow, setSelectCustFollow] = useState(); const selectCustFollowRef = useRef(); // 群聊 const [groupList, setGroupList] = useState([]); const selectGroupRef = useRef(); const [selectGroup, setSelectGroup] = useState(); // 群成员 const [groupMembersList, setGroupMembersList] = useState([]); const groupMembersObjRef = useRef({}); const [chatLogs, setChatLogs] = useState([]); const [tabs] = useState([ { key: '0', label: '内部联系人', children: '', }, { key: '1', label: '外部联系人', children: '', }, { key: '2', label: '客户群聊', children: '', }, ]); const groupStatus = { '0': '跟进人正常', '1': '跟进人离职', '2': '离职继承中', '3': '离职继承完成', }; const [searchWord, setSearchWord] = useState({ '0': '', '1': '', '2': '', }); const [flolowsBoxShow, setFlolowsBox] = useState(false); const timeShowRef = useRef(false); const chatBoxRef = useRef(); const isAllChatRef = useRef(false); const chatLogLoadingRef = useRef(false); const [chatLogLoading, setChatLogLoading] = useState(false); function show() { setFlolowsBox(false); } const callback = function (mutationsList: any) { // Use traditional 'for loops' for IE 11 for (let mutation of mutationsList) { if (mutation.type === 'childList') { document.querySelector('.curr_page' + param.curr_page)?.scrollIntoView(true); } else if (mutation.type === 'attributes') { console.log('The ' + mutation.attributeName + ' attribute was modified.'); } } }; const observer = new MutationObserver(callback); useEffect(() => { document.addEventListener('click', show, false); getStaffsList(); observer.observe(chatBoxRef.current, { childList: true }); const myScript = document.createElement('script'); myScript.src = '/public/scripts/amrnb.js'; myScript.async = false; document.body.appendChild(myScript); return () => { document.removeEventListener('click', show, false); observer.disconnect(); document.body.removeChild(myScript); }; }, []); // 获取员工 const getStaffsList = () => { post({ url: '/Staffs/Data' }).then((res) => { if (res.err_code == 0) { if (Array.isArray(res.data)) { // setSelectStaff(res.data[0]); // selectStaffRef.current = res.data[0]; res.data.forEach((element: IStaffsItem) => { if (element.user_id == 'yangxb') { setSelectStaff(element); selectStaffRef.current = element; } }); setStaffsList(res.data); setInnerStaffsList(res.data); getCustFollowsList(); getGroupList(); } } }); }; const page = (curr: number) => { param.curr_page = curr; param.msg_from = selectStaffRef.current?.user_id + ''; if (tabKey == '0') { param.msg_to_list = selectInnerStaffRef.current?.user_id + ''; } else if (tabKey == '1') { param.msg_to_list = selectCustFollowRef.current?.cust_id + ''; } else { param.room_id = selectGroupRef.current?.group_id + ''; } timeShowRef.current = false; getChatLogsList(); }; const getChatLogsList = () => { chatLogLoadingRef.current = true; setChatLogLoading(true); post({ url: tabKey == '2' ? '/ChatLogs/GroupList' : '/ChatLogs/List', data: stringify(param), }).then((res) => { const count = res.count || 0; chatLogLoadingRef.current = false; setChatLogLoading(false); isAllChatRef.current = count < param.page_count * param.curr_page; if (res.err_code == 0) { if (Array.isArray(res.data) && res.data.length) { let arr: IChat[] = []; const temp = res.data.reverse(); const mark = { curr_page: param.curr_page, msg_time: temp[temp.length - 1].msg_time }; if (param.curr_page == 1) { arr = [...temp, mark]; } else { arr = [...temp, mark, ...chatLogs]; } if (arr) { let show_time = ''; arr.forEach((el, i) => { el.show_time = false; if (i == 0) { el.show_time = true; } else { if (show_time == '') { show_time = el.msg_time; } else { if ( new Date(el.msg_time).getTime() - new Date(show_time).getTime() > 5 * 60 * 1000 ) { el.show_time = true; show_time = el.msg_time; } } } }); } setChatLogs(arr); } } }); }; const getGroupList = () => { post({ url: '/Groups/GroupsList', data: stringify({ user_id: selectStaffRef.current?.user_id }), }).then((res) => { if (res.err_code == 0) { if (Array.isArray(res.data)) { setGroupList(res.data); // if (res.data.length) { // setCustFollow(res.data[0]); // selectCustFollowRef.current = res.data[0]; // page(1); // } } } }); }; // 群成员 const getGroupMembersList = () => { post({ url: '/GroupMembers/GroupMembersList', data: stringify({ group_id: selectGroupRef.current?.group_id }), }).then((res) => { if (res.err_code == 0) { if (Array.isArray(res.data)) { setGroupMembersList(res.data); groupMembersObjRef.current = {}; res.data.forEach((item: IGroupMembers) => { groupMembersObjRef.current[item.user_id] = item; }); page(1); } } }); }; const getCustFollowsList = () => { post({ url: '/CustFollows/List', data: stringify({ user_id: selectStaffRef.current?.user_id }), }).then((res) => { if (res.err_code == 0) { if (Array.isArray(res.data)) { setCustFollowsList(res.data); // if (res.data.length) { // setCustFollow(res.data[0]); // selectCustFollowRef.current = res.data[0]; // page(1); // } } } }); }; // const { notification } = App.useApp(); const tabContent = () => { if (tabKey == '0') { // 内部联系人 return ( <> {innerStaffsList.length ? ( innerStaffsList.map((item) => { if (item.user_id == selectStaff?.user_id) { return null; } return (
{ tabKeyRef.current = tabKey; setSelectCustFollow(undefined); selectCustFollowRef.current = undefined; setSelectGroup(undefined); selectGroupRef.current = undefined; setSelectInnerStaff(item); selectInnerStaffRef.current = item; page(1); }} style={{ display: item.name.includes(searchWord['0']) ? '' : 'none' }} >
{item.name[0]}
{item.name}
); }) ) : (
暂无内部联系人
)} ); } else if (tabKey == '1') { return ( <> {custFollowsList.length ? ( custFollowsList.map((item) => { return (
{ tabKeyRef.current = tabKey; setSelectInnerStaff(undefined); selectInnerStaffRef.current = undefined; setSelectGroup(undefined); selectGroupRef.current = undefined; setSelectCustFollow(item); selectCustFollowRef.current = item; page(1); }} style={{ display: item.name.includes(searchWord['1']) ? '' : 'none' }} >
{item.name}
{item.remark}
); }) ) : (
暂无外部联系人
)} ); } else { return ( <> {groupList.length ? ( groupList.map((item) => { return (
{ tabKeyRef.current = tabKey; setSelectCustFollow(undefined); selectCustFollowRef.current = undefined; setSelectInnerStaff(undefined); selectInnerStaffRef.current = undefined; setSelectGroup(item); selectGroupRef.current = item; getGroupMembersList(); }} style={{ display: item.name.includes(searchWord['2']) ? '' : 'none' }} >
{item.name ? item.name[0] : '群'}
{item.name || '未定义群名'}
{groupStatus[item.status]}
); }) ) : (
暂无客户群聊
)} ); } }; const formatTags = () => { if (selectCustFollow?.tags) { try { const tags = JSON.parse(selectCustFollow?.tags); if (Array.isArray(tags)) { return ( <> {tags.map((item) => { return (
{item.group_name}:{item.tag_name}
); })} ); } } catch (e) {} } return <>; }; function adminList() { if (selectGroupRef.current?.admin_list) { try { const msg = JSON.parse(selectGroupRef.current?.admin_list); if (Array.isArray(msg) && msg.length) { let arr: any = []; msg.forEach((el) => { arr.push(groupMembersObjRef.current[el.userid]?.name); }); return
群管理者:{arr.join(',')}
; } } catch (e) { return <>; } } return <>; } return (
{staffsList.map((item) => { return (
{ setSelectCustFollow(undefined); selectCustFollowRef.current = undefined; setSelectInnerStaff(undefined); selectInnerStaffRef.current = undefined; setSelectGroup(undefined); selectGroupRef.current = undefined; isAllChatRef.current = false; setChatLogs([]); setSelectStaff({ ...item }); selectStaffRef.current = { ...item }; setFlolowsBox(false); getCustFollowsList(); getGroupList(); }} style={{ background: selectStaff?.user_id == item.user_id ? '#e6f4ff' : '' }} >
{item.name[0]}
{item.name}
); })}
{ e.stopPropagation(); setFlolowsBox(!flolowsBoxShow); }} > {selectStaff ? ( <>
{selectStaff.name[0]}
{selectStaff.name}
{flolowsBoxShow ? : } ) : null}
{ searchWord[tabKey] = e.target.value.trim(); setSearchWord({ ...searchWord }); }} allowClear >
{ setTabKey(val); }} >
{tabContent()}
{ setOpen(true); }} > {tabKeyRef.current == '0' ? ( <>{selectInnerStaffRef.current?.name} ) : tabKeyRef.current == '1' ? ( <>{selectCustFollowRef.current?.name} ) : ( <>{selectGroupRef.current ? selectGroupRef.current?.name || '未定义群名' : ''} )}
{selectInnerStaffRef.current?.name} 详细信息 ) : tabKeyRef.current == '1' ? ( <>{selectCustFollowRef.current?.name} 详细信息 ) : ( <>{selectGroupRef.current?.name || '未定义群名'} 详细信息 ) } open={open} centered onCancel={() => setOpen(false)} footer={false} > {tabKeyRef.current == '0' ? (
{selectInnerStaff?.name[0]}
{selectInnerStaff?.name}
账号:{selectInnerStaff?.name}
手机:{selectInnerStaff?.telephone}
职务:{selectInnerStaff?.position}
) : tabKeyRef.current == '1' ? (
{selectCustFollow?.name} {selectCustFollow?.gender == 1 ? ( ) : ( )}
{selectCustFollow?.description}
备注名称:{selectCustFollow?.remark}
{formatTags()}
) : (
群创建者:{groupMembersObjRef.current[selectGroupRef.current?.owner]?.name}
{adminList()}
创建时间:{selectGroupRef.current?.create_time}
群公告:{selectGroupRef.current?.notice}
群成员:
{groupMembersList.map((item) => { return (
{item.avatar ? ( ) : item.name ? ( item.name[0] ) : ( '' )}
{item.name}
); })}
)}
{ if ( e.target?.scrollTop == 0 && !isAllChatRef.current && !chatLogLoadingRef.current ) { page(param.curr_page + 1); } }} > {isAllChatRef.current ? (
没有更多聊天记录了
) : null} {chatLogs.map((item, i) => { if (item.curr_page) { return (
); } else { return (
{item.show_time ? : null} {tabKey == '2' ? ( ) : ( )}
); } })}
); }; export default ChatLogs;