index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <ContentWrap>
  3. <!-- 审批信息 -->
  4. <el-card
  5. v-for="(item, index) in runningTasks"
  6. :key="index"
  7. v-loading="processInstanceLoading"
  8. class="box-card"
  9. >
  10. <template #header>
  11. <span class="el-icon-picture-outline">审批任务【{{ item.name }}】</span>
  12. </template>
  13. <el-col :offset="6" :span="16">
  14. <el-form
  15. :ref="'form' + index"
  16. :model="auditForms[index]"
  17. :rules="auditRule"
  18. label-width="100px"
  19. >
  20. <el-form-item v-if="processInstance && processInstance.name" label="流程名">
  21. {{ processInstance.name }}
  22. </el-form-item>
  23. <el-form-item v-if="processInstance && processInstance.startUser" label="流程发起人">
  24. {{ processInstance.startUser.nickname }}
  25. <el-tag size="small" type="info">{{ processInstance.startUser.deptName }}</el-tag>
  26. </el-form-item>
  27. <el-form-item label="审批建议" prop="reason">
  28. <el-input
  29. v-model="auditForms[index].reason"
  30. placeholder="请输入审批建议"
  31. type="textarea"
  32. />
  33. </el-form-item>
  34. </el-form>
  35. <div style="margin-bottom: 20px; margin-left: 10%; font-size: 14px">
  36. <el-button type="success" @click="handleAudit(item, true)">
  37. <Icon icon="ep:select" />
  38. 通过
  39. </el-button>
  40. <el-button type="danger" @click="handleAudit(item, false)">
  41. <Icon icon="ep:close" />
  42. 不通过
  43. </el-button>
  44. <el-button type="primary" @click="openTaskUpdateAssigneeForm(item.id)">
  45. <Icon icon="ep:edit" />
  46. 转办
  47. </el-button>
  48. <el-button type="primary" @click="handleDelegate(item)">
  49. <Icon icon="ep:position" />
  50. 委派
  51. </el-button>
  52. <el-button type="warning" @click="handleBack(item)">
  53. <Icon icon="ep:back" />
  54. 回退
  55. </el-button>
  56. </div>
  57. </el-col>
  58. </el-card>
  59. <!-- 申请信息 -->
  60. <el-card v-loading="processInstanceLoading" class="box-card">
  61. <template #header>
  62. <span class="el-icon-document">申请信息【{{ processInstance.name }}】</span>
  63. </template>
  64. <!-- 情况一:流程表单 -->
  65. <el-col v-if="processInstance?.processDefinition?.formType === 10" :offset="6" :span="16">
  66. <form-create
  67. ref="fApi"
  68. v-model="detailForm.value"
  69. :option="detailForm.option"
  70. :rule="detailForm.rule"
  71. />
  72. </el-col>
  73. <!-- 情况二:业务表单 -->
  74. <div v-if="processInstance?.processDefinition?.formType === 20">
  75. <BusinessFormComponent :id="processInstance.businessKey" />
  76. </div>
  77. </el-card>
  78. <!-- 审批记录 -->
  79. <ProcessInstanceTaskList :loading="tasksLoad" :tasks="tasks" />
  80. <!-- 高亮流程图 -->
  81. <ProcessInstanceBpmnViewer
  82. :id="`${id}`"
  83. :bpmn-xml="bpmnXML"
  84. :loading="processInstanceLoading"
  85. :process-instance="processInstance"
  86. :tasks="tasks"
  87. />
  88. <!-- 弹窗:转派审批人 -->
  89. <TaskUpdateAssigneeForm ref="taskUpdateAssigneeFormRef" @success="getDetail" />
  90. <!-- 弹窗,回退节点 -->
  91. <TaskReturnDialog ref="taskReturnDialogRef" @success="getDetail" />
  92. </ContentWrap>
  93. </template>
  94. <script lang="ts" setup>
  95. import { useUserStore } from '@/store/modules/user'
  96. import { setConfAndFields2 } from '@/utils/formCreate'
  97. import type { ApiAttrs } from '@form-create/element-ui/types/config'
  98. import * as DefinitionApi from '@/api/bpm/definition'
  99. import * as ProcessInstanceApi from '@/api/bpm/processInstance'
  100. import * as TaskApi from '@/api/bpm/task'
  101. import TaskUpdateAssigneeForm from './TaskUpdateAssigneeForm.vue'
  102. import ProcessInstanceBpmnViewer from './ProcessInstanceBpmnViewer.vue'
  103. import ProcessInstanceTaskList from './ProcessInstanceTaskList.vue'
  104. import TaskReturnDialog from './TaskReturnDialogForm.vue'
  105. import { registerComponent } from '@/utils/routerHelper'
  106. defineOptions({ name: 'BpmProcessInstanceDetail' })
  107. const { query } = useRoute() // 查询参数
  108. const message = useMessage() // 消息弹窗
  109. const { proxy } = getCurrentInstance() as any
  110. const userId = useUserStore().getUser.id // 当前登录的编号
  111. const id = query.id as unknown as number // 流程实例的编号
  112. const processInstanceLoading = ref(false) // 流程实例的加载中
  113. const processInstance = ref<any>({}) // 流程实例
  114. const bpmnXML = ref('') // BPMN XML
  115. const tasksLoad = ref(true) // 任务的加载中
  116. const tasks = ref<any[]>([]) // 任务列表
  117. // ========== 审批信息 ==========
  118. const runningTasks = ref<any[]>([]) // 运行中的任务
  119. const auditForms = ref<any[]>([]) // 审批任务的表单
  120. const auditRule = reactive({
  121. reason: [{ required: true, message: '审批建议不能为空', trigger: 'blur' }]
  122. })
  123. // ========== 申请信息 ==========
  124. const fApi = ref<ApiAttrs>() //
  125. const detailForm = ref({
  126. // 流程表单详情
  127. rule: [],
  128. option: {},
  129. value: {}
  130. })
  131. /** 处理审批通过和不通过的操作 */
  132. const handleAudit = async (task, pass) => {
  133. // 1.1 获得对应表单
  134. const index = runningTasks.value.indexOf(task)
  135. const auditFormRef = proxy.$refs['form' + index][0]
  136. // 1.2 校验表单
  137. const elForm = unref(auditFormRef)
  138. if (!elForm) return
  139. const valid = await elForm.validate()
  140. if (!valid) return
  141. // 2.1 提交审批
  142. const data = {
  143. id: task.id,
  144. reason: auditForms.value[index].reason
  145. }
  146. if (pass) {
  147. await TaskApi.approveTask(data)
  148. message.success('审批通过成功')
  149. } else {
  150. await TaskApi.rejectTask(data)
  151. message.success('审批不通过成功')
  152. }
  153. // 2.2 加载最新数据
  154. getDetail()
  155. }
  156. /** 转派审批人 */
  157. const taskUpdateAssigneeFormRef = ref()
  158. const openTaskUpdateAssigneeForm = (id: string) => {
  159. taskUpdateAssigneeFormRef.value.open(id)
  160. }
  161. /** 处理审批退回的操作 */
  162. const handleDelegate = async (task) => {
  163. message.error('暂不支持【委派】功能,可以使用【转派】替代!')
  164. console.log(task)
  165. }
  166. //回退弹框组件
  167. const taskReturnDialogRef = ref()
  168. /** 处理审批退回的操作 */
  169. const handleBack = async (task) => {
  170. taskReturnDialogRef.value.open(task.id)
  171. }
  172. /** 获得详情 */
  173. const getDetail = () => {
  174. // 1. 获得流程实例相关
  175. getProcessInstance()
  176. // 2. 获得流程任务列表(审批记录)
  177. getTaskList()
  178. }
  179. /** 加载流程实例 */
  180. const BusinessFormComponent = ref(null) // 异步组件
  181. const getProcessInstance = async () => {
  182. try {
  183. processInstanceLoading.value = true
  184. const data = await ProcessInstanceApi.getProcessInstance(id)
  185. if (!data) {
  186. message.error('查询不到流程信息!')
  187. return
  188. }
  189. processInstance.value = data
  190. // 设置表单信息
  191. const processDefinition = data.processDefinition
  192. if (processDefinition.formType === 10) {
  193. setConfAndFields2(
  194. detailForm,
  195. processDefinition.formConf,
  196. processDefinition.formFields,
  197. data.formVariables
  198. )
  199. nextTick().then(() => {
  200. fApi.value?.fapi?.btn.show(false)
  201. fApi.value?.fapi?.resetBtn.show(false)
  202. fApi.value?.fapi?.disabled(true)
  203. })
  204. } else {
  205. BusinessFormComponent.value = registerComponent(data.processDefinition.formCustomViewPath)
  206. }
  207. // 加载流程图
  208. bpmnXML.value = await DefinitionApi.getProcessDefinitionBpmnXML(processDefinition.id as number)
  209. } finally {
  210. processInstanceLoading.value = false
  211. }
  212. }
  213. /** 加载任务列表 */
  214. const getTaskList = async () => {
  215. try {
  216. // 获得未取消的任务
  217. tasksLoad.value = true
  218. const data = await TaskApi.getTaskListByProcessInstanceId(id)
  219. tasks.value = []
  220. // 1.1 移除已取消的审批
  221. data.forEach((task) => {
  222. if (task.result !== 4) {
  223. tasks.value.push(task)
  224. }
  225. })
  226. // 1.2 排序,将未完成的排在前面,已完成的排在后面;
  227. tasks.value.sort((a, b) => {
  228. // 有已完成的情况,按照完成时间倒序
  229. if (a.endTime && b.endTime) {
  230. return b.endTime - a.endTime
  231. } else if (a.endTime) {
  232. return 1
  233. } else if (b.endTime) {
  234. return -1
  235. // 都是未完成,按照创建时间倒序
  236. } else {
  237. return b.createTime - a.createTime
  238. }
  239. })
  240. // 获得需要自己审批的任务
  241. runningTasks.value = []
  242. auditForms.value = []
  243. tasks.value.forEach((task) => {
  244. // 2.1 只有待处理才需要
  245. if (task.result !== 1) {
  246. return
  247. }
  248. // 2.2 自己不是处理人
  249. if (!task.assigneeUser || task.assigneeUser.id !== userId) {
  250. return
  251. }
  252. // 2.3 添加到处理任务
  253. runningTasks.value.push({ ...task })
  254. auditForms.value.push({
  255. reason: ''
  256. })
  257. })
  258. } finally {
  259. tasksLoad.value = false
  260. }
  261. }
  262. /** 初始化 */
  263. onMounted(() => {
  264. getDetail()
  265. })
  266. </script>