import { post } from '@/services/ajax'; import { DownOutlined, UpOutlined } from '@ant-design/icons'; import { PageContainer } from '@ant-design/pro-components'; import { Form, Input, Tabs } from 'antd'; import Spin from 'antd/lib/spin'; import { stringify } from 'qs'; import React, { useEffect, useRef, useState } from 'react'; import { IChat, ICustFollow, IStaffsItem } from './ChatLogsType'; import { ChatBar } from './components/ChatBar'; import { ChatTime } from './components/ChatTime'; import styles from './index.module.scss'; const ChatLogs: React.FC = () => { const [param] = useState({ curr_page: 1, page_count: 20, msg_from: '', msg_to_list: '', }); const tabKeyRef = useRef('0'); const [selectStaff, setSelectStaff] = useState(); const selectStaffRef = useRef(); const [staffsList, setStaffsList] = useState([]); const [custFollowsList, setCustFollowsList] = useState([]); const [selectCustFollow, setCustFollow] = useState(); const selectCustFollowRef = useRef(); const [chatLogs, setChatLogs] = useState([]); const [tabs] = useState([ { key: '0', label: '内部联系人', children: '', }, { key: '1', label: '外部联系人', children: '', }, { key: '2', label: '客户群聊', children: '', }, ]); const [flolowsBoxShow, setFlolowsBox] = useState(false); const timeDiffRef = useRef(''); 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 }); return () => { document.removeEventListener('click', show, false); observer.disconnect(); }; }, []); // 获取员工 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); getCustFollowsList(); } } }); }; const page = (curr: number) => { param.curr_page = curr; param.msg_from = selectStaffRef.current?.user_id + ''; param.msg_to_list = selectCustFollowRef.current?.cust_id + ''; timeDiffRef.current = ''; timeShowRef.current = false; getChatLogsList(); }; const getChatLogsList = () => { chatLogLoadingRef.current = true; setChatLogLoading(true); post({ url: '/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)) { if (param.curr_page == 1) { setChatLogs([...res.data.reverse(), { curr_page: param.curr_page }]); } else { setChatLogs([...res.data.reverse(), { curr_page: param.curr_page }, ...chatLogs]); } } } }); }; 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(); return (
{staffsList.map((item) => { return (
{ setSelectStaff({ ...item }); selectStaffRef.current = { ...item }; setFlolowsBox(false); getCustFollowsList(); }} >
{item.name[0]}
{item.name}
); })}
{ e.stopPropagation(); setFlolowsBox(!flolowsBoxShow); }} > {selectStaff ? ( <>
{selectStaff.name[0]}
{selectStaff.name}
{flolowsBoxShow ? : } ) : null}
{custFollowsList.map((item) => { return (
{ setCustFollow(item); selectCustFollowRef.current = item; page(1); }} >
{item.cust_info.name}
); })}
{selectCustFollowRef.current?.cust_info.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 { // if (timeDiffRef.current == '') { // timeDiffRef.current = item.msg_time; // timeShowRef.current = false; // } else { // if ( // new Date(item.msg_time).getTime() - new Date(timeDiffRef.current).getTime() > // 5 * 60 * 1000 // ) { // timeDiffRef.current = item.msg_time; // timeShowRef.current = true; // } else { // timeShowRef.current = false; // } // } // if (i == 0) { // timeShowRef.current = true; // } return (
); } })}
); }; export default ChatLogs;