index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <!-- 第一步,通过流程定义的列表,选择对应的流程 -->
  3. <template v-if="!selectProcessDefinition">
  4. <el-input
  5. v-model="searchName"
  6. class="!w-50% mb-15px"
  7. placeholder="请输入流程名称"
  8. clearable
  9. @input="handleQuery"
  10. @clear="handleQuery"
  11. >
  12. <template #prefix>
  13. <Icon icon="ep:search" />
  14. </template>
  15. </el-input>
  16. <ContentWrap
  17. :class="{ 'process-definition-container': filteredProcessDefinitionList?.length }"
  18. class="position-relative pb-20px h-700px"
  19. v-loading="loading"
  20. >
  21. <el-row v-if="filteredProcessDefinitionList?.length" :gutter="20" class="!flex-nowrap">
  22. <el-col :span="5">
  23. <div class="flex flex-col">
  24. <div
  25. v-for="category in availableCategories"
  26. :key="category.code"
  27. class="flex items-center p-10px cursor-pointer text-14px rounded-md"
  28. :class="categoryActive.code === category.code ? 'text-#3e7bff bg-#e8eeff' : ''"
  29. @click="handleCategoryClick(category)"
  30. >
  31. {{ category.name }}
  32. </div>
  33. </div>
  34. </el-col>
  35. <el-col :span="19">
  36. <el-scrollbar ref="scrollWrapper" height="700" @scroll="handleScroll">
  37. <div
  38. class="mb-20px pl-10px"
  39. v-for="(definitions, categoryCode) in processDefinitionGroup"
  40. :key="categoryCode"
  41. :ref="`category-${categoryCode}`"
  42. >
  43. <h3 class="text-18px font-bold mb-10px mt-5px">
  44. {{ getCategoryName(categoryCode as any) }}
  45. </h3>
  46. <div class="grid grid-cols-3 gap3">
  47. <el-tooltip
  48. v-for="definition in definitions"
  49. :key="definition.id"
  50. :content="definition.description"
  51. :disabled="!definition.description || definition.description.trim().length === 0"
  52. placement="top"
  53. >
  54. <el-card
  55. shadow="hover"
  56. class="cursor-pointer definition-item-card"
  57. @click="handleSelect(definition)"
  58. >
  59. <template #default>
  60. <div class="flex">
  61. <el-image
  62. v-if="definition.icon"
  63. :src="definition.icon"
  64. class="w-32px h-32px"
  65. />
  66. <div v-else class="flow-icon">
  67. <span style="font-size: 12px; color: #fff">
  68. {{ subString(definition.name, 0, 2) }}
  69. </span>
  70. </div>
  71. <el-text class="!ml-10px" size="large">{{ definition.name }}</el-text>
  72. </div>
  73. </template>
  74. </el-card>
  75. </el-tooltip>
  76. </div>
  77. </div>
  78. </el-scrollbar>
  79. </el-col>
  80. </el-row>
  81. <el-empty class="!py-200px" :image-size="200" description="没有找到搜索结果" v-else />
  82. </ContentWrap>
  83. </template>
  84. <!-- 第二步,填写表单,进行流程的提交 -->
  85. <ProcessDefinitionDetail
  86. v-else
  87. ref="processDefinitionDetailRef"
  88. :selectProcessDefinition="selectProcessDefinition"
  89. @cancel="selectProcessDefinition = undefined"
  90. />
  91. </template>
  92. <script lang="ts" setup>
  93. import * as DefinitionApi from '@/api/bpm/definition'
  94. import * as ProcessInstanceApi from '@/api/bpm/processInstance'
  95. import { CategoryApi, CategoryVO } from '@/api/bpm/category'
  96. import ProcessDefinitionDetail from './ProcessDefinitionDetail.vue'
  97. import { groupBy } from 'lodash-es'
  98. import { subString } from '@/utils/index'
  99. defineOptions({ name: 'BpmProcessInstanceCreate' })
  100. const { proxy } = getCurrentInstance() as any
  101. const route = useRoute() // 路由
  102. const message = useMessage() // 消息
  103. const searchName = ref('') // 当前搜索关键字
  104. const processInstanceId: any = route.query.processInstanceId // 流程实例编号。场景:重新发起时
  105. const loading = ref(true) // 加载中
  106. const categoryList: any = ref([]) // 分类的列表
  107. const categoryActive: any = ref({}) // 选中的分类
  108. const processDefinitionList = ref([]) // 流程定义的列表
  109. /** 查询列表 */
  110. const getList = async () => {
  111. loading.value = true
  112. try {
  113. // 所有流程分类数据
  114. await getCategoryList()
  115. // 所有流程定义数据
  116. await getProcessDefinitionList()
  117. // 如果 processInstanceId 非空,说明是重新发起
  118. if (processInstanceId?.length > 0) {
  119. const processInstance = await ProcessInstanceApi.getProcessInstance(processInstanceId)
  120. if (!processInstance) {
  121. message.error('重新发起流程失败,原因:流程实例不存在')
  122. return
  123. }
  124. const processDefinition = processDefinitionList.value.find(
  125. (item: any) => item.key == processInstance.processDefinition?.key
  126. )
  127. if (!processDefinition) {
  128. message.error('重新发起流程失败,原因:流程定义不存在')
  129. return
  130. }
  131. await handleSelect(processDefinition, processInstance.formVariables)
  132. }
  133. } finally {
  134. loading.value = false
  135. }
  136. }
  137. /** 获取所有流程分类数据 */
  138. const getCategoryList = async () => {
  139. try {
  140. // 流程分类
  141. categoryList.value = await CategoryApi.getCategorySimpleList()
  142. } finally {
  143. }
  144. }
  145. /** 获取所有流程定义数据 */
  146. const getProcessDefinitionList = async () => {
  147. try {
  148. // 流程定义
  149. processDefinitionList.value = await DefinitionApi.getProcessDefinitionList({
  150. suspensionState: 1
  151. })
  152. // 初始化过滤列表为全部流程定义
  153. filteredProcessDefinitionList.value = processDefinitionList.value
  154. // 在获取完所有数据后,设置第一个有效分类为激活状态
  155. if (availableCategories.value.length > 0 && !categoryActive.value?.code) {
  156. categoryActive.value = availableCategories.value[0]
  157. }
  158. } finally {
  159. }
  160. }
  161. /** 搜索流程 */
  162. const filteredProcessDefinitionList = ref([]) // 用于存储搜索过滤后的流程定义
  163. const handleQuery = () => {
  164. if (searchName.value.trim()) {
  165. // 如果有搜索关键字,进行过滤
  166. filteredProcessDefinitionList.value = processDefinitionList.value.filter(
  167. (definition: any) => definition.name.toLowerCase().includes(searchName.value.toLowerCase()) // 假设搜索依据是流程定义的名称
  168. )
  169. } else {
  170. // 如果没有搜索关键字,恢复所有数据
  171. filteredProcessDefinitionList.value = processDefinitionList.value
  172. }
  173. }
  174. /** 流程定义的分组 */
  175. const processDefinitionGroup: any = computed(() => {
  176. if (!processDefinitionList.value?.length) {
  177. return {}
  178. }
  179. const grouped = groupBy(filteredProcessDefinitionList.value, 'category')
  180. // 按照 categoryList 的顺序重新组织数据
  181. const orderedGroup = {}
  182. categoryList.value.forEach((category: any) => {
  183. if (grouped[category.code]) {
  184. orderedGroup[category.code] = grouped[category.code]
  185. }
  186. })
  187. return orderedGroup
  188. })
  189. /** 左侧分类切换 */
  190. const handleCategoryClick = (category: any) => {
  191. categoryActive.value = category
  192. const categoryRef = proxy.$refs[`category-${category.code}`] // 获取点击分类对应的 DOM 元素
  193. if (categoryRef?.length) {
  194. const scrollWrapper = proxy.$refs.scrollWrapper // 获取右侧滚动容器
  195. const categoryOffsetTop = categoryRef[0].offsetTop
  196. // 滚动到对应位置
  197. scrollWrapper.scrollTo({ top: categoryOffsetTop, behavior: 'smooth' })
  198. }
  199. }
  200. /** 通过分类 code 获取对应的名称 */
  201. const getCategoryName = (categoryCode: string) => {
  202. return categoryList.value?.find((ctg: any) => ctg.code === categoryCode)?.name
  203. }
  204. // ========== 表单相关 ==========
  205. const selectProcessDefinition = ref() // 选择的流程定义
  206. const processDefinitionDetailRef = ref()
  207. /** 处理选择流程的按钮操作 **/
  208. const handleSelect = async (row, formVariables?) => {
  209. // 设置选择的流程
  210. selectProcessDefinition.value = row
  211. // 初始化流程定义详情
  212. await nextTick()
  213. processDefinitionDetailRef.value?.initProcessInfo(row, formVariables)
  214. }
  215. /** 处理滚动事件,和左侧分类联动 */
  216. const handleScroll = (e: any) => {
  217. // 直接使用事件对象获取滚动位置
  218. const scrollTop = e.scrollTop
  219. // 获取所有分类区域的位置信息
  220. const categoryPositions = categoryList.value
  221. .map((category: CategoryVO) => {
  222. const categoryRef = proxy.$refs[`category-${category.code}`]
  223. if (categoryRef?.[0]) {
  224. return {
  225. code: category.code,
  226. offsetTop: categoryRef[0].offsetTop,
  227. height: categoryRef[0].offsetHeight
  228. }
  229. }
  230. return null
  231. })
  232. .filter(Boolean)
  233. // 查找当前滚动位置对应的分类
  234. let currentCategory = categoryPositions[0]
  235. for (const position of categoryPositions) {
  236. // 为了更好的用户体验,可以添加一个缓冲区域(比如 50px)
  237. if (scrollTop >= position.offsetTop - 50) {
  238. currentCategory = position
  239. } else {
  240. break
  241. }
  242. }
  243. // 更新当前 active 的分类
  244. if (currentCategory && categoryActive.value.code !== currentCategory.code) {
  245. categoryActive.value = categoryList.value.find(
  246. (c: CategoryVO) => c.code === currentCategory.code
  247. )
  248. }
  249. }
  250. /** 过滤出有流程的分类列表。目的:只展示有流程的分类 */
  251. const availableCategories = computed(() => {
  252. if (!categoryList.value?.length || !processDefinitionGroup.value) {
  253. return []
  254. }
  255. // 获取所有有流程的分类代码
  256. const availableCategoryCodes = Object.keys(processDefinitionGroup.value)
  257. // 过滤出有流程的分类
  258. return categoryList.value.filter((category: CategoryVO) =>
  259. availableCategoryCodes.includes(category.code)
  260. )
  261. })
  262. /** 初始化 */
  263. onMounted(() => {
  264. getList()
  265. })
  266. </script>
  267. <style lang="scss" scoped>
  268. .flow-icon {
  269. display: flex;
  270. width: 32px;
  271. height: 32px;
  272. margin-right: 10px;
  273. background-color: var(--el-color-primary);
  274. border-radius: 0.25rem;
  275. align-items: center;
  276. justify-content: center;
  277. }
  278. .process-definition-container::before {
  279. position: absolute;
  280. left: 20.8%;
  281. height: 100%;
  282. border-left: 1px solid #e6e6e6;
  283. content: '';
  284. }
  285. :deep() {
  286. .definition-item-card {
  287. .el-card__body {
  288. padding: 14px;
  289. }
  290. }
  291. }
  292. </style>