tab.js 6.57 KB
import { connect } from 'umi';
import { Icon as LegacyIcon } from '@ant-design/compatible';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import { Tabs, Modal } from 'antd';
import IndexCenter from '@/routes/indexCenter/indexCenter';/* 首页 */
import TabComponent from '@/components/Tab/TabComponent';
import CommonListEdit from '@/routes/commonList/commonListEdit'; /* 可编辑公共列表 */
import CommonList from '@/routes/commonList/commonList';/* 公共列表 */
import CommonBill from '@/routes/commonList/commonBill';/* 通用单据页 */
import CommonNewBill from '@/routes/commonList/commonNewBill';/* 通用单据页 */
import CommonClassify from '@/routes/commonClassify/commonClassify';/* 公共分类 */
import styles from '@/index.less';
import { sendWebSocketMessage } from '@/components/Common/commonFunc';
import { isNotEmptyObject, isNotEmptyArr } from '@/utils/utils'; /* 通用方法 */
import KnifemouldInfo from '@/routes/basicInfoSetting/knifemouldInfo/knifemouldInfo';/* 刀模信息 */
/* 计算方案s */
const { confirm } = Modal;
const { TabPane } = Tabs;

function Tab({ dispatch, app }) {
  const { panes } = app;
  let paneType = '';
  const tagArr = panes.filter(item => item.newRecordFlag !== undefined);
  const tagLastKey = tagArr !== undefined && tagArr.length > 0 ? tagArr[tagArr.length - 1].key : '';
  const tabpanes = panes.map((pane) => {
    const bRecordFlagDisabled = (tagArr.length > 0 && pane.key !== tagLastKey);
    const routeList = {
      '/indexPage/commonList': <CommonList />,
      '/indexPage/commonNewBill': <CommonNewBill />,
      '/indexPage/commonBill' : <CommonBill/>,
      '/indexPage/commonListEdit' : <CommonListEdit/>,
      '/indexPage/commonClassify' : <CommonClassify/>,
      '/indexPage/eleknifemould' : <KnifemouldInfo/>
    }
    const content = routeList[pane.route] || <IndexCenter />;

    if (pane.paneType) {
      paneType = <span><LegacyIcon type={pane.paneType} />{pane.title}</span>;
    } else {
      paneType = <span>{pane.title}</span>;
    }
    return (
      <TabPane tab={paneType} closable={pane.paneType !== 'home'} disabled={bRecordFlagDisabled} key={pane.key} className={styles.tabPane}>
        {content}
      </TabPane>
    );
  });
  const totalProps = {
    tabpanes,
    app,
    onSaveCurrentPane: saveCurrentPane,
    onRemove: removePane,
  };

  const handleSendSocketMessage = (flag, showType, sId, sendTo, msgInfo, param) => {
    const { token } = app;
    const sendws = handleSendWebSocketMsg;
    if (isNotEmptyObject(token)) {
      if (app.webSocket !== null && app.webSocket.readyState === WebSocket.OPEN) {
        sendws(flag, showType, msgInfo, sId, sendTo, param);
      } else {
        dispatch({ type: 'app/createWebSocket', payload: { reStart: true, dispatch } });
        setTimeout(() => {
          sendws(flag, showType, msgInfo, sId, sendTo, param);
        }, 30000);
      }
    }
  };

  const handleSendWebSocketMsg = (flag, showType, msgInfo, sId, sendTo, param) => {
    const { webSocket, userinfo, currentPane } = app;
    const { key } = currentPane;
    sendWebSocketMessage(webSocket, key, flag, showType, userinfo.sId, msgInfo, sId, sendTo, param);
  };

  function saveCurrentPane(currentPane) {
    dispatch({ type: 'app/saveCurrentPane', payload: { currentPane } });
  }

  const clearWebSocketMsg = (i) => {
    const { userinfo } = app;
    const { copyTo } = i;
    if (isNotEmptyObject(copyTo)) {
      const {
        slaveData, srcFormRoute, copyOtherData, masterData, copyToDataSid,
      } = copyTo;
      const sIdArray = [...copyToDataSid || []];

      if (!i?.checkedId) {
        if (isNotEmptyObject(srcFormRoute) && srcFormRoute.includes('materialRequirementsPlanning')) { /* 物料需求计划用sWorkOrderMaterialId作为唯一键 */
          if (isNotEmptyArr(copyOtherData) && copyOtherData[0]?.name === 'detail' && isNotEmptyArr(copyOtherData[0]?.data)) {
            const slaveDetailData = copyOtherData[0].data;
            slaveDetailData.forEach((item) => {
              const redisKey = item.sWorkOrderMaterialId;
              sIdArray.push(redisKey);
            });
          } else { /* 变更单、采购申请单 */
            slaveData.forEach((item) => {
              const redisKey = item.sWorkOrderMaterialId;
              sIdArray.push(redisKey);
            });
          }
        }
      }

      if (isNotEmptyObject(masterData) &&
        isNotEmptyObject(masterData.sSrcSlaveId)) {
        sIdArray.push(masterData.sSrcSlaveId);
      }

      if (Array.isArray(slaveData)) {
        slaveData.forEach((item) => {
          const redisKey = item.sSlaveId;
          sIdArray.push(redisKey);
        });
      }

      const sId = [...new Set(sIdArray)].filter(Boolean).toString();
      handleSendSocketMessage('copyfinish', 'noAction', sId, userinfo.sId, null, null);
    }
    handleSendSocketMessage('release', 'noAction', i?.checkedId || '', userinfo.sId, null, null);
    handleSendSocketMessage('release', 'noAction', i?.formId || '', userinfo.sId, null, null);
  };

  function removePane(changePanes, currentPane, removePane) {
    const removeData = Array.isArray(removePane) ? removePane.filter(Boolean) : [];
    if (removePane && removePane.length === 1 && removePane[0] && removePane[0].fromKey) {
      const currentPaneNew = changePanes.find(item => item.key === removePane[0].fromKey);
      if (currentPaneNew) {
        currentPane = currentPaneNew;
      }
    }
    if (removePane && removePane[0]) {
      const isProductionScheduleTree = removePane[0].route.indexOf('systemPermission') > -1 ||
        (removePane[0].route.indexOf('production') > -1);
      if (isProductionScheduleTree) {
        removeData.forEach((i) => {
          clearWebSocketMsg(i);
        });
        dispatch({ type: 'app/removePane', payload: { changePanes, currentPane } });
        return;
      }
    }
    if (app.diffMap && removePane[0] && app.diffMap.get(removePane[0].key)) {
      confirm({
        icon: <ExclamationCircleOutlined />,
        content: '内容未保存,是否离开?',
        onOk() {
          removeData.forEach((i) => {
            clearWebSocketMsg(i);
          });
          dispatch({ type: 'app/removePane', payload: { changePanes, currentPane } });
        },
        onCancel() {
          console.log('取消关闭');
        },
      });
    } else {
      removeData.forEach((i) => {
        clearWebSocketMsg(i);
      });
      dispatch({ type: 'app/removePane', payload: { changePanes, currentPane } });
    }
  }
  return (
    <div style={{ height: '100%' }}>
      <TabComponent {...totalProps} />
    </div>
  );
}

export default connect(({ app }) => ({ app }))(Tab);