index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 :loading="tasksLoad" :tasks="tasks" @refresh="getTaskList" />
  107. <!-- 高亮流程图 -->
  108. <ProcessInstanceBpmnViewer
  109. :id="`${id}`"
  110. :bpmn-xml="bpmnXML"
  111. :loading="processInstanceLoading"
  112. :process-instance="processInstance"
  113. :tasks="tasks"
  114. />
  115. <!-- 弹窗:转派审批人 -->
  116. <TaskTransferForm ref="taskTransferFormRef" @success="getDetail" />
  117. <!-- 弹窗:回退节点 -->
  118. <TaskReturnForm ref="taskReturnFormRef" @success="getDetail" />
  119. <!-- 弹窗:委派,将任务委派给别人处理,处理完成后,会重新回到原审批人手中-->
  120. <TaskDelegateForm ref="taskDelegateForm" @success="getDetail" />
  121. <!-- 弹窗:加签,当前任务审批人为A,向前加签选了一个C,则需要C先审批,然后再是A审批,向后加签B,A审批完,需要B再审批完,才算完成这个任务节点 -->
  122. <TaskSignCreateForm ref="taskSignCreateFormRef" @success="getDetail" />
  123. </ContentWrap>
  124. </template>
  125. <script lang="ts" setup>
  126. import { useUserStore } from '@/store/modules/user'
  127. import { setConfAndFields2 } from '@/utils/formCreate'
  128. import type { ApiAttrs } from '@form-create/element-ui/types/config'
  129. import * as DefinitionApi from '@/api/bpm/definition'
  130. import * as ProcessInstanceApi from '@/api/bpm/processInstance'
  131. import * as TaskApi from '@/api/bpm/task'
  132. import ProcessInstanceBpmnViewer from './ProcessInstanceBpmnViewer.vue'
  133. import ProcessInstanceTaskList from './ProcessInstanceTaskList.vue'
  134. import TaskReturnForm from './dialog/TaskReturnForm.vue'
  135. import TaskDelegateForm from './dialog/TaskDelegateForm.vue'
  136. import TaskTransferForm from './dialog/TaskTransferForm.vue'
  137. import TaskSignCreateForm from './dialog/TaskSignCreateForm.vue'
  138. import { registerComponent } from '@/utils/routerHelper'
  139. import { isEmpty } from '@/utils/is'
  140. import * as UserApi from '@/api/system/user'
  141. defineOptions({ name: 'BpmProcessInstanceDetail' })
  142. const { query } = useRoute() // 查询参数
  143. const message = useMessage() // 消息弹窗
  144. const { proxy } = getCurrentInstance() as any
  145. const userId = useUserStore().getUser.id // 当前登录的编号
  146. const id = query.id as unknown as string // 流程实例的编号
  147. const processInstanceLoading = ref(false) // 流程实例的加载中
  148. const processInstance = ref<any>({}) // 流程实例
  149. const bpmnXML = ref('') // BPMN XML
  150. const tasksLoad = ref(true) // 任务的加载中
  151. const tasks = ref<any[]>([]) // 任务列表
  152. // ========== 审批信息 ==========
  153. const runningTasks = ref<any[]>([]) // 运行中的任务
  154. const auditForms = ref<any[]>([]) // 审批任务的表单
  155. const auditRule = reactive({
  156. reason: [{ required: true, message: '审批建议不能为空', trigger: 'blur' }]
  157. })
  158. const approveForms = ref<any[]>([]) // 审批通过时,额外的补充信息
  159. const approveFormFApis = ref<ApiAttrs[]>([]) // approveForms 的 fAPi
  160. // ========== 申请信息 ==========
  161. const fApi = ref<ApiAttrs>() //
  162. const detailForm = ref({
  163. // 流程表单详情
  164. rule: [],
  165. option: {},
  166. value: {}
  167. })
  168. /** 监听 approveFormFApis,实现它对应的 form-create 初始化后,隐藏掉对应的表单提交按钮 */
  169. watch(
  170. () => approveFormFApis.value,
  171. (value) => {
  172. value?.forEach((api) => {
  173. api.btn.show(false)
  174. api.resetBtn.show(false)
  175. })
  176. },
  177. {
  178. deep: true
  179. }
  180. )
  181. /** 处理审批通过和不通过的操作 */
  182. const handleAudit = async (task, pass) => {
  183. // 1.1 获得对应表单
  184. const index = runningTasks.value.indexOf(task)
  185. const auditFormRef = proxy.$refs['form' + index][0]
  186. // 1.2 校验表单
  187. const elForm = unref(auditFormRef)
  188. if (!elForm) return
  189. const valid = await elForm.validate()
  190. if (!valid) return
  191. // 2.1 提交审批
  192. const data = {
  193. id: task.id,
  194. reason: auditForms.value[index].reason,
  195. copyUserIds: auditForms.value[index].copyUserIds
  196. }
  197. if (pass) {
  198. // 审批通过,并且有额外的 approveForm 表单,需要校验 + 拼接到 data 表单里提交
  199. const formCreateApi = approveFormFApis.value[index]
  200. if (formCreateApi) {
  201. await formCreateApi.validate()
  202. data.variables = approveForms.value[index].value
  203. }
  204. await TaskApi.approveTask(data)
  205. message.success('审批通过成功')
  206. } else {
  207. await TaskApi.rejectTask(data)
  208. message.success('审批不通过成功')
  209. }
  210. // 2.2 加载最新数据
  211. getDetail()
  212. }
  213. /** 转派审批人 */
  214. const taskTransferFormRef = ref()
  215. const openTaskUpdateAssigneeForm = (id: string) => {
  216. taskTransferFormRef.value.open(id)
  217. }
  218. /** 处理审批退回的操作 */
  219. const taskDelegateForm = ref()
  220. const handleDelegate = async (task) => {
  221. taskDelegateForm.value.open(task.id)
  222. }
  223. /** 处理审批退回的操作 */
  224. const taskReturnFormRef = ref()
  225. const handleBack = async (task: any) => {
  226. taskReturnFormRef.value.open(task.id)
  227. }
  228. /** 处理审批加签的操作 */
  229. const taskSignCreateFormRef = ref()
  230. const handleSign = async (task: any) => {
  231. taskSignCreateFormRef.value.open(task.id)
  232. }
  233. /** 获得详情 */
  234. const getDetail = () => {
  235. // 1. 获得流程实例相关
  236. getProcessInstance()
  237. // 2. 获得流程任务列表(审批记录)
  238. getTaskList()
  239. }
  240. /** 加载流程实例 */
  241. const BusinessFormComponent = ref(null) // 异步组件
  242. const getProcessInstance = async () => {
  243. try {
  244. processInstanceLoading.value = true
  245. const data = await ProcessInstanceApi.getProcessInstance(id)
  246. if (!data) {
  247. message.error('查询不到流程信息!')
  248. return
  249. }
  250. processInstance.value = data
  251. // 设置表单信息
  252. const processDefinition = data.processDefinition
  253. if (processDefinition.formType === 10) {
  254. setConfAndFields2(
  255. detailForm,
  256. processDefinition.formConf,
  257. processDefinition.formFields,
  258. data.formVariables
  259. )
  260. nextTick().then(() => {
  261. fApi.value?.fapi?.btn.show(false)
  262. fApi.value?.fapi?.resetBtn.show(false)
  263. fApi.value?.fapi?.disabled(true)
  264. })
  265. } else {
  266. // 注意:data.processDefinition.formCustomViewPath 是组件的全路径,例如说:/crm/contract/detail/index.vue
  267. BusinessFormComponent.value = registerComponent(data.processDefinition.formCustomViewPath)
  268. }
  269. // 加载流程图
  270. bpmnXML.value = await DefinitionApi.getProcessDefinitionBpmnXML(processDefinition.id as number)
  271. } finally {
  272. processInstanceLoading.value = false
  273. }
  274. }
  275. /** 加载任务列表 */
  276. const getTaskList = async () => {
  277. runningTasks.value = []
  278. auditForms.value = []
  279. approveForms.value = []
  280. approveFormFApis.value = []
  281. try {
  282. // 获得未取消的任务
  283. tasksLoad.value = true
  284. const data = await TaskApi.getTaskListByProcessInstanceId(id)
  285. tasks.value = []
  286. // 1.1 移除已取消的审批
  287. data.forEach((task) => {
  288. if (task.status !== 4) {
  289. tasks.value.push(task)
  290. }
  291. })
  292. // 1.2 排序,将未完成的排在前面,已完成的排在后面;
  293. tasks.value.sort((a, b) => {
  294. // 有已完成的情况,按照完成时间倒序
  295. if (a.endTime && b.endTime) {
  296. return b.endTime - a.endTime
  297. } else if (a.endTime) {
  298. return 1
  299. } else if (b.endTime) {
  300. return -1
  301. // 都是未完成,按照创建时间倒序
  302. } else {
  303. return b.createTime - a.createTime
  304. }
  305. })
  306. // 获得需要自己审批的任务
  307. loadRunningTask(tasks.value)
  308. } finally {
  309. tasksLoad.value = false
  310. }
  311. }
  312. /**
  313. * 设置 runningTasks 中的任务
  314. */
  315. const loadRunningTask = (tasks) => {
  316. tasks.forEach((task) => {
  317. if (!isEmpty(task.children)) {
  318. loadRunningTask(task.children)
  319. }
  320. // 2.1 只有待处理才需要
  321. if (task.status !== 1 && task.status !== 6) {
  322. return
  323. }
  324. // 2.2 自己不是处理人
  325. if (!task.assigneeUser || task.assigneeUser.id !== userId) {
  326. return
  327. }
  328. // 2.3 添加到处理任务
  329. runningTasks.value.push({ ...task })
  330. auditForms.value.push({
  331. reason: '',
  332. copyUserIds: []
  333. })
  334. // 2.4 处理 approve 表单
  335. if (task.formId && task.formConf) {
  336. const approveForm = {}
  337. setConfAndFields2(approveForm, task.formConf, task.formFields, task.formVariable)
  338. approveForms.value.push(approveForm)
  339. } else {
  340. approveForms.value.push({}) // 占位,避免为空
  341. }
  342. })
  343. }
  344. /** 初始化 */
  345. const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
  346. onMounted(async () => {
  347. getDetail()
  348. // 获得用户列表
  349. userOptions.value = await UserApi.getSimpleUserList()
  350. })
  351. </script>