index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 v-if="runningTasks[index].formId > 0" class="mb-15px !-mt-10px">
  28. <template #header>
  29. <span class="el-icon-picture-outline">
  30. 填写表单【{{ runningTasks[index]?.formName }}】
  31. </span>
  32. </template>
  33. <form-create
  34. v-model="approveForms[index].value"
  35. v-model:api="approveFormFApis[index]"
  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="itemx in userOptions"
  51. :key="itemx.id"
  52. :label="itemx.nickname"
  53. :value="itemx.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. <!-- TODO @jason:建议搞个 if 来判断,替代现有的 !item.buttonsSetting || item.buttonsSetting[OpsButtonType.APPROVE]?.enable -->
  60. <el-button
  61. type="success"
  62. v-if="!item.buttonsSetting || item.buttonsSetting[OperationButtonType.APPROVE]?.enable"
  63. @click="handleAudit(item, true)"
  64. >
  65. <Icon icon="ep:select" />
  66. <!-- TODO @jason:这个也是类似哈,搞个方法来生成名字 -->
  67. {{
  68. item.buttonsSetting?.[OperationButtonType.APPROVE]?.displayName ||
  69. OPERATION_BUTTON_NAME.get(OperationButtonType.APPROVE)
  70. }}
  71. </el-button>
  72. <el-button
  73. v-if="!item.buttonsSetting || item.buttonsSetting[OperationButtonType.REJECT]?.enable"
  74. type="danger"
  75. @click="handleAudit(item, false)"
  76. >
  77. <Icon icon="ep:close" />
  78. {{
  79. item.buttonsSetting?.[OperationButtonType.REJECT].displayName ||
  80. OPERATION_BUTTON_NAME.get(OperationButtonType.REJECT)
  81. }}
  82. </el-button>
  83. <el-button
  84. v-if="!item.buttonsSetting || item.buttonsSetting[OperationButtonType.TRANSFER]?.enable"
  85. type="primary"
  86. @click="openTaskUpdateAssigneeForm(item.id)"
  87. >
  88. <Icon icon="ep:edit" />
  89. {{
  90. item.buttonsSetting?.[OperationButtonType.TRANSFER]?.displayName ||
  91. OPERATION_BUTTON_NAME.get(OperationButtonType.TRANSFER)
  92. }}
  93. </el-button>
  94. <el-button
  95. v-if="!item.buttonsSetting || item.buttonsSetting[OperationButtonType.DELEGATE]?.enable"
  96. type="primary"
  97. @click="handleDelegate(item)"
  98. >
  99. <Icon icon="ep:position" />
  100. {{
  101. item.buttonsSetting?.[OperationButtonType.DELEGATE]?.displayName ||
  102. OPERATION_BUTTON_NAME.get(OperationButtonType.DELEGATE)
  103. }}
  104. </el-button>
  105. <el-button
  106. v-if="!item.buttonsSetting || item.buttonsSetting[OperationButtonType.ADD_SIGN]?.enable"
  107. type="primary"
  108. @click="handleSign(item)"
  109. >
  110. <Icon icon="ep:plus" />
  111. {{
  112. item.buttonsSetting?.[OperationButtonType.ADD_SIGN]?.displayName ||
  113. OPERATION_BUTTON_NAME.get(OperationButtonType.ADD_SIGN)
  114. }}
  115. </el-button>
  116. <el-button
  117. v-if="!item.buttonsSetting || item.buttonsSetting[OperationButtonType.RETURN]?.enable"
  118. type="warning"
  119. @click="handleBack(item)"
  120. >
  121. <Icon icon="ep:back" />
  122. {{
  123. item.buttonsSetting?.[OperationButtonType.RETURN]?.displayName ||
  124. OPERATION_BUTTON_NAME.get(OperationButtonType.RETURN)
  125. }}
  126. </el-button>
  127. </div>
  128. </el-col>
  129. </el-card>
  130. <!-- 申请信息 -->
  131. <el-card v-loading="processInstanceLoading" class="box-card">
  132. <template #header>
  133. <span class="el-icon-document">申请信息【{{ processInstance.name }}】</span>
  134. </template>
  135. <!-- 情况一:流程表单 -->
  136. <el-col v-if="processInstance?.processDefinition?.formType === 10" :offset="6" :span="16">
  137. <form-create
  138. v-model="detailForm.value"
  139. v-model:api="fApi"
  140. :option="detailForm.option"
  141. :rule="detailForm.rule"
  142. />
  143. </el-col>
  144. <!-- 情况二:业务表单 -->
  145. <div v-if="processInstance?.processDefinition?.formType === 20">
  146. <BusinessFormComponent :id="processInstance.businessKey" />
  147. </div>
  148. </el-card>
  149. <!-- 审批记录 -->
  150. <ProcessInstanceTaskList
  151. :loading="tasksLoad"
  152. :process-instance="processInstance"
  153. :tasks="tasks"
  154. @refresh="getTaskList"
  155. />
  156. <!-- 高亮流程图 -->
  157. <ProcessInstanceBpmnViewer
  158. :id="`${id}`"
  159. :bpmn-xml="bpmnXml"
  160. :loading="processInstanceLoading"
  161. :process-instance="processInstance"
  162. :tasks="tasks"
  163. />
  164. <!-- 弹窗:转派审批人 -->
  165. <TaskTransferForm ref="taskTransferFormRef" @success="getDetail" />
  166. <!-- 弹窗:回退节点 -->
  167. <TaskReturnForm ref="taskReturnFormRef" @success="getDetail" />
  168. <!-- 弹窗:委派,将任务委派给别人处理,处理完成后,会重新回到原审批人手中-->
  169. <TaskDelegateForm ref="taskDelegateForm" @success="getDetail" />
  170. <!-- 弹窗:加签,当前任务审批人为A,向前加签选了一个C,则需要C先审批,然后再是A审批,向后加签B,A审批完,需要B再审批完,才算完成这个任务节点 -->
  171. <TaskSignCreateForm ref="taskSignCreateFormRef" @success="getDetail" />
  172. </ContentWrap>
  173. </template>
  174. <script lang="ts" setup>
  175. import { useUserStore } from '@/store/modules/user'
  176. import { setConfAndFields2 } from '@/utils/formCreate'
  177. import type { ApiAttrs } from '@form-create/element-ui/types/config'
  178. import * as DefinitionApi from '@/api/bpm/definition'
  179. import * as ProcessInstanceApi from '@/api/bpm/processInstance'
  180. import * as TaskApi from '@/api/bpm/task'
  181. import ProcessInstanceBpmnViewer from './ProcessInstanceBpmnViewer.vue'
  182. import ProcessInstanceTaskList from './ProcessInstanceTaskList.vue'
  183. import TaskReturnForm from './dialog/TaskReturnForm.vue'
  184. import TaskDelegateForm from './dialog/TaskDelegateForm.vue'
  185. import TaskTransferForm from './dialog/TaskTransferForm.vue'
  186. import TaskSignCreateForm from './dialog/TaskSignCreateForm.vue'
  187. import { registerComponent } from '@/utils/routerHelper'
  188. import { isEmpty } from '@/utils/is'
  189. import * as UserApi from '@/api/system/user'
  190. import {
  191. OperationButtonType,
  192. OPERATION_BUTTON_NAME
  193. } from '@/components/SimpleProcessDesignerV2/src/consts'
  194. defineOptions({ name: 'BpmProcessInstanceDetail' })
  195. const { query } = useRoute() // 查询参数
  196. const message = useMessage() // 消息弹窗
  197. const { proxy } = getCurrentInstance() as any
  198. const userId = useUserStore().getUser.id // 当前登录的编号
  199. const id = query.id as unknown as string // 流程实例的编号
  200. const processInstanceLoading = ref(false) // 流程实例的加载中
  201. const processInstance = ref<any>({}) // 流程实例
  202. const bpmnXml = ref('') // BPMN XML
  203. const tasksLoad = ref(true) // 任务的加载中
  204. const tasks = ref<any[]>([]) // 任务列表
  205. // ========== 审批信息 ==========
  206. const runningTasks = ref<any[]>([]) // 运行中的任务
  207. const auditForms = ref<any[]>([]) // 审批任务的表单
  208. const auditRule = reactive({
  209. reason: [{ required: true, message: '审批建议不能为空', trigger: 'blur' }]
  210. })
  211. const approveForms = ref<any[]>([]) // 审批通过时,额外的补充信息
  212. const approveFormFApis = ref<ApiAttrs[]>([]) // approveForms 的 fAPi
  213. // ========== 申请信息 ==========
  214. const fApi = ref<ApiAttrs>() //
  215. const detailForm = ref({
  216. rule: [],
  217. option: {},
  218. value: {}
  219. }) // 流程实例的表单详情
  220. /** 监听 approveFormFApis,实现它对应的 form-create 初始化后,隐藏掉对应的表单提交按钮 */
  221. watch(
  222. () => approveFormFApis.value,
  223. (value) => {
  224. value?.forEach((api) => {
  225. api.btn.show(false)
  226. api.resetBtn.show(false)
  227. })
  228. },
  229. {
  230. deep: true
  231. }
  232. )
  233. /** 处理审批通过和不通过的操作 */
  234. const handleAudit = async (task, pass) => {
  235. // 1.1 获得对应表单
  236. const index = runningTasks.value.indexOf(task)
  237. const auditFormRef = proxy.$refs['form' + index][0]
  238. // 1.2 校验表单
  239. const elForm = unref(auditFormRef)
  240. if (!elForm) return
  241. let valid = await elForm.validate()
  242. if (!valid) return
  243. // 校验申请表单
  244. if (!fApi.value) return
  245. valid = await fApi.value.validate()
  246. if (!valid) return
  247. // 2.1 提交审批
  248. const data = {
  249. id: task.id,
  250. reason: auditForms.value[index].reason,
  251. copyUserIds: auditForms.value[index].copyUserIds
  252. }
  253. if (pass) {
  254. // 审批通过,并且有额外的 approveForm 表单,需要校验 + 拼接到 data 表单里提交
  255. const formCreateApi = approveFormFApis.value[index]
  256. if (formCreateApi) {
  257. await formCreateApi.validate()
  258. data.variables = approveForms.value[index].value
  259. }
  260. // 获取表单可编辑字段的值
  261. data.variables = getWritableValueOfForm(task.fieldsPermission)
  262. await TaskApi.approveTask(data)
  263. message.success('审批通过成功')
  264. } else {
  265. await TaskApi.rejectTask(data)
  266. message.success('审批不通过成功')
  267. }
  268. // 2.2 加载最新数据
  269. getDetail()
  270. }
  271. /** 转派审批人 */
  272. const taskTransferFormRef = ref()
  273. const openTaskUpdateAssigneeForm = (id: string) => {
  274. taskTransferFormRef.value.open(id)
  275. }
  276. /** 处理审批退回的操作 */
  277. const taskDelegateForm = ref()
  278. const handleDelegate = async (task) => {
  279. taskDelegateForm.value.open(task.id)
  280. }
  281. /** 处理审批退回的操作 */
  282. const taskReturnFormRef = ref()
  283. const handleBack = async (task: any) => {
  284. taskReturnFormRef.value.open(task.id)
  285. }
  286. /** 处理审批加签的操作 */
  287. const taskSignCreateFormRef = ref()
  288. const handleSign = async (task: any) => {
  289. taskSignCreateFormRef.value.open(task.id)
  290. }
  291. /** 获得详情 */
  292. const getDetail = async () => {
  293. // 1. 获得流程任务列表(审批记录)。 需要先获取任务,表单的权限设置需要根据任务来设置
  294. await getTaskList()
  295. // 2. 获得流程实例相关
  296. getProcessInstance()
  297. }
  298. /** 加载流程实例 */
  299. const BusinessFormComponent = ref(null) // 异步组件
  300. const getProcessInstance = async () => {
  301. try {
  302. processInstanceLoading.value = true
  303. const data = await ProcessInstanceApi.getProcessInstance(id)
  304. if (!data) {
  305. message.error('查询不到流程信息!')
  306. return
  307. }
  308. processInstance.value = data
  309. // 设置表单信息
  310. const processDefinition = data.processDefinition
  311. if (processDefinition.formType === 10) {
  312. if (detailForm.value.rule.length > 0) {
  313. detailForm.value.value = data.formVariables
  314. } else {
  315. setConfAndFields2(
  316. detailForm,
  317. processDefinition.formConf,
  318. processDefinition.formFields,
  319. data.formVariables
  320. )
  321. }
  322. nextTick().then(() => {
  323. fApi.value?.btn.show(false)
  324. fApi.value?.resetBtn.show(false)
  325. fApi.value?.disabled(true)
  326. // 设置表单权限。后续需要改造成。只处理一个运行中的任务
  327. if (runningTasks.value.length > 0) {
  328. const task = runningTasks.value.at(0)
  329. if (task.fieldsPermission) {
  330. Object.keys(task.fieldsPermission).forEach((item) => {
  331. setFieldPermission(item, task.fieldsPermission[item])
  332. })
  333. }
  334. }
  335. })
  336. } else {
  337. // 注意:data.processDefinition.formCustomViewPath 是组件的全路径,例如说:/crm/contract/detail/index.vue
  338. BusinessFormComponent.value = registerComponent(data.processDefinition.formCustomViewPath)
  339. }
  340. // 加载流程图
  341. bpmnXml.value = (
  342. await DefinitionApi.getProcessDefinition(processDefinition.id as number)
  343. )?.bpmnXml
  344. } finally {
  345. processInstanceLoading.value = false
  346. }
  347. }
  348. /** 加载任务列表 */
  349. const getTaskList = async () => {
  350. runningTasks.value = []
  351. auditForms.value = []
  352. approveForms.value = []
  353. approveFormFApis.value = []
  354. try {
  355. // 获得未取消的任务
  356. tasksLoad.value = true
  357. const data = await TaskApi.getTaskListByProcessInstanceId(id)
  358. tasks.value = []
  359. // 1.1 移除已取消的审批
  360. data.forEach((task) => {
  361. if (task.status !== 4) {
  362. tasks.value.push(task)
  363. }
  364. })
  365. // 1.2 排序,将未完成的排在前面,已完成的排在后面;
  366. tasks.value.sort((a, b) => {
  367. // 有已完成的情况,按照完成时间倒序
  368. if (a.endTime && b.endTime) {
  369. return b.endTime - a.endTime
  370. } else if (a.endTime) {
  371. return 1
  372. } else if (b.endTime) {
  373. return -1
  374. // 都是未完成,按照创建时间倒序
  375. } else {
  376. return b.createTime - a.createTime
  377. }
  378. })
  379. // 获得需要自己审批的任务
  380. loadRunningTask(tasks.value)
  381. } finally {
  382. tasksLoad.value = false
  383. }
  384. }
  385. /**
  386. * 设置 runningTasks 中的任务
  387. */
  388. const loadRunningTask = (tasks) => {
  389. tasks.forEach((task) => {
  390. if (!isEmpty(task.children)) {
  391. loadRunningTask(task.children)
  392. }
  393. // 2.1 只有待处理才需要
  394. if (task.status !== 1 && task.status !== 6) {
  395. return
  396. }
  397. // 2.2 自己不是处理人
  398. if (!task.assigneeUser || task.assigneeUser.id !== userId) {
  399. return
  400. }
  401. // 2.3 添加到处理任务
  402. runningTasks.value.push({ ...task })
  403. auditForms.value.push({
  404. reason: '',
  405. copyUserIds: []
  406. })
  407. // 2.4 处理 approve 表单
  408. if (task.formId && task.formConf) {
  409. const approveForm = {}
  410. setConfAndFields2(approveForm, task.formConf, task.formFields, task.formVariables)
  411. approveForms.value.push(approveForm)
  412. } else {
  413. approveForms.value.push({}) // 占位,避免为空
  414. }
  415. })
  416. }
  417. /**
  418. * 设置表单权限
  419. */
  420. const setFieldPermission = (field: string, permission: string) => {
  421. if (permission === '1') {
  422. fApi.value?.disabled(true, field)
  423. }
  424. if (permission === '2') {
  425. fApi.value?.disabled(false, field)
  426. }
  427. if (permission === '3') {
  428. fApi.value?.hidden(true, field)
  429. }
  430. }
  431. /**
  432. * 获取可以编辑字段的值
  433. */
  434. const getWritableValueOfForm = (fieldsPermission: Object) => {
  435. const fieldsValue = {}
  436. if (fieldsPermission && fApi.value) {
  437. Object.keys(fieldsPermission).forEach((item) => {
  438. if (fieldsPermission[item] === '2') {
  439. fieldsValue[item] = fApi.value.getValue(item)
  440. }
  441. })
  442. }
  443. return fieldsValue
  444. }
  445. /** 初始化 */
  446. const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
  447. onMounted(async () => {
  448. getDetail()
  449. // 获得用户列表
  450. userOptions.value = await UserApi.getSimpleUserList()
  451. })
  452. </script>