index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <template>
  2. <ContentWrap>
  3. <div class="mx-auto">
  4. <!-- 头部导航栏 -->
  5. <div
  6. class="absolute top-0 left-0 right-0 h-50px bg-white border-bottom z-10 flex items-center px-20px"
  7. >
  8. <!-- 左侧标题 -->
  9. <div class="w-200px flex items-center overflow-hidden">
  10. <Icon icon="ep:arrow-left" class="cursor-pointer flex-shrink-0" @click="handleBack" />
  11. <span class="ml-10px text-16px truncate" :title="formData.name || '创建流程'">
  12. {{ formData.name || '创建流程' }}
  13. </span>
  14. </div>
  15. <!-- 步骤条 -->
  16. <div class="flex-1 flex items-center justify-center h-full">
  17. <div class="w-400px flex items-center justify-between h-full">
  18. <div
  19. v-for="(step, index) in steps"
  20. :key="index"
  21. class="flex items-center cursor-pointer mx-15px relative h-full"
  22. :class="[
  23. currentStep === index
  24. ? 'text-[#3473ff] border-[#3473ff] border-b-2 border-b-solid'
  25. : 'text-gray-500'
  26. ]"
  27. @click="handleStepClick(index)"
  28. >
  29. <div
  30. class="w-28px h-28px rounded-full flex items-center justify-center mr-8px border-2 border-solid text-15px"
  31. :class="[
  32. currentStep === index
  33. ? 'bg-[#3473ff] text-white border-[#3473ff]'
  34. : 'border-gray-300 bg-white text-gray-500'
  35. ]"
  36. >
  37. {{ index + 1 }}
  38. </div>
  39. <span class="text-16px font-bold whitespace-nowrap">{{ step.title }}</span>
  40. </div>
  41. </div>
  42. </div>
  43. <!-- 右侧按钮 -->
  44. <div class="w-200px flex items-center justify-end gap-2">
  45. <el-button v-if="actionType === 'update'" type="success" @click="handleDeploy">
  46. 发 布
  47. </el-button>
  48. <el-button type="primary" @click="handleSave">
  49. <span v-if="actionType === 'definition'">恢 复</span>
  50. <span v-else>保 存</span>
  51. </el-button>
  52. </div>
  53. </div>
  54. <!-- 主体内容 -->
  55. <div class="mt-50px">
  56. <!-- 第一步:基本信息 -->
  57. <div v-if="currentStep === 0" class="mx-auto w-560px">
  58. <BasicInfo
  59. v-model="formData"
  60. :categoryList="categoryList"
  61. :userList="userList"
  62. :deptList="deptList"
  63. ref="basicInfoRef"
  64. />
  65. </div>
  66. <!-- 第二步:表单设计 -->
  67. <div v-if="currentStep === 1" class="mx-auto w-560px">
  68. <FormDesign v-model="formData" :formList="formList" ref="formDesignRef" />
  69. </div>
  70. <!-- 第三步:流程设计 -->
  71. <ProcessDesign v-if="currentStep === 2" v-model="formData" ref="processDesignRef" />
  72. <!-- 第四步:更多设置 -->
  73. <div v-show="currentStep === 3" class="mx-auto w-700px">
  74. <ExtraSettings v-model="formData" ref="extraSettingsRef" />
  75. </div>
  76. </div>
  77. </div>
  78. </ContentWrap>
  79. </template>
  80. <script lang="ts" setup>
  81. import { useRoute, useRouter } from 'vue-router'
  82. import { useMessage } from '@/hooks/web/useMessage'
  83. import { useTagsViewStore } from '@/store/modules/tagsView'
  84. import { useUserStoreWithOut } from '@/store/modules/user'
  85. import * as ModelApi from '@/api/bpm/model'
  86. import * as FormApi from '@/api/bpm/form'
  87. import { CategoryApi, CategoryVO } from '@/api/bpm/category'
  88. import * as UserApi from '@/api/system/user'
  89. import * as DeptApi from '@/api/system/dept'
  90. import { useUserStoreWithOut } from '@/store/modules/user'
  91. import * as DefinitionApi from '@/api/bpm/definition'
  92. import { BpmModelFormType, BpmModelType, BpmAutoApproveType } from '@/utils/constants'
  93. import BasicInfo from './BasicInfo.vue'
  94. import FormDesign from './FormDesign.vue'
  95. import ProcessDesign from './ProcessDesign.vue'
  96. import ExtraSettings from './ExtraSettings.vue'
  97. import { useTagsView } from '@/hooks/web/useTagsView'
  98. const router = useRouter()
  99. const { delView } = useTagsViewStore() // 视图操作
  100. const tagsView = useTagsView()
  101. const route = useRoute()
  102. const message = useMessage()
  103. const userStore = useUserStoreWithOut()
  104. // 组件引用
  105. const basicInfoRef = ref()
  106. const formDesignRef = ref()
  107. const processDesignRef = ref()
  108. const extraSettingsRef = ref()
  109. /** 步骤校验函数 */
  110. const validateBasic = async () => {
  111. await basicInfoRef.value?.validate()
  112. }
  113. /** 表单设计校验 */
  114. const validateForm = async () => {
  115. await formDesignRef.value?.validate()
  116. }
  117. /** 流程设计校验 */
  118. const validateProcess = async () => {
  119. await processDesignRef.value?.validate()
  120. }
  121. const currentStep = ref(-1) // 步骤控制。-1 用于,一开始全部不展示等当前页面数据初始化完成
  122. const steps = [
  123. { title: '基本信息', validator: validateBasic },
  124. { title: '表单设计', validator: validateForm },
  125. { title: '流程设计', validator: validateProcess },
  126. { title: '更多设置', validator: null }
  127. ]
  128. // 表单数据
  129. const formData: any = ref({
  130. id: undefined,
  131. name: '',
  132. key: '',
  133. category: undefined,
  134. icon: undefined,
  135. description: '',
  136. type: BpmModelType.BPMN,
  137. formType: BpmModelFormType.NORMAL,
  138. formId: '',
  139. formCustomCreatePath: '',
  140. formCustomViewPath: '',
  141. visible: true,
  142. startUserType: undefined,
  143. startUserIds: [],
  144. startDeptIds: [],
  145. managerUserIds: [],
  146. allowCancelRunningProcess: true,
  147. processIdRule: {
  148. enable: false,
  149. prefix: '',
  150. infix: '',
  151. postfix: '',
  152. length: 5
  153. },
  154. autoApprovalType: BpmAutoApproveType.NONE,
  155. titleSetting: {
  156. enable: false,
  157. title: ''
  158. },
  159. summarySetting: {
  160. enable: false,
  161. summary: []
  162. }
  163. })
  164. // 流程数据
  165. const processData = ref<any>()
  166. provide('processData', processData)
  167. provide('modelData', formData)
  168. // 数据列表
  169. const formList = ref([])
  170. const categoryList = ref<CategoryVO[]>([])
  171. const userList = ref<UserApi.UserVO[]>([])
  172. const deptList = ref<DeptApi.DeptVO[]>([])
  173. /** 初始化数据 */
  174. const actionType = route.params.type as string
  175. const initData = async () => {
  176. if (actionType === 'definition') {
  177. // 情况一:流程定义场景(恢复)
  178. const definitionId = route.params.id as string
  179. const data = await DefinitionApi.getProcessDefinition(definitionId)
  180. // 将 definition => model,最终赋值
  181. data.type = data.modelType
  182. delete data.modelType
  183. data.id = data.modelId
  184. delete data.modelId
  185. if (data.simpleModel) {
  186. data.simpleModel = JSON.parse(data.simpleModel)
  187. }
  188. formData.value = data
  189. formData.value.startUserType = formData.value.startUserIds?.length > 0 ? 1 : formData.value?.startDeptIds?.length > 0 ? 2 : 0
  190. } else if (['update', 'copy'].includes(actionType)) {
  191. // 情况二:修改场景/复制场景
  192. const modelId = route.params.id as string
  193. formData.value = await ModelApi.getModel(modelId)
  194. formData.value.startUserType = formData.value.startUserIds?.length > 0 ? 1 : formData.value?.startDeptIds?.length > 0 ? 2 : 0
  195. // 复制场景
  196. if (route.params.type === 'copy') {
  197. delete formData.value.id
  198. formData.value.name += '副本'
  199. formData.value.key += '_copy'
  200. tagsView.setTitle('复制流程')
  201. }
  202. } else {
  203. // 情况三:新增场景
  204. formData.value.startUserType = 0 // 全体
  205. formData.value.managerUserIds.push(userStore.getUser.id)
  206. }
  207. // 获取表单列表
  208. formList.value = await FormApi.getFormSimpleList()
  209. // 获取分类列表
  210. categoryList.value = await CategoryApi.getCategorySimpleList()
  211. // 获取用户列表
  212. userList.value = await UserApi.getSimpleUserList()
  213. // 获取部门列表
  214. deptList.value = await DeptApi.getSimpleDeptList()
  215. // 最终,设置 currentStep 切换到第一步
  216. currentStep.value = 0
  217. // 兼容,以前未配置更多设置的流程
  218. extraSettingsRef.value.initData()
  219. }
  220. /** 根据类型切换流程数据 */
  221. watch(
  222. async () => formData.value.type,
  223. () => {
  224. if (formData.value.type === BpmModelType.BPMN) {
  225. processData.value = formData.value.bpmnXml
  226. } else if (formData.value.type === BpmModelType.SIMPLE) {
  227. processData.value = formData.value.simpleModel
  228. }
  229. console.log('加载流程数据', processData.value)
  230. },
  231. {
  232. immediate: true
  233. }
  234. )
  235. /** 校验所有步骤数据是否完整 */
  236. const validateAllSteps = async () => {
  237. try {
  238. // 基本信息校验
  239. try {
  240. await validateBasic()
  241. } catch (error) {
  242. currentStep.value = 0
  243. throw new Error('请完善基本信息')
  244. }
  245. // 表单设计校验
  246. try {
  247. await validateForm()
  248. } catch (error) {
  249. currentStep.value = 1
  250. throw new Error('请完善自定义表单信息')
  251. }
  252. // 流程设计校验
  253. // 表单设计校验
  254. try {
  255. await validateProcess()
  256. } catch (error) {
  257. currentStep.value = 2
  258. throw new Error('请设计流程')
  259. }
  260. return true
  261. } catch (error) {
  262. throw error
  263. }
  264. }
  265. /** 保存操作 */
  266. const handleSave = async () => {
  267. try {
  268. // 保存前校验所有步骤的数据
  269. await validateAllSteps()
  270. // 更新表单数据
  271. const modelData = {
  272. ...formData.value
  273. }
  274. if (actionType === 'definition') {
  275. // 情况一:流程定义场景(恢复)
  276. await ModelApi.updateModel(modelData)
  277. // 提示成功
  278. message.success('恢复成功,可点击【发布】按钮,进行发布模型')
  279. } else if (actionType === 'update') {
  280. // 修改场景
  281. await ModelApi.updateModel(modelData)
  282. // 提示成功
  283. message.success('修改成功,可点击【发布】按钮,进行发布模型')
  284. } else if (actionType === 'copy') {
  285. // 情况三:复制场景
  286. formData.value.id = await ModelApi.createModel(modelData)
  287. // 提示成功
  288. message.success('复制成功,可点击【发布】按钮,进行发布模型')
  289. } else {
  290. // 情况四:新增场景
  291. formData.value.id = await ModelApi.createModel(modelData)
  292. // 提示成功
  293. message.success('新建成功,可点击【发布】按钮,进行发布模型')
  294. }
  295. // 返回列表页(排除更新的情况)
  296. if (actionType !== 'update') {
  297. await router.push({ name: 'BpmModel' })
  298. }
  299. } catch (error: any) {
  300. console.error('保存失败:', error)
  301. message.warning(error.message || '请完善所有步骤的必填信息')
  302. }
  303. }
  304. /** 发布操作 */
  305. const handleDeploy = async () => {
  306. try {
  307. // 修改场景下直接发布,新增场景下需要先确认
  308. if (!formData.value.id) {
  309. await message.confirm('是否确认发布该流程?')
  310. }
  311. // 校验所有步骤
  312. await validateAllSteps()
  313. // 更新表单数据
  314. const modelData = {
  315. ...formData.value
  316. }
  317. // 先保存所有数据
  318. if (formData.value.id) {
  319. await ModelApi.updateModel(modelData)
  320. } else {
  321. const result = await ModelApi.createModel(modelData)
  322. formData.value.id = result.id
  323. }
  324. // 发布
  325. await ModelApi.deployModel(formData.value.id)
  326. message.success('发布成功')
  327. // 返回列表页
  328. await router.push({ name: 'BpmModel' })
  329. } catch (error: any) {
  330. console.error('发布失败:', error)
  331. message.warning(error.message || '发布失败')
  332. }
  333. }
  334. /** 步骤切换处理 */
  335. const handleStepClick = async (index: number) => {
  336. try {
  337. if (index !== 0) {
  338. await validateBasic()
  339. }
  340. if (index !== 1) {
  341. await validateForm()
  342. }
  343. if (index !== 2) {
  344. await validateProcess()
  345. }
  346. // 切换步骤
  347. currentStep.value = index
  348. // 如果切换到流程设计步骤,等待组件渲染完成后刷新设计器
  349. if (index === 2) {
  350. await nextTick()
  351. // 等待更长时间确保组件完全初始化
  352. await new Promise((resolve) => setTimeout(resolve, 200))
  353. if (processDesignRef.value?.refresh) {
  354. await processDesignRef.value.refresh()
  355. }
  356. }
  357. } catch (error) {
  358. console.error('步骤切换失败:', error)
  359. message.warning('请先完善当前步骤必填信息')
  360. }
  361. }
  362. /** 返回列表页 */
  363. const handleBack = () => {
  364. // 先删除当前页签
  365. delView(unref(router.currentRoute))
  366. // 跳转到列表页
  367. router.push({ name: 'BpmModel' })
  368. }
  369. /** 初始化 */
  370. onMounted(async () => {
  371. await initData()
  372. })
  373. // 添加组件卸载前的清理代码
  374. onBeforeUnmount(() => {
  375. // 清理所有的引用
  376. basicInfoRef.value = null
  377. formDesignRef.value = null
  378. processDesignRef.value = null
  379. })
  380. </script>
  381. <style lang="scss" scoped>
  382. .border-bottom {
  383. border-bottom: 1px solid #dcdfe6;
  384. }
  385. .text-primary {
  386. color: #3473ff;
  387. }
  388. .bg-primary {
  389. background-color: #3473ff;
  390. }
  391. .border-primary {
  392. border-color: #3473ff;
  393. }
  394. </style>