index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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="route.params.id" type="success" @click="handleDeploy">发 布</el-button>
  46. <el-button type="primary" @click="handleSave">保 存</el-button>
  47. </div>
  48. </div>
  49. <!-- 主体内容 -->
  50. <div class="mt-50px">
  51. <!-- 第一步:基本信息 -->
  52. <div v-if="currentStep === 0" class="mx-auto w-560px">
  53. <BasicInfo
  54. v-model="formData"
  55. :categoryList="categoryList"
  56. :userList="userList"
  57. ref="basicInfoRef"
  58. />
  59. </div>
  60. <!-- 第二步:表单设计 -->
  61. <div v-if="currentStep === 1" class="mx-auto w-560px">
  62. <FormDesign v-model="formData" :formList="formList" ref="formDesignRef" />
  63. </div>
  64. <!-- 第三步:流程设计 -->
  65. <ProcessDesign
  66. v-if="currentStep === 2"
  67. v-model="formData"
  68. ref="processDesignRef"
  69. @success="handleDesignSuccess"
  70. />
  71. </div>
  72. </div>
  73. </ContentWrap>
  74. </template>
  75. <script lang="ts" setup>
  76. import { useRouter, useRoute } from 'vue-router'
  77. import { useMessage } from '@/hooks/web/useMessage'
  78. import * as ModelApi from '@/api/bpm/model'
  79. import * as FormApi from '@/api/bpm/form'
  80. import { CategoryApi } from '@/api/bpm/category'
  81. import * as UserApi from '@/api/system/user'
  82. import { useUserStoreWithOut } from '@/store/modules/user'
  83. import { BpmModelType, BpmModelFormType } from '@/utils/constants'
  84. import BasicInfo from './BasicInfo.vue'
  85. import FormDesign from './FormDesign.vue'
  86. import ProcessDesign from './ProcessDesign.vue'
  87. import { useTagsViewStore } from '@/store/modules/tagsView'
  88. const router = useRouter()
  89. const { delView } = useTagsViewStore() // 视图操作
  90. const route = useRoute()
  91. const message = useMessage()
  92. const userStore = useUserStoreWithOut()
  93. // 组件引用
  94. const basicInfoRef = ref()
  95. const formDesignRef = ref()
  96. const processDesignRef = ref()
  97. /** 步骤校验函数 */
  98. const validateBasic = async () => {
  99. await basicInfoRef.value?.validate()
  100. }
  101. const validateForm = async () => {
  102. await formDesignRef.value?.validate()
  103. }
  104. const validateProcess = async () => {
  105. await processDesignRef.value?.validate()
  106. }
  107. // 步骤控制
  108. const currentStep = ref(0)
  109. const steps = [
  110. { title: '基本信息', validator: validateBasic },
  111. { title: '表单设计', validator: validateForm },
  112. { title: '流程设计', validator: validateProcess }
  113. ]
  114. // 表单数据
  115. const formData: any = ref({
  116. id: undefined,
  117. name: '',
  118. key: '',
  119. category: undefined,
  120. icon: undefined,
  121. description: '',
  122. type: BpmModelType.BPMN,
  123. formType: BpmModelFormType.NORMAL,
  124. formId: '',
  125. formCustomCreatePath: '',
  126. formCustomViewPath: '',
  127. visible: true,
  128. startUserType: undefined,
  129. managerUserType: undefined,
  130. startUserIds: [],
  131. managerUserIds: []
  132. })
  133. // 数据列表
  134. const formList = ref([])
  135. const categoryList = ref([])
  136. const userList = ref<UserApi.UserVO[]>([])
  137. /** 初始化数据 */
  138. const initData = async () => {
  139. const modelId = route.params.id as string
  140. if (modelId) {
  141. // 修改场景
  142. formData.value = await ModelApi.getModel(modelId)
  143. } else {
  144. // 新增场景
  145. formData.value.managerUserIds.push(userStore.getUser.id)
  146. }
  147. // 获取表单列表
  148. formList.value = await FormApi.getFormSimpleList()
  149. // 获取分类列表
  150. categoryList.value = await CategoryApi.getCategorySimpleList()
  151. // 获取用户列表
  152. userList.value = await UserApi.getSimpleUserList()
  153. }
  154. /** 校验所有步骤数据是否完整 */
  155. const validateAllSteps = async () => {
  156. try {
  157. // 基本信息校验
  158. await basicInfoRef.value?.validate()
  159. if (!formData.value.key || !formData.value.name || !formData.value.category) {
  160. currentStep.value = 0
  161. throw new Error('请完善基本信息')
  162. }
  163. // 表单设计校验
  164. await formDesignRef.value?.validate()
  165. if (formData.value.formType === 10 && !formData.value.formId) {
  166. currentStep.value = 1
  167. throw new Error('请选择流程表单')
  168. }
  169. if (
  170. formData.value.formType === 20 &&
  171. (!formData.value.formCustomCreatePath || !formData.value.formCustomViewPath)
  172. ) {
  173. currentStep.value = 1
  174. throw new Error('请完善自定义表单信息')
  175. }
  176. // 流程设计校验
  177. await processDesignRef.value?.validate()
  178. const bpmnXml = processDesignRef.value?.getXmlString()
  179. if (!bpmnXml) {
  180. currentStep.value = 2
  181. throw new Error('请设计流程')
  182. }
  183. return true
  184. } catch (error) {
  185. throw error
  186. }
  187. }
  188. /** 保存操作 */
  189. const handleSave = async () => {
  190. try {
  191. // 保存前校验所有步骤的数据
  192. await validateAllSteps()
  193. // 获取最新的流程设计数据
  194. const bpmnXml = await processDesignRef.value?.getXmlString()
  195. if (!bpmnXml) {
  196. throw new Error('获取流程数据失败')
  197. }
  198. // 更新表单数据
  199. formData.value = {
  200. ...formData.value,
  201. bpmnXml: bpmnXml
  202. }
  203. if (formData.value.id) {
  204. // 修改场景
  205. await ModelApi.updateModel(formData.value)
  206. message.success('修改成功')
  207. // 询问是否发布流程
  208. try {
  209. await message.confirm('修改流程成功,是否发布流程?')
  210. // 用户点击确认,执行发布
  211. await handleDeploy()
  212. } catch {
  213. // 用户点击取消,停留在当前页面
  214. }
  215. } else {
  216. // 新增场景
  217. const result = await ModelApi.createModel(formData.value)
  218. formData.value.id = result
  219. message.success('新增成功')
  220. try {
  221. await message.confirm('创建流程成功,是否继续编辑?')
  222. // 用户点击继续编辑,跳转到编辑页面
  223. await nextTick()
  224. // 先删除当前页签
  225. delView(unref(router.currentRoute))
  226. // 跳转到编辑页面
  227. await router.push({
  228. name: 'BpmModelUpdate',
  229. params: { id: formData.value.id }
  230. })
  231. } catch {
  232. // 先删除当前页签
  233. delView(unref(router.currentRoute))
  234. // 用户点击返回列表
  235. await router.push({ name: 'BpmModel' })
  236. }
  237. }
  238. } catch (error: any) {
  239. console.error('保存失败:', error)
  240. message.warning(error.message || '请完善所有步骤的必填信息')
  241. }
  242. }
  243. /** 发布操作 */
  244. const handleDeploy = async () => {
  245. try {
  246. // 修改场景下直接发布,新增场景下需要先确认
  247. if (!formData.value.id) {
  248. await message.confirm('是否确认发布该流程?')
  249. }
  250. // 校验所有步骤
  251. await validateAllSteps()
  252. // 获取最新的流程设计数据
  253. const bpmnXml = await processDesignRef.value?.getXmlString()
  254. if (!bpmnXml) {
  255. throw new Error('获取流程数据失败')
  256. }
  257. // 更新表单数据
  258. formData.value = {
  259. ...formData.value,
  260. bpmnXml: bpmnXml
  261. }
  262. // 先保存所有数据
  263. if (formData.value.id) {
  264. await ModelApi.updateModel(formData.value)
  265. } else {
  266. const result = await ModelApi.createModel(formData.value)
  267. formData.value.id = result.id
  268. }
  269. // 发布
  270. await ModelApi.deployModel(formData.value.id)
  271. message.success('发布成功')
  272. // 返回列表页
  273. router.push({ name: 'BpmModel' })
  274. } catch (error: any) {
  275. console.error('发布失败:', error)
  276. message.warning(error.message || '发布失败')
  277. }
  278. }
  279. /** 步骤切换处理 */
  280. const handleStepClick = async (index: number) => {
  281. // 如果是切换到第三步(流程设计),需要校验key和name
  282. if (index === 2) {
  283. if (!formData.value.key || !formData.value.name) {
  284. message.warning('请先填写流程标识和流程名称')
  285. return
  286. }
  287. }
  288. // 只有在向后切换时才进行校验
  289. if (index > currentStep.value) {
  290. try {
  291. if (typeof steps[currentStep.value].validator === 'function') {
  292. await steps[currentStep.value].validator()
  293. }
  294. currentStep.value = index
  295. } catch (error) {
  296. message.warning('请先完善当前步骤必填信息')
  297. }
  298. } else {
  299. // 向前切换时直接切换
  300. currentStep.value = index
  301. }
  302. }
  303. /** 处理设计器保存成功 */
  304. const handleDesignSuccess = (bpmnXml?: string) => {
  305. if (bpmnXml) {
  306. formData.value.bpmnXml = bpmnXml
  307. }
  308. }
  309. /** 返回列表页 */
  310. const handleBack = () => {
  311. // 先删除当前页签
  312. delView(unref(router.currentRoute))
  313. // 跳转到列表页
  314. router.push({ name: 'BpmModel' })
  315. }
  316. /** 初始化 */
  317. onMounted(async () => {
  318. await initData()
  319. })
  320. // 添加组件卸载前的清理代码
  321. onBeforeUnmount(() => {
  322. // 清理所有的引用
  323. basicInfoRef.value = null
  324. formDesignRef.value = null
  325. processDesignRef.value = null
  326. })
  327. </script>
  328. <style lang="scss" scoped>
  329. .border-bottom {
  330. border-bottom: 1px solid #dcdfe6;
  331. }
  332. .text-primary {
  333. color: #3473ff;
  334. }
  335. .bg-primary {
  336. background-color: #3473ff;
  337. }
  338. .border-primary {
  339. border-color: #3473ff;
  340. }
  341. </style>