index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. <!-- 委派,将任务委派给别人处理,处理完成后,会重新回到原审批人手中-->
  93. <TaskDelegateForm ref="taskDelegateForm" @success="getDetail" />
  94. </ContentWrap>
  95. </template>
  96. <script lang="ts" setup>
  97. import { useUserStore } from '@/store/modules/user'
  98. import { setConfAndFields2 } from '@/utils/formCreate'
  99. import type { ApiAttrs } from '@form-create/element-ui/types/config'
  100. import * as DefinitionApi from '@/api/bpm/definition'
  101. import * as ProcessInstanceApi from '@/api/bpm/processInstance'
  102. import * as TaskApi from '@/api/bpm/task'
  103. import TaskUpdateAssigneeForm from './TaskUpdateAssigneeForm.vue'
  104. import ProcessInstanceBpmnViewer from './ProcessInstanceBpmnViewer.vue'
  105. import ProcessInstanceTaskList from './ProcessInstanceTaskList.vue'
  106. import TaskReturnDialog from './TaskReturnDialogForm.vue'
  107. import TaskDelegateForm from './taskDelegateForm.vue'
  108. import { registerComponent } from '@/utils/routerHelper'
  109. defineOptions({ name: 'BpmProcessInstanceDetail' })
  110. const { query } = useRoute() // 查询参数
  111. const message = useMessage() // 消息弹窗
  112. const { proxy } = getCurrentInstance() as any
  113. const userId = useUserStore().getUser.id // 当前登录的编号
  114. const id = query.id as unknown as number // 流程实例的编号
  115. const processInstanceLoading = ref(false) // 流程实例的加载中
  116. const processInstance = ref<any>({}) // 流程实例
  117. const bpmnXML = ref('') // BPMN XML
  118. const tasksLoad = ref(true) // 任务的加载中
  119. const tasks = ref<any[]>([]) // 任务列表
  120. // ========== 审批信息 ==========
  121. const runningTasks = ref<any[]>([]) // 运行中的任务
  122. const auditForms = ref<any[]>([]) // 审批任务的表单
  123. const auditRule = reactive({
  124. reason: [{ required: true, message: '审批建议不能为空', trigger: 'blur' }]
  125. })
  126. // ========== 申请信息 ==========
  127. const fApi = ref<ApiAttrs>() //
  128. const detailForm = ref({
  129. // 流程表单详情
  130. rule: [],
  131. option: {},
  132. value: {}
  133. })
  134. /** 处理审批通过和不通过的操作 */
  135. const handleAudit = async (task, pass) => {
  136. // 1.1 获得对应表单
  137. const index = runningTasks.value.indexOf(task)
  138. const auditFormRef = proxy.$refs['form' + index][0]
  139. // 1.2 校验表单
  140. const elForm = unref(auditFormRef)
  141. if (!elForm) return
  142. const valid = await elForm.validate()
  143. if (!valid) return
  144. // 2.1 提交审批
  145. const data = {
  146. id: task.id,
  147. reason: auditForms.value[index].reason
  148. }
  149. if (pass) {
  150. await TaskApi.approveTask(data)
  151. message.success('审批通过成功')
  152. } else {
  153. await TaskApi.rejectTask(data)
  154. message.success('审批不通过成功')
  155. }
  156. // 2.2 加载最新数据
  157. getDetail()
  158. }
  159. /** 转派审批人 */
  160. const taskUpdateAssigneeFormRef = ref()
  161. const openTaskUpdateAssigneeForm = (id: string) => {
  162. taskUpdateAssigneeFormRef.value.open(id)
  163. }
  164. const taskDelegateForm = ref()
  165. /** 处理审批退回的操作 */
  166. const handleDelegate = async (task) => {
  167. taskDelegateForm.value.open(task.id)
  168. }
  169. //回退弹框组件
  170. const taskReturnDialogRef = ref()
  171. /** 处理审批退回的操作 */
  172. const handleBack = async (task) => {
  173. taskReturnDialogRef.value.open(task.id)
  174. }
  175. /** 获得详情 */
  176. const getDetail = () => {
  177. // 1. 获得流程实例相关
  178. getProcessInstance()
  179. // 2. 获得流程任务列表(审批记录)
  180. getTaskList()
  181. }
  182. /** 加载流程实例 */
  183. const BusinessFormComponent = ref(null) // 异步组件
  184. const getProcessInstance = async () => {
  185. try {
  186. processInstanceLoading.value = true
  187. const data = await ProcessInstanceApi.getProcessInstance(id)
  188. if (!data) {
  189. message.error('查询不到流程信息!')
  190. return
  191. }
  192. processInstance.value = data
  193. // 设置表单信息
  194. const processDefinition = data.processDefinition
  195. if (processDefinition.formType === 10) {
  196. setConfAndFields2(
  197. detailForm,
  198. processDefinition.formConf,
  199. processDefinition.formFields,
  200. data.formVariables
  201. )
  202. nextTick().then(() => {
  203. fApi.value?.fapi?.btn.show(false)
  204. fApi.value?.fapi?.resetBtn.show(false)
  205. fApi.value?.fapi?.disabled(true)
  206. })
  207. } else {
  208. BusinessFormComponent.value = registerComponent(data.processDefinition.formCustomViewPath)
  209. }
  210. // 加载流程图
  211. bpmnXML.value = await DefinitionApi.getProcessDefinitionBpmnXML(processDefinition.id as number)
  212. } finally {
  213. processInstanceLoading.value = false
  214. }
  215. }
  216. /** 加载任务列表 */
  217. const getTaskList = async () => {
  218. try {
  219. // 获得未取消的任务
  220. tasksLoad.value = true
  221. const data = await TaskApi.getTaskListByProcessInstanceId(id)
  222. tasks.value = []
  223. // 1.1 移除已取消的审批
  224. data.forEach((task) => {
  225. if (task.result !== 4) {
  226. tasks.value.push(task)
  227. }
  228. })
  229. // 1.2 排序,将未完成的排在前面,已完成的排在后面;
  230. tasks.value.sort((a, b) => {
  231. // 有已完成的情况,按照完成时间倒序
  232. if (a.endTime && b.endTime) {
  233. return b.endTime - a.endTime
  234. } else if (a.endTime) {
  235. return 1
  236. } else if (b.endTime) {
  237. return -1
  238. // 都是未完成,按照创建时间倒序
  239. } else {
  240. return b.createTime - a.createTime
  241. }
  242. })
  243. // 获得需要自己审批的任务
  244. runningTasks.value = []
  245. auditForms.value = []
  246. tasks.value.forEach((task) => {
  247. // 2.1 只有待处理才需要
  248. if (task.result !== 1 && task.result !== 6) {
  249. return
  250. }
  251. // 2.2 自己不是处理人
  252. if (!task.assigneeUser || task.assigneeUser.id !== userId) {
  253. return
  254. }
  255. // 2.3 添加到处理任务
  256. runningTasks.value.push({ ...task })
  257. auditForms.value.push({
  258. reason: ''
  259. })
  260. })
  261. } finally {
  262. tasksLoad.value = false
  263. }
  264. }
  265. /** 初始化 */
  266. onMounted(() => {
  267. getDetail()
  268. })
  269. </script>