index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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-card class="mb-15px !-mt-10px" v-if="runningTasks[index].formId > 0">
  28. <template #header>
  29. <span class="el-icon-picture-outline">
  30. 填写表单【{{ runningTasks[index]?.formName }}】
  31. </span>
  32. </template>
  33. <form-create
  34. v-model:api="approveFormFApis[index]"
  35. v-model="approveForms[index].value"
  36. :option="approveForms[index].option"
  37. :rule="approveForms[index].rule"
  38. />
  39. </el-card>
  40. <el-form-item label="审批建议" prop="reason">
  41. <el-input
  42. v-model="auditForms[index].reason"
  43. placeholder="请输入审批建议"
  44. type="textarea"
  45. />
  46. </el-form-item>
  47. <el-form-item label="抄送人" prop="copyUserIds">
  48. <el-select v-model="auditForms[index].copyUserIds" multiple placeholder="请选择抄送人">
  49. <el-option
  50. v-for="item in userOptions"
  51. :key="item.id"
  52. :label="item.nickname"
  53. :value="item.id"
  54. />
  55. </el-select>
  56. </el-form-item>
  57. </el-form>
  58. <div style="margin-bottom: 20px; margin-left: 10%; font-size: 14px">
  59. <el-button type="success" @click="handleAudit(item, true)">
  60. <Icon icon="ep:select" />
  61. 通过
  62. </el-button>
  63. <el-button type="danger" @click="handleAudit(item, false)">
  64. <Icon icon="ep:close" />
  65. 不通过
  66. </el-button>
  67. <el-button type="primary" @click="openTaskUpdateAssigneeForm(item.id)">
  68. <Icon icon="ep:edit" />
  69. 转办
  70. </el-button>
  71. <el-button type="primary" @click="handleDelegate(item)">
  72. <Icon icon="ep:position" />
  73. 委派
  74. </el-button>
  75. <el-button type="primary" @click="handleSign(item)">
  76. <Icon icon="ep:plus" />
  77. 加签
  78. </el-button>
  79. <el-button type="warning" @click="handleBack(item)">
  80. <Icon icon="ep:back" />
  81. 回退
  82. </el-button>
  83. </div>
  84. </el-col>
  85. </el-card>
  86. <!-- 申请信息 -->
  87. <el-card v-loading="processInstanceLoading" class="box-card">
  88. <template #header>
  89. <span class="el-icon-document">申请信息【{{ processInstance.name }}】</span>
  90. </template>
  91. <!-- 情况一:流程表单 -->
  92. <el-col v-if="processInstance?.processDefinition?.formType === 10" :offset="6" :span="16">
  93. <form-create
  94. ref="fApi"
  95. v-model="detailForm.value"
  96. :option="detailForm.option"
  97. :rule="detailForm.rule"
  98. />
  99. </el-col>
  100. <!-- 情况二:业务表单 -->
  101. <div v-if="processInstance?.processDefinition?.formType === 20">
  102. <BusinessFormComponent :id="processInstance.businessKey" />
  103. </div>
  104. </el-card>
  105. <!-- 审批记录 -->
  106. <ProcessInstanceTaskList
  107. :loading="tasksLoad"
  108. :process-instance="processInstance"
  109. :tasks="tasks"
  110. @refresh="getTaskList"
  111. />
  112. <!-- 高亮流程图 -->
  113. <ProcessInstanceBpmnViewer
  114. :id="`${id}`"
  115. :bpmn-xml="bpmnXml"
  116. :loading="processInstanceLoading"
  117. :process-instance="processInstance"
  118. :tasks="tasks"
  119. />
  120. <!-- 弹窗:转派审批人 -->
  121. <TaskTransferForm ref="taskTransferFormRef" @success="getDetail" />
  122. <!-- 弹窗:回退节点 -->
  123. <TaskReturnForm ref="taskReturnFormRef" @success="getDetail" />
  124. <!-- 弹窗:委派,将任务委派给别人处理,处理完成后,会重新回到原审批人手中-->
  125. <TaskDelegateForm ref="taskDelegateForm" @success="getDetail" />
  126. <!-- 弹窗:加签,当前任务审批人为A,向前加签选了一个C,则需要C先审批,然后再是A审批,向后加签B,A审批完,需要B再审批完,才算完成这个任务节点 -->
  127. <TaskSignCreateForm ref="taskSignCreateFormRef" @success="getDetail" />
  128. </ContentWrap>
  129. </template>
  130. <script lang="ts" setup>
  131. import { useUserStore } from '@/store/modules/user'
  132. import { setConfAndFields2 } from '@/utils/formCreate'
  133. import type { ApiAttrs } from '@form-create/element-ui/types/config'
  134. import * as DefinitionApi from '@/api/bpm/definition'
  135. import * as ProcessInstanceApi from '@/api/bpm/processInstance'
  136. import * as TaskApi from '@/api/bpm/task'
  137. import ProcessInstanceBpmnViewer from './ProcessInstanceBpmnViewer.vue'
  138. import ProcessInstanceTaskList from './ProcessInstanceTaskList.vue'
  139. import TaskReturnForm from './dialog/TaskReturnForm.vue'
  140. import TaskDelegateForm from './dialog/TaskDelegateForm.vue'
  141. import TaskTransferForm from './dialog/TaskTransferForm.vue'
  142. import TaskSignCreateForm from './dialog/TaskSignCreateForm.vue'
  143. import { registerComponent } from '@/utils/routerHelper'
  144. import { isEmpty } from '@/utils/is'
  145. import * as UserApi from '@/api/system/user'
  146. defineOptions({ name: 'BpmProcessInstanceDetail' })
  147. const { query } = useRoute() // 查询参数
  148. const message = useMessage() // 消息弹窗
  149. const { proxy } = getCurrentInstance() as any
  150. const userId = useUserStore().getUser.id // 当前登录的编号
  151. const id = query.id as unknown as string // 流程实例的编号
  152. const processInstanceLoading = ref(false) // 流程实例的加载中
  153. const processInstance = ref<any>({}) // 流程实例
  154. const bpmnXml = ref('') // BPMN XML
  155. const tasksLoad = ref(true) // 任务的加载中
  156. const tasks = ref<any[]>([]) // 任务列表
  157. // ========== 审批信息 ==========
  158. const runningTasks = ref<any[]>([]) // 运行中的任务
  159. const auditForms = ref<any[]>([]) // 审批任务的表单
  160. const auditRule = reactive({
  161. reason: [{ required: true, message: '审批建议不能为空', trigger: 'blur' }]
  162. })
  163. const approveForms = ref<any[]>([]) // 审批通过时,额外的补充信息
  164. const approveFormFApis = ref<ApiAttrs[]>([]) // approveForms 的 fAPi
  165. // ========== 申请信息 ==========
  166. const fApi = ref<ApiAttrs>() //
  167. const detailForm = ref({
  168. rule: [],
  169. option: {},
  170. value: {}
  171. }) // 流程实例的表单详情
  172. /** 监听 approveFormFApis,实现它对应的 form-create 初始化后,隐藏掉对应的表单提交按钮 */
  173. watch(
  174. () => approveFormFApis.value,
  175. (value) => {
  176. value?.forEach((api) => {
  177. api.btn.show(false)
  178. api.resetBtn.show(false)
  179. })
  180. },
  181. {
  182. deep: true
  183. }
  184. )
  185. /** 处理审批通过和不通过的操作 */
  186. const handleAudit = async (task, pass) => {
  187. // 1.1 获得对应表单
  188. const index = runningTasks.value.indexOf(task)
  189. const auditFormRef = proxy.$refs['form' + index][0]
  190. // 1.2 校验表单
  191. const elForm = unref(auditFormRef)
  192. if (!elForm) return
  193. const valid = await elForm.validate()
  194. if (!valid) return
  195. // 2.1 提交审批
  196. const data = {
  197. id: task.id,
  198. reason: auditForms.value[index].reason,
  199. copyUserIds: auditForms.value[index].copyUserIds
  200. }
  201. if (pass) {
  202. // 审批通过,并且有额外的 approveForm 表单,需要校验 + 拼接到 data 表单里提交
  203. const formCreateApi = approveFormFApis.value[index]
  204. if (formCreateApi) {
  205. await formCreateApi.validate()
  206. data.variables = approveForms.value[index].value
  207. }
  208. await TaskApi.approveTask(data)
  209. message.success('审批通过成功')
  210. } else {
  211. await TaskApi.rejectTask(data)
  212. message.success('审批不通过成功')
  213. }
  214. // 2.2 加载最新数据
  215. getDetail()
  216. }
  217. /** 转派审批人 */
  218. const taskTransferFormRef = ref()
  219. const openTaskUpdateAssigneeForm = (id: string) => {
  220. taskTransferFormRef.value.open(id)
  221. }
  222. /** 处理审批退回的操作 */
  223. const taskDelegateForm = ref()
  224. const handleDelegate = async (task) => {
  225. taskDelegateForm.value.open(task.id)
  226. }
  227. /** 处理审批退回的操作 */
  228. const taskReturnFormRef = ref()
  229. const handleBack = async (task: any) => {
  230. taskReturnFormRef.value.open(task.id)
  231. }
  232. /** 处理审批加签的操作 */
  233. const taskSignCreateFormRef = ref()
  234. const handleSign = async (task: any) => {
  235. taskSignCreateFormRef.value.open(task.id)
  236. }
  237. /** 获得详情 */
  238. const getDetail = () => {
  239. // 1. 获得流程实例相关
  240. getProcessInstance()
  241. // 2. 获得流程任务列表(审批记录)
  242. getTaskList()
  243. }
  244. /** 加载流程实例 */
  245. const BusinessFormComponent = ref(null) // 异步组件
  246. const getProcessInstance = async () => {
  247. try {
  248. processInstanceLoading.value = true
  249. const data = await ProcessInstanceApi.getProcessInstance(id)
  250. if (!data) {
  251. message.error('查询不到流程信息!')
  252. return
  253. }
  254. processInstance.value = data
  255. // 设置表单信息
  256. const processDefinition = data.processDefinition
  257. if (processDefinition.formType === 10) {
  258. setConfAndFields2(
  259. detailForm,
  260. processDefinition.formConf,
  261. processDefinition.formFields,
  262. data.formVariables
  263. )
  264. nextTick().then(() => {
  265. fApi.value?.fapi?.btn.show(false)
  266. fApi.value?.fapi?.resetBtn.show(false)
  267. fApi.value?.fapi?.disabled(true)
  268. })
  269. } else {
  270. // 注意:data.processDefinition.formCustomViewPath 是组件的全路径,例如说:/crm/contract/detail/index.vue
  271. BusinessFormComponent.value = registerComponent(data.processDefinition.formCustomViewPath)
  272. }
  273. // 加载流程图
  274. bpmnXml.value = (
  275. await DefinitionApi.getProcessDefinition(processDefinition.id as number)
  276. )?.bpmnXml
  277. } finally {
  278. processInstanceLoading.value = false
  279. }
  280. }
  281. /** 加载任务列表 */
  282. const getTaskList = async () => {
  283. runningTasks.value = []
  284. auditForms.value = []
  285. approveForms.value = []
  286. approveFormFApis.value = []
  287. try {
  288. // 获得未取消的任务
  289. tasksLoad.value = true
  290. const data = await TaskApi.getTaskListByProcessInstanceId(id)
  291. tasks.value = []
  292. // 1.1 移除已取消的审批
  293. data.forEach((task) => {
  294. if (task.status !== 4) {
  295. tasks.value.push(task)
  296. }
  297. })
  298. // 1.2 排序,将未完成的排在前面,已完成的排在后面;
  299. tasks.value.sort((a, b) => {
  300. // 有已完成的情况,按照完成时间倒序
  301. if (a.endTime && b.endTime) {
  302. return b.endTime - a.endTime
  303. } else if (a.endTime) {
  304. return 1
  305. } else if (b.endTime) {
  306. return -1
  307. // 都是未完成,按照创建时间倒序
  308. } else {
  309. return b.createTime - a.createTime
  310. }
  311. })
  312. // 获得需要自己审批的任务
  313. loadRunningTask(tasks.value)
  314. } finally {
  315. tasksLoad.value = false
  316. }
  317. }
  318. /**
  319. * 设置 runningTasks 中的任务
  320. */
  321. const loadRunningTask = (tasks) => {
  322. tasks.forEach((task) => {
  323. if (!isEmpty(task.children)) {
  324. loadRunningTask(task.children)
  325. }
  326. // 2.1 只有待处理才需要
  327. if (task.status !== 1 && task.status !== 6) {
  328. return
  329. }
  330. // 2.2 自己不是处理人
  331. if (!task.assigneeUser || task.assigneeUser.id !== userId) {
  332. return
  333. }
  334. // 2.3 添加到处理任务
  335. runningTasks.value.push({ ...task })
  336. auditForms.value.push({
  337. reason: '',
  338. copyUserIds: []
  339. })
  340. // 2.4 处理 approve 表单
  341. if (task.formId && task.formConf) {
  342. const approveForm = {}
  343. setConfAndFields2(approveForm, task.formConf, task.formFields, task.formVariable)
  344. approveForms.value.push(approveForm)
  345. } else {
  346. approveForms.value.push({}) // 占位,避免为空
  347. }
  348. })
  349. }
  350. /** 初始化 */
  351. const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
  352. onMounted(async () => {
  353. getDetail()
  354. // 获得用户列表
  355. userOptions.value = await UserApi.getSimpleUserList()
  356. })
  357. </script>