index.vue 12 KB

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