index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. sliceName(definition.name)
  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. defineOptions({ name: 'BpmProcessInstanceCreate' })
  99. const { proxy } = getCurrentInstance() as any
  100. const route = useRoute() // 路由
  101. const message = useMessage() // 消息
  102. const searchName = ref('') // 当前搜索关键字
  103. const processInstanceId: any = route.query.processInstanceId // 流程实例编号。场景:重新发起时
  104. const loading = ref(true) // 加载中
  105. const categoryList: any = ref([]) // 分类的列表
  106. const categoryActive: any = ref({}) // 选中的分类
  107. const processDefinitionList = ref([]) // 流程定义的列表
  108. /** 查询列表 */
  109. const getList = async () => {
  110. loading.value = true
  111. try {
  112. // 所有流程分类数据
  113. await getCategoryList()
  114. // 所有流程定义数据
  115. await getProcessDefinitionList()
  116. // 如果 processInstanceId 非空,说明是重新发起
  117. if (processInstanceId?.length > 0) {
  118. const processInstance = await ProcessInstanceApi.getProcessInstance(processInstanceId)
  119. if (!processInstance) {
  120. message.error('重新发起流程失败,原因:流程实例不存在')
  121. return
  122. }
  123. const processDefinition = processDefinitionList.value.find(
  124. (item: any) => item.key == processInstance.processDefinition?.key
  125. )
  126. if (!processDefinition) {
  127. message.error('重新发起流程失败,原因:流程定义不存在')
  128. return
  129. }
  130. await handleSelect(processDefinition, processInstance.formVariables)
  131. }
  132. } finally {
  133. loading.value = false
  134. }
  135. }
  136. /** 获取所有流程分类数据 */
  137. const getCategoryList = async () => {
  138. try {
  139. // 流程分类
  140. categoryList.value = await CategoryApi.getCategorySimpleList()
  141. } finally {
  142. }
  143. }
  144. /** 获取所有流程定义数据 */
  145. const getProcessDefinitionList = async () => {
  146. try {
  147. // 流程定义
  148. processDefinitionList.value = await DefinitionApi.getProcessDefinitionList({
  149. suspensionState: 1
  150. })
  151. // 初始化过滤列表为全部流程定义
  152. filteredProcessDefinitionList.value = processDefinitionList.value
  153. // 在获取完所有数据后,设置第一个有效分类为激活状态
  154. if (availableCategories.value.length > 0 && !categoryActive.value?.code) {
  155. categoryActive.value = availableCategories.value[0]
  156. }
  157. } finally {
  158. }
  159. }
  160. /** 搜索流程 */
  161. const filteredProcessDefinitionList = ref([]) // 用于存储搜索过滤后的流程定义
  162. const handleQuery = () => {
  163. if (searchName.value.trim()) {
  164. // 如果有搜索关键字,进行过滤
  165. filteredProcessDefinitionList.value = processDefinitionList.value.filter(
  166. (definition: any) => definition.name.toLowerCase().includes(searchName.value.toLowerCase()) // 假设搜索依据是流程定义的名称
  167. )
  168. } else {
  169. // 如果没有搜索关键字,恢复所有数据
  170. filteredProcessDefinitionList.value = processDefinitionList.value
  171. }
  172. }
  173. /** 流程定义的分组 */
  174. const processDefinitionGroup: any = computed(() => {
  175. if (!processDefinitionList.value?.length) {
  176. return {}
  177. }
  178. const grouped = groupBy(filteredProcessDefinitionList.value, 'category')
  179. // 按照 categoryList 的顺序重新组织数据
  180. const orderedGroup = {}
  181. categoryList.value.forEach((category: any) => {
  182. if (grouped[category.code]) {
  183. orderedGroup[category.code] = grouped[category.code]
  184. }
  185. })
  186. return orderedGroup
  187. })
  188. /** 左侧分类切换 */
  189. const handleCategoryClick = (category: any) => {
  190. categoryActive.value = category
  191. const categoryRef = proxy.$refs[`category-${category.code}`] // 获取点击分类对应的 DOM 元素
  192. if (categoryRef?.length) {
  193. const scrollWrapper = proxy.$refs.scrollWrapper // 获取右侧滚动容器
  194. const categoryOffsetTop = categoryRef[0].offsetTop
  195. // 滚动到对应位置
  196. scrollWrapper.scrollTo({ top: categoryOffsetTop, behavior: 'smooth' })
  197. }
  198. }
  199. /** 通过分类 code 获取对应的名称 */
  200. const getCategoryName = (categoryCode: string) => {
  201. return categoryList.value?.find((ctg: any) => ctg.code === categoryCode)?.name
  202. }
  203. // ========== 表单相关 ==========
  204. const selectProcessDefinition = ref() // 选择的流程定义
  205. const processDefinitionDetailRef = ref()
  206. /** 处理选择流程的按钮操作 **/
  207. const handleSelect = async (row, formVariables?) => {
  208. // 设置选择的流程
  209. selectProcessDefinition.value = row
  210. // 初始化流程定义详情
  211. await nextTick()
  212. processDefinitionDetailRef.value?.initProcessInfo(row, formVariables)
  213. }
  214. /** 处理滚动事件,和左侧分类联动 */
  215. const handleScroll = (e: any) => {
  216. // 直接使用事件对象获取滚动位置
  217. const scrollTop = e.scrollTop
  218. // 获取所有分类区域的位置信息
  219. const categoryPositions = categoryList.value
  220. .map((category: CategoryVO) => {
  221. const categoryRef = proxy.$refs[`category-${category.code}`]
  222. if (categoryRef?.[0]) {
  223. return {
  224. code: category.code,
  225. offsetTop: categoryRef[0].offsetTop,
  226. height: categoryRef[0].offsetHeight
  227. }
  228. }
  229. return null
  230. })
  231. .filter(Boolean)
  232. // 查找当前滚动位置对应的分类
  233. let currentCategory = categoryPositions[0]
  234. for (const position of categoryPositions) {
  235. // 为了更好的用户体验,可以添加一个缓冲区域(比如 50px)
  236. if (scrollTop >= position.offsetTop - 50) {
  237. currentCategory = position
  238. } else {
  239. break
  240. }
  241. }
  242. // 更新当前 active 的分类
  243. if (currentCategory && categoryActive.value.code !== currentCategory.code) {
  244. categoryActive.value = categoryList.value.find(
  245. (c: CategoryVO) => c.code === currentCategory.code
  246. )
  247. }
  248. }
  249. /** 过滤出有流程的分类列表。目的:只展示有流程的分类 */
  250. const availableCategories = computed(() => {
  251. if (!categoryList.value?.length || !processDefinitionGroup.value) {
  252. return []
  253. }
  254. // 获取所有有流程的分类代码
  255. const availableCategoryCodes = Object.keys(processDefinitionGroup.value)
  256. // 过滤出有流程的分类
  257. return categoryList.value.filter((category: CategoryVO) =>
  258. availableCategoryCodes.includes(category.code)
  259. )
  260. })
  261. // 处理显示的名称
  262. const sliceName = (name: string) => {
  263. if (name.length > 2) {
  264. return name.slice(0, 2)
  265. }
  266. return name
  267. }
  268. /** 初始化 */
  269. onMounted(() => {
  270. getList()
  271. })
  272. </script>
  273. <style lang="scss" scoped>
  274. .flow-icon {
  275. display: flex;
  276. width: 32px;
  277. height: 32px;
  278. margin-right: 10px;
  279. background-color: var(--el-color-primary);
  280. border-radius: 0.25rem;
  281. align-items: center;
  282. justify-content: center;
  283. }
  284. .process-definition-container::before {
  285. position: absolute;
  286. left: 20.8%;
  287. height: 100%;
  288. border-left: 1px solid #e6e6e6;
  289. content: '';
  290. }
  291. :deep() {
  292. .definition-item-card {
  293. .el-card__body {
  294. padding: 14px;
  295. }
  296. }
  297. }
  298. </style>