index.vue 10 KB

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