Workflow.jsx 12.3 KB
Newer Older
1
/* eslint-disable prettier/prettier */
2
import React, { useEffect, useState } from 'react';
邓超's avatar
邓超 committed
3
import { WFGetAllFlow, GetFlowNode, DeleteFlow } from '@/services/workflow/workflow';
4 5 6 7 8 9 10 11 12 13

import { Card, Spin, Tooltip, notification, Empty, Modal } from 'antd';
import {
  DoubleLeftOutlined,
  DoubleRightOutlined,
  PlusSquareFilled,
  FileAddTwoTone,
  FormOutlined,
  DeleteTwoTone,
  ExclamationCircleOutlined,
14
  ControlOutlined,
15 16 17 18 19 20 21 22
} from '@ant-design/icons';

import classnames from 'classnames';
import Tree from '@/components/ExpendableTree';
import PageContainer from '@/components/BasePageContainer';
import FlowChart from './workFlowComponents/FlowChart';
import FlowModal from './workFlowComponents/FlowModal';
import FlowGroupModal from './workFlowComponents/FlowGroupModal';
23
import Timelimit from '../workFlow/flowComponents/Timelimit';
24 25

import styles from './workflow.less';
26

27 28 29 30 31 32 33 34 35 36 37 38 39
const { confirm } = Modal;

