CategoryDraggableModel.vue 20 KB

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