1.简介
在案例1的基础上进行完善,案例1,点击打开。
2. 案例2
2.1 对比案例1,增加的内容(后台IDEA)
查看当前流程图,当前任务节点标红,返回到前台进行展示。
ShowCurrentView
package com.yb.controller;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @author [email protected]
* @version 1.0
* @date 2020/8/7
*/
@RestController
@RequestMapping("/showCurrentView")
public class ShowCurrentView {
@Resource
private TaskService taskService;
@Resource
private RepositoryService repositoryService;
@Resource
private RuntimeService runtimeService;
@Resource
private ProcessEngine processEngine;
@GetMapping("/{id}")
public void showCurrentView(@PathVariable("id") String taskId,
HttpServletResponse response){
Task task = taskService.createTaskQuery()
.taskId(taskId) //根据任务id查询
.singleResult();
//获取流程实例id
String processInstanceId = task.getProcessInstanceId();
// 查询流程实例
ProcessInstance pi = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
// 查询流程实例
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(pi.getProcessDefinitionId()).singleResult();
// 获取BPMN模型对象
BpmnModel model = repositoryService.getBpmnModel(pd.getId());
// 定义使用宋体
String fontName = "宋体";
// 获取流程实实例当前点的节点,需要高亮显示
List currentActs = runtimeService.getActiveActivityIds(pi.getId());
// BPMN模型对象、图片类型、显示的节点
InputStream is = processEngine
.getProcessEngineConfiguration()
.getProcessDiagramGenerator()
.generateDiagram(model, "png", currentActs, new ArrayList(),
fontName, fontName, fontName, null, 1.0);
OutputStream out = null;
try {
response.setContentType("multipart/form-data;charset=utf8");
response.setContentType("image/png");
out = response.getOutputStream();
out.write(IOUtils.toByteArray(is));
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (Exception e) {
}
}
}
}
增加了在审批时查看任务信息
ApprovalServiceImpl
/**
* 查询当前用户待办任务+回显需要审批的任务信息
* @param name
* @return
*/
@Override
public BaseResult searchApprovalByName(String name) {
List<ActRuTask> list = actRuTaskDao.findByAssignee(name);
if(list.size()>0){
//查询当前待办任务时,获取请假单信息
Leave leave = new Leave();
for (ActRuTask actRuTask : list) {
String procInstId = actRuTask.getProcInstId();
Map<String, Object> variables = runtimeService.getVariables(procInstId);
//inputUser是办理请假单任务的人,获取到的信息存进请假单业务类中返回
for (Map.Entry<String, Object> entry : variables.entrySet()) {
if(entry.getKey().equals("inputUser")){
leave.setName((String) entry.getValue());
}else {
Object value = entry.getValue();
leave.setDays((Integer) value);
}
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
return BaseResult.ok("查询成功",list).append("leave",leave);
}else {
List<ActRuIdentitylink> byUserId = actRuIdentitylinkDao.findByUserId(name);
if (byUserId.size()>0) {
for (ActRuIdentitylink actRuIdentitylink : byUserId) {
if (actRuIdentitylink.getTaskId() != null) {
taskService.claim(actRuIdentitylink.getTaskId(), actRuIdentitylink.getUserId());
List<ActRuTask> byAssignee = actRuTaskDao.findByAssignee(name);
return BaseResult.ok("查询成功", byAssignee);
}
}
}
}
return BaseResult.ok("暂时没有您的任务");
}
增加了审批通过时更改申请人审批状态
/**
* 审批-多人审批
* @param approval
* @return
*/
@Override
public BaseResult Approval(Approval approval) {
List<ActRuTask> byAssignee = actRuTaskDao.findByAssignee(approval.getName());
if(byAssignee.size()>0){
for (ActRuTask actRuTask : byAssignee) {
if(approval.getOpinion().equals("同意")){
String procInstId = actRuTask.getProcInstId();
System.out.println(procInstId);
Map<String, Object> variables = runtimeService.getVariables(procInstId);
for (Map.Entry<String, Object> entry : variables.entrySet()) {
if(entry.getKey().equals("inputUser")){
String value = (String) entry.getValue();
System.out.println("申请人"+value);
//已审批通过更改申请人状态
Integer i = leaveRepository.updateStateByName(value);
}
}
taskService.complete(actRuTask.getId());
return BaseResult.ok("已审批");
}else {
return BaseResult.error("该流程未通过审批,已通知该员工");
}
}
}
List<ActRuIdentitylink> byUserId = actRuIdentitylinkDao.findByUserId(approval.getName());
if(byUserId.size()>0){
for (ActRuIdentitylink actRuIdentitylink : byUserId) {
if(actRuIdentitylink.getTaskId()!=null){
taskService.claim(actRuIdentitylink.getTaskId(),actRuIdentitylink.getUserId());
if(approval.getOpinion().equals("同意")){
String procInstId = actRuIdentitylink.getProcInstId();
Map<String, Object> variables = runtimeService.getVariables(procInstId);
//inputUser是办理请假单任务的人,获取到的信息存进请假单业务类中返回
for (Map.Entry<String, Object> entry : variables.entrySet()) {
if(entry.getKey().equals("inputUser")){
String value = (String) entry.getValue();
//已审批通过更改申请人状态
Integer i = leaveRepository.updateStateByName(value);
}
}
taskService.complete(actRuIdentitylink.getTaskId());
return BaseResult.ok("已审批");
}else {
return BaseResult.error("该流程未通过审批,已通知该员工");
}
}
}
}
return BaseResult.error("该流程未通过审批,已通知该员工");
}
2.2 对比案例1,增加的内容(前台VUE)
增加查看当前流程图
前台传递任务id到后端,接收后端传递的二进制流程图数据,对数据进行处理并展示
//查看流程图
searchFlowChartFn(id){
this.dialogFormVisibleImg=true
axios.get('http://localhost:9090/showCurrentView/'+id,{
responseType: 'arraybuffer' //这里是声明期望返回的数据类型
}).then(response => {
//这里的第一次处理后台返回来的二进制留数据 转化为base64
//这里的data数据是后台返回来的,这里的key是params中的键值(byte)
return 'data:image/png;base64,' + btoa(
new Uint8Array(response.data).reduce((data, key) => data + String.fromCharCode(key), '')
)
}).then( data =>{
//这一次箭头函数是依赖于第一次.then函数处理的data值
console.log(data)
this.imgsrc=data
})
},
展示:
增加审批时回显任务信息
效果
增加了审批通过时更改申请人审批状态,效果:
说明:
相对案例1,点击打开,案例2,增加了查看流程图,审批通过更改申请人状态,不同的人登录查看不同的信息,例如:员工登录可以查看请假的申请状态,项目经理登录不但可以看到自己的请假信息,还可以做自己应有的待办任务。