index.vue 11 KB

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