import React, { useState, useEffect } from 'react'; import { Modal, Form, Input, Radio, Message } from '@arco-design/web-react'; import { appPublish } from '@/api/flow'; import { getPubFlowList, getMyFlowList } from '@/api/flow'; import { getMyComponents, getPubComponents, getTeamComponents } from '@/api/components'; import { useSelector } from 'react-redux'; import { publishType } from '@/api/interface'; import dayjs from 'dayjs'; interface PublishFlowModalProps { visible: boolean; onCancel: () => void; onSuccess: () => void; appId?: string; flowId?: string; currentFlowName?: string; } const FormItem = Form.Item; const RadioGroup = Radio.Group; const TextArea = Input.TextArea; const PublishFlowModal: React.FC = ({ visible, onCancel, onSuccess, appId, flowId, currentFlowName }) => { const [form] = Form.useForm(); const [loading, setLoading] = useState(false); const { appRuntimeData, currentAppData } = useSelector((state: any) => state.ideContainer); // 当弹窗打开时,初始化表单数据 useEffect(() => { if (visible) { form.setFieldsValue({ name: currentFlowName || '', published: 1, // 默认发布为公共流程 tag: '', description: '' }); } }, [visible, currentFlowName, form]); // 当弹窗关闭时,重置表单 useEffect(() => { if (!visible) { form.resetFields(); } }, [visible, form]); // 更新组件和流程库数据 const updateComponentData = async () => { try { const requests = [ { promise: getMyComponents(), key: 'myLibs' }, { promise: getPubComponents(), key: 'pubLibs' }, { promise: getTeamComponents(), key: 'teamLibs' }, { promise: getPubFlowList({ currPage: 1, pageSize: 999 }), key: 'pubFlow' }, { promise: getMyFlowList({ currPage: 1, pageSize: 999 }), key: 'myFlow' } ]; const obj: any = { myLibs: null, pubLibs: null, teamLibs: null, pubFlow: null, myFlow: null, updateTime: dayjs().format('YYYY-MM-DD HH:mm:ss') }; const userInfo = JSON.parse(sessionStorage.getItem('userInfo') || '{}'); // 分别处理每个请求 for (const { promise, key } of requests) { try { const res: any = await promise; if (res?.code === 200) { if (key === 'pubFlow' || key === 'myFlow') { res?.data.list.forEach(item => { item['fontCompType'] = 'complex'; }); // 更新本地存储数据 obj[key] = res?.data.list || null; } // 给协同组件添加一个后续处理数据时使用的类型 else if (key === 'teamLibs') { res.data.forEach(item => { item.children.forEach(v => { v['fontCompType'] = 'team'; }); }); obj[key] = res?.data || null; } else { // 更新本地存储数据 res.data.forEach(item => { item.children.forEach(v => { v['fontCompType'] = 'normal'; }); }); obj[key] = res?.data || null; } } sessionStorage.setItem(`compLibs${userInfo.userId}`, JSON.stringify(obj)); } catch (error) { console.error(`加载${key}失败:`, error); } } } catch (error) { console.error('更新组件库失败:', error); } }; const handleSubmit = async () => { try { const values = await form.validate(); console.log('currentAppData:', currentAppData); setLoading(true); let publishData: publishType = {}; if (currentAppData.hasOwnProperty('parentAppId')) { publishData = { appId: currentAppData.parentAppId, flowId: currentAppData.key, name: values.name, published: values.published, tag: values.tag, description: values.description, isMain: 0 // 子流程 }; } else { publishData = { appId: currentAppData.id, flowId: '', name: values.name, published: values.published, tag: values.tag, description: values.description, isMain: 1 // 主流程 }; } const res: any = await appPublish(publishData); if (res.code === 200) { Message.success('流程发布成功'); form.resetFields(); // 发布成功后更新组件和流程库数据 await updateComponentData(); onSuccess(); } else { Message.error(res.msg || '流程发布失败'); } } catch (error) { console.error('流程发布失败:', error); if (error?.errors) { // 表单验证错误,不需要显示消息 return; } Message.error('流程发布失败'); } finally { setLoading(false); } }; const handleClose = () => { form.resetFields(); onCancel(); }; return (
私有流程(仅自己可见) 公共流程(所有人可见)