index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 :src="definition.icon" class="w-32px h-32px" />
  62. <el-text class="!ml-10px" size="large">{{ definition.name }}</el-text>
  63. </div>
  64. </template>
  65. </el-card>
  66. </el-tooltip>
  67. </div>
  68. </div>
  69. </el-scrollbar>
  70. </el-col>
  71. </el-row>
  72. <el-empty class="!py-200px" :image-size="200" description="没有找到搜索结果" v-else />
  73. </ContentWrap>
  74. </template>
  75. <!-- 第二步,填写表单,进行流程的提交 -->
  76. <ProcessDefinitionDetail
  77. v-else
  78. ref="processDefinitionDetailRef"
  79. :selectProcessDefinition="selectProcessDefinition"
  80. @cancel="selectProcessDefinition = undefined"
  81. />
  82. </template>
  83. <script lang="ts" setup>
  84. import * as DefinitionApi from '@/api/bpm/definition'
  85. import * as ProcessInstanceApi from '@/api/bpm/processInstance'
  86. import { CategoryApi } from '@/api/bpm/category'
  87. import ProcessDefinitionDetail from './ProcessDefinitionDetail.vue'
  88. import { groupBy } from 'lodash-es'
  89. defineOptions({ name: 'BpmProcessInstanceCreate' })
  90. const { proxy } = getCurrentInstance() as any
  91. const route = useRoute() // 路由
  92. const message = useMessage() // 消息
  93. const searchName = ref('') // 当前搜索关键字
  94. const processInstanceId: any = route.query.processInstanceId // 流程实例编号。场景:重新发起时
  95. const loading = ref(true) // 加载中
  96. const categoryList: any = ref([]) // 分类的列表
  97. const categoryActive: any = ref({}) // 选中的分类
  98. const processDefinitionList = ref([]) // 流程定义的列表
  99. /** 查询列表 */
  100. const getList = async () => {
  101. loading.value = true
  102. try {
  103. // 所有流程分类数据
  104. await getCategoryList()
  105. // 所有流程定义数据
  106. await getProcessDefinitionList()
  107. // 如果 processInstanceId 非空,说明是重新发起
  108. if (processInstanceId?.length > 0) {
  109. const processInstance = await ProcessInstanceApi.getProcessInstance(processInstanceId)
  110. if (!processInstance) {
  111. message.error('重新发起流程失败,原因:流程实例不存在')
  112. return
  113. }
  114. const processDefinition = processDefinitionList.value.find(
  115. (item: any) => item.key == processInstance.processDefinition?.key
  116. )
  117. if (!processDefinition) {
  118. message.error('重新发起流程失败,原因:流程定义不存在')
  119. return
  120. }
  121. await handleSelect(processDefinition, processInstance.formVariables)
  122. }
  123. } finally {
  124. loading.value = false
  125. }
  126. }
  127. /** 获取所有流程分类数据 */
  128. const getCategoryList = async () => {
  129. try {
  130. // 流程分类
  131. categoryList.value = await CategoryApi.getCategorySimpleList()
  132. } finally {
  133. }
  134. }
  135. /** 获取所有流程定义数据 */
  136. const getProcessDefinitionList = async () => {
  137. try {
  138. // 流程定义
  139. processDefinitionList.value = await DefinitionApi.getProcessDefinitionList({
  140. suspensionState: 1
  141. })
  142. // 初始化过滤列表为全部流程定义
  143. filteredProcessDefinitionList.value = processDefinitionList.value
  144. // 在获取完所有数据后,设置第一个有效分类为激活状态
  145. if (availableCategories.value.length > 0 && !categoryActive.value?.code) {
  146. categoryActive.value = availableCategories.value[0]
  147. }
  148. } finally {
  149. }
  150. }
  151. /** 搜索流程 */
  152. const filteredProcessDefinitionList = ref([]) // 用于存储搜索过滤后的流程定义
  153. const handleQuery = () => {
  154. if (searchName.value.trim()) {
  155. // 如果有搜索关键字,进行过滤
  156. filteredProcessDefinitionList.value = processDefinitionList.value.filter(
  157. (definition: any) => definition.name.toLowerCase().includes(searchName.value.toLowerCase()) // 假设搜索依据是流程定义的名称
  158. )
  159. } else {
  160. // 如果没有搜索关键字,恢复所有数据
  161. filteredProcessDefinitionList.value = processDefinitionList.value
  162. }
  163. }
  164. /** 流程定义的分组 */
  165. const processDefinitionGroup: any = computed(() => {
  166. if (!processDefinitionList.value?.length) {
  167. return {}
  168. }
  169. const grouped = groupBy(filteredProcessDefinitionList.value, 'category')
  170. // 按照 categoryList 的顺序重新组织数据
  171. const orderedGroup = {}
  172. categoryList.value.forEach((category: any) => {
  173. if (grouped[category.code]) {
  174. orderedGroup[category.code] = grouped[category.code]
  175. }
  176. })
  177. return orderedGroup
  178. })
  179. /** 左侧分类切换 */
  180. const handleCategoryClick = (category: any) => {
  181. categoryActive.value = category
  182. const categoryRef = proxy.$refs[`category-${category.code}`] // 获取点击分类对应的 DOM 元素
  183. if (categoryRef?.length) {
  184. const scrollWrapper = proxy.$refs.scrollWrapper // 获取右侧滚动容器
  185. const categoryOffsetTop = categoryRef[0].offsetTop
  186. // 滚动到对应位置
  187. scrollWrapper.scrollTo({ top: categoryOffsetTop, behavior: 'smooth' })
  188. }
  189. }
  190. /** 通过分类 code 获取对应的名称 */
  191. const getCategoryName = (categoryCode: string) => {
  192. return categoryList.value?.find((ctg: any) => ctg.code === categoryCode)?.name
  193. }
  194. // ========== 表单相关 ==========
  195. const selectProcessDefinition = ref() // 选择的流程定义
  196. const processDefinitionDetailRef = ref()
  197. /** 处理选择流程的按钮操作 **/
  198. const handleSelect = async (row, formVariables?) => {
  199. // 设置选择的流程
  200. selectProcessDefinition.value = row
  201. // 初始化流程定义详情
  202. await nextTick()
  203. processDefinitionDetailRef.value?.initProcessInfo(row, formVariables)
  204. }
  205. /** 处理滚动事件 */
  206. const handleScroll = (e) => {
  207. // 直接使用事件对象获取滚动位置
  208. const scrollTop = e.scrollTop
  209. // 获取所有分类区域的位置信息
  210. const categoryPositions = categoryList.value
  211. .map((category) => {
  212. const categoryRef = proxy.$refs[`category-${category.code}`]
  213. if (categoryRef?.[0]) {
  214. return {
  215. code: category.code,
  216. offsetTop: categoryRef[0].offsetTop,
  217. height: categoryRef[0].offsetHeight
  218. }
  219. }
  220. return null
  221. })
  222. .filter(Boolean)
  223. // 查找当前滚动位置对应的分类
  224. let currentCategory = categoryPositions[0]
  225. for (const position of categoryPositions) {
  226. // 为了更好的用户体验,可以添加一个缓冲区域(比如 50px)
  227. if (scrollTop >= position.offsetTop - 50) {
  228. currentCategory = position
  229. } else {
  230. break
  231. }
  232. }
  233. // 更新当前 active 的分类
  234. if (currentCategory && categoryActive.value.code !== currentCategory.code) {
  235. categoryActive.value = categoryList.value.find((c) => c.code === currentCategory.code)
  236. }
  237. }
  238. /** 初始化 */
  239. onMounted(() => {
  240. getList()
  241. })
  242. /** 过滤出有流程的分类列表 */
  243. const availableCategories = computed(() => {
  244. if (!categoryList.value?.length || !processDefinitionGroup.value) {
  245. return []
  246. }
  247. // 获取所有有流程的分类代码
  248. const availableCategoryCodes = Object.keys(processDefinitionGroup.value)
  249. // 过滤出有流程的分类
  250. return categoryList.value.filter(category =>
  251. availableCategoryCodes.includes(category.code)
  252. )
  253. })
  254. </script>
  255. <style lang="scss" scoped>
  256. .process-definition-container::before {
  257. content: '';
  258. border-left: 1px solid #e6e6e6;
  259. position: absolute;
  260. left: 20.8%;
  261. height: 100%;
  262. }
  263. :deep() {
  264. .definition-item-card {
  265. .el-card__body {
  266. padding: 14px;
  267. }
  268. }
  269. }
  270. </style>