CategoryDraggableModel.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <template>
  2. <div class="flex items-center h-50px" v-memo="[categoryInfo.name, isCategorySorting]">
  3. <!-- 头部:分类名 -->
  4. <div class="flex items-center">
  5. <el-tooltip content="拖动排序" v-if="isCategorySorting">
  6. <Icon
  7. :size="22"
  8. icon="ic:round-drag-indicator"
  9. class="ml-10px category-drag-icon cursor-move text-#8a909c"
  10. />
  11. </el-tooltip>
  12. <h3 class="ml-20px mr-8px text-18px">{{ categoryInfo.name }}</h3>
  13. <div class="color-gray-600 text-16px"> ({{ categoryInfo.modelList?.length || 0 }}) </div>
  14. </div>
  15. <!-- 头部:操作 -->
  16. <div class="flex-1 flex" v-show="!isCategorySorting">
  17. <div
  18. v-if="categoryInfo.modelList.length > 0"
  19. class="ml-20px flex items-center"
  20. :class="[
  21. 'transition-transform duration-300 cursor-pointer',
  22. isExpand ? 'rotate-180' : 'rotate-0'
  23. ]"
  24. @click="isExpand = !isExpand"
  25. >
  26. <Icon icon="ep:arrow-down-bold" color="#999" />
  27. </div>
  28. <div class="ml-auto flex items-center" :class="isModelSorting ? 'mr-15px' : 'mr-45px'">
  29. <template v-if="!isModelSorting">
  30. <el-button
  31. v-if="categoryInfo.modelList.length > 0"
  32. link
  33. type="info"
  34. class="mr-20px"
  35. @click.stop="handleModelSort"
  36. >
  37. <Icon icon="fa:sort-amount-desc" class="mr-5px" />
  38. 排序
  39. </el-button>
  40. <el-button v-else link type="info" class="mr-20px" @click.stop="openModelForm('create')">
  41. <Icon icon="fa:plus" class="mr-5px" />
  42. 新建
  43. </el-button>
  44. <el-dropdown
  45. @command="(command) => handleCategoryCommand(command, categoryInfo)"
  46. placement="bottom"
  47. >
  48. <el-button link type="info">
  49. <Icon icon="ep:setting" class="mr-5px" />
  50. 分类
  51. </el-button>
  52. <template #dropdown>
  53. <el-dropdown-menu>
  54. <el-dropdown-item command="handleRename"> 重命名 </el-dropdown-item>
  55. <el-dropdown-item command="handleDeleteCategory"> 删除该类 </el-dropdown-item>
  56. </el-dropdown-menu>
  57. </template>
  58. </el-dropdown>
  59. </template>
  60. <template v-else>
  61. <el-button @click.stop="handleModelSortCancel"> 取 消 </el-button>
  62. <el-button type="primary" @click.stop="handleModelSortSubmit"> 保存排序 </el-button>
  63. </template>
  64. </div>
  65. </div>
  66. </div>
  67. <!-- 模型列表 -->
  68. <el-collapse-transition>
  69. <div v-show="isExpand">
  70. <el-table
  71. v-if="modelList && modelList.length > 0"
  72. :class="categoryInfo.name"
  73. ref="tableRef"
  74. :data="modelList"
  75. row-key="id"
  76. :header-cell-style="tableHeaderStyle"
  77. :cell-style="tableCellStyle"
  78. :row-style="{ height: '68px' }"
  79. >
  80. <el-table-column label="流程名" prop="name" min-width="150">
  81. <template #default="{ row }">
  82. <div class="flex items-center">
  83. <el-tooltip content="拖动排序" v-if="isModelSorting">
  84. <Icon
  85. icon="ic:round-drag-indicator"
  86. class="drag-icon cursor-move text-#8a909c mr-10px"
  87. />
  88. </el-tooltip>
  89. <el-image v-if="row.icon" :src="row.icon" class="h-38px w-38px mr-10px rounded" />
  90. <div v-else class="flow-icon">
  91. <span style="font-size: 12px; color: #fff">{{ subString(row.name, 0, 2) }}</span>
  92. </div>
  93. {{ row.name }}
  94. </div>
  95. </template>
  96. </el-table-column>
  97. <el-table-column label="可见范围" prop="startUserIds" min-width="150">
  98. <template #default="{ row }">
  99. <el-text v-if="!row.startUsers?.length"> 全部可见 </el-text>
  100. <el-text v-else-if="row.startUsers.length === 1">
  101. {{ row.startUsers[0].nickname }}
  102. </el-text>
  103. <el-text v-else>
  104. <el-tooltip
  105. class="box-item"
  106. effect="dark"
  107. placement="top"
  108. :content="row.startUsers.map((user: any) => user.nickname).join('、')"
  109. >
  110. {{ row.startUsers[0].nickname }}等 {{ row.startUsers.length }} 人可见
  111. </el-tooltip>
  112. </el-text>
  113. </template>
  114. </el-table-column>
  115. <el-table-column label="流程类型" prop="modelType" min-width="120">
  116. <template #default="{ row }">
  117. <dict-tag :value="row.type" :type="DICT_TYPE.BPM_MODEL_TYPE" />
  118. </template>
  119. </el-table-column>
  120. <el-table-column label="表单信息" prop="formType" min-width="150">
  121. <template #default="scope">
  122. <el-button
  123. v-if="scope.row.formType === BpmModelFormType.NORMAL"
  124. type="primary"
  125. link
  126. @click="handleFormDetail(scope.row)"
  127. >
  128. <span>{{ scope.row.formName }}</span>
  129. </el-button>
  130. <el-button
  131. v-else-if="scope.row.formType === BpmModelFormType.CUSTOM"
  132. type="primary"
  133. link
  134. @click="handleFormDetail(scope.row)"
  135. >
  136. <span>{{ scope.row.formCustomCreatePath }}</span>
  137. </el-button>
  138. <label v-else>暂无表单</label>
  139. </template>
  140. </el-table-column>
  141. <el-table-column label="最后发布" prop="deploymentTime" min-width="250">
  142. <template #default="scope">
  143. <div class="flex items-center">
  144. <span v-if="scope.row.processDefinition" class="w-150px">
  145. {{ formatDate(scope.row.processDefinition.deploymentTime) }}
  146. </span>
  147. <el-tag v-if="scope.row.processDefinition">
  148. v{{ scope.row.processDefinition.version }}
  149. </el-tag>
  150. <el-tag v-else type="warning">未部署</el-tag>
  151. <el-tag
  152. v-if="scope.row.processDefinition?.suspensionState === 2"
  153. type="warning"
  154. class="ml-10px"
  155. >
  156. 已停用
  157. </el-tag>
  158. </div>
  159. </template>
  160. </el-table-column>
  161. <el-table-column label="操作" width="200" fixed="right">
  162. <template #default="scope">
  163. <el-button
  164. link
  165. type="primary"
  166. @click="openModelForm('update', scope.row.id)"
  167. v-if="hasPermiUpdate"
  168. :disabled="!isManagerUser(scope.row)"
  169. >
  170. 修改
  171. </el-button>
  172. <el-button
  173. link
  174. type="primary"
  175. @click="openModelForm('copy', scope.row.id)"
  176. v-if="hasPermiUpdate"
  177. :disabled="!isManagerUser(scope.row)"
  178. >
  179. 复制
  180. </el-button>
  181. <el-button
  182. link
  183. class="!ml-5px"
  184. type="primary"
  185. @click="handleDeploy(scope.row)"
  186. v-if="hasPermiDeploy"
  187. :disabled="!isManagerUser(scope.row)"
  188. >
  189. 发布
  190. </el-button>
  191. <el-dropdown
  192. class="!align-middle ml-5px"
  193. @command="(command) => handleModelCommand(command, scope.row)"
  194. v-if="hasPermiMore"
  195. >
  196. <el-button type="primary" link>更多</el-button>
  197. <template #dropdown>
  198. <el-dropdown-menu>
  199. <el-dropdown-item command="handleDefinitionList" v-if="hasPermiPdQuery">
  200. 历史
  201. </el-dropdown-item>
  202. <el-dropdown-item
  203. command="handleReport"
  204. v-if="
  205. checkPermi(['bpm:process-instance:manager-query']) &&
  206. scope.row.processDefinition
  207. "
  208. :disabled="!isManagerUser(scope.row)"
  209. >
  210. 报表
  211. </el-dropdown-item>
  212. <el-dropdown-item
  213. command="handleChangeState"
  214. v-if="hasPermiUpdate && scope.row.processDefinition"
  215. :disabled="!isManagerUser(scope.row)"
  216. >
  217. {{ scope.row.processDefinition.suspensionState === 1 ? '停用' : '启用' }}
  218. </el-dropdown-item>
  219. <el-dropdown-item
  220. type="danger"
  221. command="handleClean"
  222. v-if="checkPermi(['bpm:model:clean'])"
  223. :disabled="!isManagerUser(scope.row)"
  224. >
  225. 清理
  226. </el-dropdown-item>
  227. <el-dropdown-item
  228. type="danger"
  229. command="handleDelete"
  230. v-if="hasPermiDelete"
  231. :disabled="!isManagerUser(scope.row)"
  232. >
  233. 删除
  234. </el-dropdown-item>
  235. </el-dropdown-menu>
  236. </template>
  237. </el-dropdown>
  238. </template>
  239. </el-table-column>
  240. </el-table>
  241. </div>
  242. </el-collapse-transition>
  243. <!-- 弹窗:重命名分类 -->
  244. <Dialog :fullscreen="false" class="rename-dialog" v-model="renameCategoryVisible" width="400">
  245. <template #title>
  246. <div class="pl-10px font-bold text-18px"> 重命名分类 </div>
  247. </template>
  248. <div class="px-30px">
  249. <el-input v-model="renameCategoryForm.name" />
  250. </div>
  251. <template #footer>
  252. <div class="pr-25px pb-25px">
  253. <el-button @click="renameCategoryVisible = false">取 消</el-button>
  254. <el-button type="primary" @click="handleRenameConfirm">确 定</el-button>
  255. </div>
  256. </template>
  257. </Dialog>
  258. <!-- 弹窗:表单详情 -->
  259. <Dialog title="表单详情" :fullscreen="true" v-model="formDetailVisible">
  260. <form-create :rule="formDetailPreview.rule" :option="formDetailPreview.option" />
  261. </Dialog>
  262. </template>
  263. <script lang="ts" setup>
  264. import { DICT_TYPE } from '@/utils/dict'
  265. import { CategoryApi, CategoryVO } from '@/api/bpm/category'
  266. import Sortable from 'sortablejs'
  267. import { formatDate } from '@/utils/formatTime'
  268. import * as ModelApi from '@/api/bpm/model'
  269. import * as FormApi from '@/api/bpm/form'
  270. import { setConfAndFields2 } from '@/utils/formCreate'
  271. import { BpmModelFormType } from '@/utils/constants'
  272. import { checkPermi } from '@/utils/permission'
  273. import { useUserStoreWithOut } from '@/store/modules/user'
  274. import { useAppStore } from '@/store/modules/app'
  275. import { cloneDeep, isEqual } from 'lodash-es'
  276. import { useTagsView } from '@/hooks/web/useTagsView'
  277. import { useDebounceFn } from '@vueuse/core'
  278. import { subString } from '@/utils/index'
  279. defineOptions({ name: 'BpmModel' })
  280. // 优化 Props 类型定义
  281. interface UserInfo {
  282. nickname: string
  283. [key: string]: any
  284. }
  285. interface ProcessDefinition {
  286. deploymentTime: string
  287. version: number
  288. suspensionState: number
  289. }
  290. interface ModelInfo {
  291. id: number
  292. name: string
  293. icon?: string
  294. startUsers?: UserInfo[]
  295. processDefinition?: ProcessDefinition
  296. formType?: number
  297. formId?: number
  298. formName?: string
  299. formCustomCreatePath?: string
  300. managerUserIds?: number[]
  301. [key: string]: any
  302. }
  303. interface CategoryInfoProps {
  304. id: number
  305. name: string
  306. modelList: ModelInfo[]
  307. }
  308. const props = defineProps<{
  309. categoryInfo: CategoryInfoProps
  310. isCategorySorting: boolean
  311. }>()
  312. const emit = defineEmits(['success'])
  313. const message = useMessage() // 消息弹窗
  314. const { t } = useI18n() // 国际化
  315. const { push } = useRouter() // 路由
  316. const userStore = useUserStoreWithOut() // 用户信息缓存
  317. const isDark = computed(() => useAppStore().getIsDark) // 是否黑暗模式
  318. const router = useRouter() // 路由
  319. const isModelSorting = ref(false) // 是否正处于排序状态
  320. const originalData = ref<ModelInfo[]>([]) // 原始数据
  321. const modelList = ref<ModelInfo[]>([]) // 模型列表
  322. const isExpand = ref(false) // 是否处于展开状态
  323. // 使用 computed 优化表格样式计算
  324. const tableHeaderStyle = computed(() => ({
  325. backgroundColor: isDark.value ? '' : '#edeff0',
  326. paddingLeft: '10px'
  327. }))
  328. const tableCellStyle = computed(() => ({
  329. paddingLeft: '10px'
  330. }))
  331. /** 权限校验:通过 computed 解决列表的卡顿问题 */
  332. const hasPermiUpdate = computed(() => {
  333. return checkPermi(['bpm:model:update'])
  334. })
  335. const hasPermiDelete = computed(() => {
  336. return checkPermi(['bpm:model:delete'])
  337. })
  338. const hasPermiDeploy = computed(() => {
  339. return checkPermi(['bpm:model:deploy'])
  340. })
  341. const hasPermiMore = computed(() => {
  342. return checkPermi(['bpm:process-definition:query', 'bpm:model:update', 'bpm:model:delete'])
  343. })
  344. const hasPermiPdQuery = computed(() => {
  345. return checkPermi(['bpm:process-definition:query'])
  346. })
  347. /** '更多'操作按钮 */
  348. const handleModelCommand = (command: string, row: any) => {
  349. switch (command) {
  350. case 'handleDefinitionList':
  351. handleDefinitionList(row)
  352. break
  353. case 'handleDelete':
  354. handleDelete(row)
  355. break
  356. case 'handleChangeState':
  357. handleChangeState(row)
  358. break
  359. case 'handleClean':
  360. handleClean(row)
  361. break
  362. case 'handleReport':
  363. router.push({
  364. name: 'BpmProcessInstanceReport',
  365. query: {
  366. processDefinitionId: row.processDefinition.id,
  367. processDefinitionKey: row.key
  368. }
  369. })
  370. break
  371. default:
  372. break
  373. }
  374. }
  375. /** '分类'操作按钮 */
  376. const handleCategoryCommand = async (command: string, row: any) => {
  377. switch (command) {
  378. case 'handleRename':
  379. renameCategoryForm.value = await CategoryApi.getCategory(row.id)
  380. renameCategoryVisible.value = true
  381. break
  382. case 'handleDeleteCategory':
  383. await handleDeleteCategory()
  384. break
  385. default:
  386. break
  387. }
  388. }
  389. /** 删除按钮操作 */
  390. const handleDelete = async (row: any) => {
  391. try {
  392. // 删除的二次确认
  393. await message.delConfirm()
  394. // 发起删除
  395. await ModelApi.deleteModel(row.id)
  396. message.success(t('common.delSuccess'))
  397. // 刷新列表
  398. emit('success')
  399. } catch {}
  400. }
  401. /** 清理按钮操作 */
  402. const handleClean = async (row: any) => {
  403. try {
  404. // 清理的二次确认
  405. await message.confirm('是否确认清理流程名字为"' + row.name + '"的数据项?')
  406. // 发起清理
  407. await ModelApi.cleanModel(row.id)
  408. message.success('清理成功')
  409. // 刷新列表
  410. emit('success')
  411. } catch {}
  412. }
  413. /** 更新状态操作 */
  414. const handleChangeState = async (row: any) => {
  415. const state = row.processDefinition.suspensionState
  416. const newState = state === 1 ? 2 : 1
  417. try {
  418. // 修改状态的二次确认
  419. const id = row.id
  420. debugger
  421. const statusState = state === 1 ? '停用' : '启用'
  422. const content = '是否确认' + statusState + '流程名字为"' + row.name + '"的数据项?'
  423. await message.confirm(content)
  424. // 发起修改状态
  425. await ModelApi.updateModelState(id, newState)
  426. message.success(statusState + '成功')
  427. // 刷新列表
  428. emit('success')
  429. } catch {}
  430. }
  431. /** 发布流程 */
  432. const handleDeploy = async (row: any) => {
  433. try {
  434. await message.confirm('是否确认发布该流程?')
  435. // 发起部署
  436. await ModelApi.deployModel(row.id)
  437. message.success(t('发布成功'))
  438. // 刷新列表
  439. emit('success')
  440. } catch {}
  441. }
  442. /** 跳转到指定流程定义列表 */
  443. const handleDefinitionList = (row: any) => {
  444. push({
  445. name: 'BpmProcessDefinition',
  446. query: {
  447. key: row.key
  448. }
  449. })
  450. }
  451. /** 流程表单的详情按钮操作 */
  452. const formDetailVisible = ref(false)
  453. const formDetailPreview = ref({
  454. rule: [],
  455. option: {}
  456. })
  457. const handleFormDetail = async (row: any) => {
  458. if (row.formType == BpmModelFormType.NORMAL) {
  459. // 设置表单
  460. const data = await FormApi.getForm(row.formId)
  461. setConfAndFields2(formDetailPreview, data.conf, data.fields)
  462. // 弹窗打开
  463. formDetailVisible.value = true
  464. } else {
  465. await push({
  466. path: row.formCustomCreatePath
  467. })
  468. }
  469. }
  470. /** 判断是否可以操作 */
  471. const isManagerUser = (row: any) => {
  472. const userId = userStore.getUser.id
  473. return row.managerUserIds && row.managerUserIds.includes(userId)
  474. }
  475. /** 处理模型的排序 **/
  476. const handleModelSort = () => {
  477. // 保存初始数据
  478. originalData.value = cloneDeep(props.categoryInfo.modelList)
  479. isModelSorting.value = true
  480. initSort()
  481. }
  482. /** 处理模型的排序提交 */
  483. const handleModelSortSubmit = async () => {
  484. // 保存排序
  485. const ids = modelList.value.map((item: any) => item.id)
  486. await ModelApi.updateModelSortBatch(ids)
  487. // 刷新列表
  488. isModelSorting.value = false
  489. message.success('排序模型成功')
  490. emit('success')
  491. }
  492. /** 处理模型的排序取消 */
  493. const handleModelSortCancel = () => {
  494. // 恢复初始数据
  495. modelList.value = cloneDeep(originalData.value)
  496. isModelSorting.value = false
  497. }
  498. /** 创建拖拽实例 */
  499. const tableRef = ref()
  500. const initSort = useDebounceFn(() => {
  501. const table = document.querySelector(`.${props.categoryInfo.name} .el-table__body-wrapper tbody`)
  502. if (!table) return
  503. Sortable.create(table, {
  504. group: 'shared',
  505. animation: 150,
  506. draggable: '.el-table__row',
  507. handle: '.drag-icon',
  508. onEnd: ({ newDraggableIndex, oldDraggableIndex }) => {
  509. if (oldDraggableIndex !== newDraggableIndex) {
  510. modelList.value.splice(
  511. newDraggableIndex,
  512. 0,
  513. modelList.value.splice(oldDraggableIndex, 1)[0]
  514. )
  515. }
  516. }
  517. })
  518. }, 200)
  519. /** 更新 modelList 模型列表 */
  520. const updateModeList = useDebounceFn(() => {
  521. const newModelList = props.categoryInfo.modelList
  522. if (!isEqual(modelList.value, newModelList)) {
  523. modelList.value = cloneDeep(newModelList)
  524. if (newModelList?.length > 0) {
  525. isExpand.value = true
  526. }
  527. }
  528. }, 100)
  529. /** 重命名弹窗确定 */
  530. const renameCategoryVisible = ref(false)
  531. const renameCategoryForm = ref({
  532. name: ''
  533. })
  534. const handleRenameConfirm = async () => {
  535. if (renameCategoryForm.value?.name.length === 0) {
  536. return message.warning('请输入名称')
  537. }
  538. // 发起修改
  539. await CategoryApi.updateCategory(renameCategoryForm.value as CategoryVO)
  540. message.success('重命名成功')
  541. // 刷新列表
  542. renameCategoryVisible.value = false
  543. emit('success')
  544. }
  545. /** 删除分类 */
  546. const handleDeleteCategory = async () => {
  547. try {
  548. if (props.categoryInfo.modelList.length > 0) {
  549. return message.warning('该分类下仍有流程定义,不允许删除')
  550. }
  551. await message.confirm('确认删除分类吗?')
  552. // 发起删除
  553. await CategoryApi.deleteCategory(props.categoryInfo.id)
  554. message.success(t('common.delSuccess'))
  555. // 刷新列表
  556. emit('success')
  557. } catch {}
  558. }
  559. /** 添加流程模型弹窗 */
  560. const tagsView = useTagsView()
  561. const openModelForm = async (type: string, id?: number) => {
  562. if (type === 'create') {
  563. await push({ name: 'BpmModelCreate' })
  564. } else {
  565. await push({
  566. name: 'BpmModelUpdate',
  567. params: { id, type }
  568. })
  569. // 设置标题
  570. if (type === 'copy') {
  571. tagsView.setTitle('复制流程')
  572. }
  573. }
  574. }
  575. watchEffect(() => {
  576. if (props.categoryInfo?.modelList) {
  577. updateModeList()
  578. }
  579. if (props.isCategorySorting) {
  580. isExpand.value = false
  581. }
  582. })
  583. </script>
  584. <style lang="scss">
  585. .rename-dialog.el-dialog {
  586. padding: 0 !important;
  587. .el-dialog__header {
  588. border-bottom: none;
  589. }
  590. .el-dialog__footer {
  591. border-top: none !important;
  592. }
  593. }
  594. </style>
  595. <style lang="scss" scoped>
  596. .flow-icon {
  597. display: flex;
  598. width: 38px;
  599. height: 38px;
  600. margin-right: 10px;
  601. background-color: var(--el-color-primary);
  602. border-radius: 0.25rem;
  603. align-items: center;
  604. justify-content: center;
  605. }
  606. .category-draggable-model {
  607. :deep(.el-table__cell) {
  608. overflow: hidden;
  609. border-bottom: none !important;
  610. }
  611. // 优化表格渲染性能
  612. :deep(.el-table__body) {
  613. will-change: transform;
  614. transform: translateZ(0);
  615. }
  616. }
  617. </style>