index.vue 10 KB

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