index.vue 8.6 KB

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