const Workflow = () => {
  const [treeLoading, setTreeLoading] = useState(false);
  const [treeData, setTreeData] = useState([]); // 流程数据
  const [currentSelectId, setCurrentSelectId] = useState(''); // 选中得节点
  const [flowData, setFlowData] = useState({ Nodes: [], Lines: [] }); // 流程图数据
  const [treeId, setTreeId] = useState(''); // 树形节点ID
  const [modalType, setModalType] = useState(''); // 弹窗类型是编辑还是新增
  const [editMsg, setEditMsg] = useState({}); // 弹窗编辑回显
  const [editIndex, setEditIndex] = useState(); // 编辑流程组得索引
  const [treeVisible, setTreeVisible] = useState(true);
  const [chartLoading, setChartLoading] = useState(false); // 流程图的loading
皮倩雯's avatar
皮倩雯 committed
40
  const [canSelect, setCanSelect] = useState(true); // 是否可以切换树
41 42
  const [expandedKey, setExpandedKey] = useState(''); // 默认展开项
  const [flag, setFlag] = useState(1);
43
  const [keep, setKeep] = useState([]);
44 45 46
  const [visible, setVisible] = useState({
    FlowModal: false,
    FlowGroupModal: false,
47
    auxiliaryView: false,
48 49 50 51
  }); // 弹窗显示
  useEffect(() => {
    getFlowList();
  }, []);
52 53
  useEffect(() => {
    if (flag === 2) {
54 55 56
      if (treeData.length === 0) {
        return;
      }
57
      setExpandedKey(treeData[0].name + 0);
58 59 60
      if (!treeData[0].children) {
        return;
      }
61 62 63 64 65
      setCurrentSelectId(treeData[0].children[0].FlowID);
      setTreeId(treeData[0].children[0].FlowID);
      GetFlowNode({ flowID: treeData[0].children[0].FlowID }).then(res => {
        setChartLoading(false);
        if (res.code === 0) {
邓超's avatar
邓超 committed
66 67 68
          res.data.Nodes.forEach(item => {
            item.nodeDetail = JSON.stringify(item);
          });
69
          setFlowData({ ...res.data, flowName: treeData[0].children[0].FlowName });
70
        } else {
71
          setFlowData({ Nodes: [], Lines: [], flowName: '' });
72 73 74 75 76 77 78 79 80
          notification.error({
            title: '提示',
            duration: 3,
            description: res.msg,
          });
        }
      });
    }
  }, [treeData]);
81 82 83 84 85 86 87 88 89 90
  // 弹窗显示控制
  const showModal = (key, value) => {
    setVisible({ ...visible, [key]: value });
  };
  // 获取流程列表
  const getFlowList = () => {
    setTreeLoading(true);
    WFGetAllFlow().then(res => {
      setTreeLoading(false);
      if (res.code === 0) {
91
        setFlag(flag + 1);
92 93 94 95 96 97
        let listdata = [];
        res.data.forEach(i => {
          if (i.children.length > 0) {
            i.children.forEach(j => {
              listdata.push(j.FlowName);
            });
98
          }
99 100 101
        });

        setKeep(listdata);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        let data = res.data.map(item => {
          item.isOld = true;
          return item;
        });
        setTreeData(data);
      }
    });
  };
  // 新增流程
  const addFlow = (val, e) => {
    e.stopPropagation();
    showModal('FlowModal', true);
    setEditMsg(val);
    setModalType('add');
  };
  // 编辑流程
  const editFlow = (val, e) => {
    e.stopPropagation();
    showModal('FlowModal', true);
    setModalType('edit');
    setEditMsg(val);
  };
124
  const timeConfig = (val, e) => {
邓超's avatar
邓超 committed
125
    console.log(val, 'val1');
126 127 128 129 130
    e.stopPropagation();
    showModal('auxiliaryView', true);
    setModalType('edit');
    setEditMsg({ ...val, name: val.FlowName, ID: val.FlowID });
  };
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  // 删除流程
  const delFlow = (val, e) => {
    e.stopPropagation();
    confirm({
      title: '确定要删除吗?',
      icon: <ExclamationCircleOutlined />,
      content: '',
      okText: '是',
      okType: 'danger',
      cancelText: '否',
      onOk() {
        DeleteFlow({ FlowId: val.FlowID })
          .then(res => {
            if (res.code === 0) {
              getFlowList();
              notification.success({
                message: '提示',
                duration: 3,
                description: '删除成功',
              });
            } else {
              notification.error({
                message: '提示',
                duration: 3,
                description: res.msg,
              });
            }
          })
          .catch(() => {
            notification.error({
              message: '提示',
              duration: 3,
              description: '网络异常请稍后再试',
            });
          });
      },
      onCancel() {},
    });
  };
  // 编辑流程组
  const eiditFlowGroup = (val, e, index) => {
    e.stopPropagation();
    setEditIndex(index);
    showModal('FlowGroupModal', true);
    setModalType('edit');
    setEditMsg(val);
  };
  // 添加流程组
  const addFlowGroup = e => {
    e.stopPropagation();
    showModal('FlowGroupModal', true);
    setModalType('add');
  };
皮倩雯's avatar
皮倩雯 committed
184 185
  // 选择节点
  const chooseNode = (prop, treeNode) => {
186 187 188 189 190 191 192
    if (!treeNode || treeNode.node.children) {
      setCurrentSelectId(treeId);
      return;
    }
    // 组节点不触发选中
    if (prop[0]) {
      // 选中节点处理
193 194

      setTreeVisible(false);
195 196 197 198 199 200
      setCurrentSelectId(prop[0]);
      setTreeId(prop[0]);
      setChartLoading(true);
      GetFlowNode({ flowID: prop[0] }).then(res => {
        setChartLoading(false);
        if (res.code === 0) {
201 202 203
          res.data.Nodes.forEach(item => {
            item.nodeDetail = JSON.stringify(item);
          });
204
          setFlowData({ ...res.data, flowName: treeNode.node.flowName });
205
        } else {
206
          setFlowData({ Nodes: [], Lines: [], flowName: '' });
207 208 209 210 211 212 213 214 215 216 217
          notification.error({
            title: '提示',
            duration: 3,
            description: res.msg,
          });
        }
      });
    } else {
      setCurrentSelectId(treeId);
    }
  };
皮倩雯's avatar
皮倩雯 committed
218 219 220
  // 点击节点树触发
  const onSelect = (prop, treeNode) => {
    // 如果没有保存弹是否保存弹窗
221 222

    if (!canSelect && !treeNode.node.children) {
皮倩雯's avatar
皮倩雯 committed
223
      confirm({
邓超's avatar
邓超 committed
224
        title: '已编辑流程未发布,确定要离开吗?',
皮倩雯's avatar
皮倩雯 committed
225 226 227
        icon: <ExclamationCircleOutlined />,
        content: '',
        okText: '是',
邓超's avatar
邓超 committed
228
        okType: 'primary',
皮倩雯's avatar
皮倩雯 committed
229 230 231 232 233 234 235 236 237 238 239
        cancelText: '否',
        onOk() {
          setCanSelect(true);
          chooseNode(prop, treeNode);
        },
        onCancel() {},
      });
    } else {
      chooseNode(prop, treeNode);
    }
  };
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  // 处理树数据
  const mapTree = (val, index) => {
    const obj = { ...val };
    return {
      title: (
        <div className={styles.nodeTitle}>
          <span>{obj.name}</span>
          <div className={styles.nodeTip}>
            <Tooltip title="新增流程" className={styles.fs}>
              <FileAddTwoTone onClick={e => addFlow(obj, e)} />
            </Tooltip>
            <Tooltip title="编辑流程组" className={styles.fs}>
              <FormOutlined onClick={e => eiditFlowGroup(obj, e, index)} />
            </Tooltip>
          </div>
        </div>
      ),
      key: obj.name + index,
      children: obj.children ? obj.children.map(i => mapTreeNode(i)) : [],
    };
  };
  // 渲染树子节点
  const mapTreeNode = val => {
    const obj = { ...val };
    return {
      title: (
        <div className={styles.nodeTitle}>
          <span
            style={{
              overflow: 'hidden',
              textOverflow: 'ellipsis',
              whiteSpace: 'nowrap',
            }}
          >
            {obj.FlowName}
          </span>
          <div className={styles.nodeTip}>
277
            {/* <Tooltip title="时限配置">
278 279 280 281 282 283
              <ControlOutlined
                onClick={e => {
                  timeConfig(obj, e);
                }}
                style={{ fontSize: '16px', color: '#1890FF' }}
              />
284
            </Tooltip> */}
285 286 287 288 289 290 291 292 293
            <Tooltip title="编辑流程" className={styles.fs}>
              <FormOutlined onClick={e => editFlow(obj, e)} />
            </Tooltip>
            <Tooltip title="删除流程" className={styles.fs}>
              <DeleteTwoTone onClick={e => delFlow(obj, e)} />
            </Tooltip>
          </div>
        </div>
      ),
294
      flowName: obj.FlowName,
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
      key: obj.FlowID,
    };
  };
  //  保存分组后的回调函数
  const groupCallBack = val => {
    showModal('FlowGroupModal', false);
    // 编辑老数据需要掉接口更新新数据手动修改数据,新增插入一条数据
    if (modalType === 'edit' && editMsg.isOld) {
      getFlowList();
    } else if (modalType === 'edit') {
      let newTree = [...treeData];
      newTree[editIndex].name = val;
      setTreeData(newTree);
    } else {
      setTreeData([...treeData, { name: val }]);
    }
    // 选中当前选中的节点
    onSelect();
  };
皮倩雯's avatar
皮倩雯 committed
314 315 316 317
  // 数据改变后的回调函数
  const leaveCallBack = val => {
    setCanSelect(!val);
  };
318 319 320 321 322 323 324 325 326 327 328
  return (
    <PageContainer>
      <div className={styles.flowContainer}>
        {/* 流程树 */}
        <Spin spinning={treeLoading}>
          <Card
            className={classnames({
              [styles.treeContainer]: true,
              [styles.treeContainerHide]: !treeVisible,
            })}
          >
329
            <div style={{ display: `${treeVisible ? 'block' : 'none'}`, height: '100%' }}>
330
              <span className={styles.processTitle}>流程树</span>
331 332
              <Tooltip title="添加流程">
                <PlusSquareFilled onClick={e => addFlow({}, e)} className={styles.treeHeadIcon} />
333 334 335 336 337 338 339 340 341
              </Tooltip>
              <hr className={styles.splitLine} />
              <div className={styles.treeContent}>
                <Tree
                  blockNode
                  autoExpandParent
                  onSelect={onSelect}
                  selectedKeys={[currentSelectId]}
                  treeData={treeData.map((item, index) => mapTree(item, index))}
342
                  expandedKeys={expandedKey}
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
                />
              </div>
            </div>
            <div className={styles.switcher}>
              {treeVisible && (
                <Tooltip title="隐藏流程列表">
                  <DoubleLeftOutlined onClick={() => setTreeVisible(false)} />
                </Tooltip>
              )}
              {!treeVisible && (
                <Tooltip title="显示流程列表">
                  <DoubleRightOutlined onClick={() => setTreeVisible(true)} />
                </Tooltip>
              )}
            </div>
          </Card>
        </Spin>
        {/* 流程图 */}
        <div className={styles.flowChartContainer}>
          {currentSelectId ? (
            <FlowChart
364
              treeVisible={treeVisible}
365 366 367
              flowData={flowData}
              flowID={currentSelectId}
              chartLoading={chartLoading}
皮倩雯's avatar
皮倩雯 committed
368
              leaveCallBack={leaveCallBack}
369 370
            />
          ) : (
371
            <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="当前未选中流程" />
372 373 374 375 376 377 378 379 380 381
          )}
        </div>
      </div>
      {/* 添加流程弹窗 */}
      <FlowModal
        visible={visible.FlowModal}
        msg={editMsg}
        modalType={modalType}
        handleCancel={() => showModal('FlowModal', false)}
        treeData={treeData}
382
        keep={keep}
383 384 385 386 387 388 389 390 391 392 393 394
        onSubumit={() => {
          showModal('FlowModal', false);
          getFlowList();
        }}
      />
      {/* 创建分组弹窗 */}
      <FlowGroupModal
        visible={visible.FlowGroupModal}
        msg={editMsg}
        modalType={modalType}
        handleCancel={() => showModal('FlowGroupModal', false)}
        treeData={treeData}
395
        keep={keep}
396 397
        onSubumit={val => groupCallBack(val)}
      />
398
      {/* 流程时限配置 */}
399
      {/* <Timelimit
400 401 402
        visible={visible.auxiliaryView}
        msg={editMsg}
        handleCancel={() => showModal('auxiliaryView', false)}
403
      /> */}
404 405 406 407 408
    </PageContainer>
  );
};

export default Workflow;