index.vue 8.6 KB

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