前言

activiti是目前比较流行的工作流框架,但是activiti学起来还是费劲,还是有点难度的,如何整合在线编辑器,如何和业务表单绑定,如何和系统权限绑定,这些问题都是要考虑到的,不是说纯粹的把activiti整合到系统中就完事了。如今国内比较流行的是前后端分离模式,之前都是前后端放一个工程里面,接口风格很乱,并且不好维护,前后端开发不是很方便。目前前端都做成了工程化的形式,比如国产的Vue,国外的React等等。为了顺应潮流,我就做了一套springboot-vue-activiti的快速开发小脚手架,对于大型项目来说还是不太适合,毕竟来说,一个人的开发能力有限,但是对于中小微型项目,并且带有审批业务的项目来说,那简直就是一个福音了。

一、效果展示

1.模型设计器

模型设计

2.流程节点设置

审批人员可以根据角色、部门、部门负责人、直接选择人员等。
在这里插入图片描述

3.审批进度查询

在这里插入图片描述

二、操作过程

1.引入activiti依赖

<!-- Activiti 启动器 -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-spring-boot-starter-basic</artifactId>
        <version>${activiti.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Activiti 流程图 -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-diagram-rest</artifactId>
        <version>${activiti.version}</version>
    </dependency>
    <!-- Activiti 在线设计 -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-modeler</artifactId>
        <version>${activiti.version}</version>
    </dependency>

2.编辑器代码及汉化

编辑器

3.application.yml配置

activiti:
    check-process-definitions: false
    #启用作业执行器
    async-executor-activate: false
    #启用异步执行器
    job-executor-activate: false    

4.数据库表

在这里插入图片描述

三、业务表单和系统权限绑定

1.思路

1.新建一张流程定义扩展表(用来存储流程部署的基本信息、关联业务表名、前端路由信息、业务表单类型等),流程部署完后往流程定义扩展表插入部署基本信息,然后编辑已发布的流程,往扩展表中插入本流程的分类,关联表单信息。
2.关于自定义节点设置,主要是用来定义每个节点由哪个角色,或者具体哪个人来审批。建立一张节点扩展表(存储节点流转信息,指定人ID、角色ID,等)。

2.部分代码

// 获取模型
        Model modelData = repositoryService.getModel(id);
        byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());
        if (bytes == null) {
            return Result.error("模型数据为空,请先成功设计流程并保存");
        }
        try {
            JsonNode modelNode = new ObjectMapper().readTree(bytes);
            BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
            if(model.getProcesses().size()==0){
                return Result.error("模型不符要求,请至少设计一条主线流程");
            }
            byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);
            // 部署发布模型流程
            String processName = modelData.getName() + ".bpmn20.xml";
            Deployment deployment = repositoryService.createDeployment()
                    .name(modelData.getName())
                    .addString(processName, new String(bpmnBytes, "UTF-8"))
                    .deploy();
            String metaInfo = modelData.getMetaInfo();
            JSONObject metaInfoMap = JSON.parseObject(metaInfo);
            // 设置流程分类,保存扩展流程至数据库
            List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).list();
            for (ProcessDefinition pd : list) {
                ActZprocess actZprocess = new ActZprocess();
                actZprocess.setId(pd.getId());
                actZprocess.setName(modelData.getName());
                actZprocess.setProcessKey(modelData.getKey());
                actZprocess.setDeploymentId(deployment.getId());
                actZprocess.setDescription(metaInfoMap.getString(ModelDataJsonConstants.MODEL_DESCRIPTION));
                actZprocess.setVersion(pd.getVersion());
                actZprocess.setDiagramName(pd.getDiagramResourceName());
                actZprocessService.setAllOldByProcessKey(modelData.getKey());
                actZprocess.setLatest(true);
                actZprocessService.save(actZprocess);
            }
        }catch (Exception e){
            String err = e.toString();
            log.error(e.getMessage(),e);
            if (err.indexOf("NCName")>-1){
                return Result.error("部署失败:流程设计中的流程名称不能为空,不能为数字以及特殊字符开头!");
            }
            if (err.indexOf("PRIMARY")>-1){
                return Result.error("部署失败:该模型已发布,key唯一!");
            }
            return Result.error("部署失败!");
        }
        return Result.ok("部署成功");
/*获取高亮实时流程图*/
    @RequestMapping(value = "/getHighlightImg/{id}", method = RequestMethod.GET)
    public void getHighlightImg(@PathVariable String id, HttpServletResponse response){
        InputStream inputStream = null;
        ProcessInstance pi = null;
        String picName = "";
        // 查询历史
        HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery().processInstanceId(id).singleResult();
        if (hpi.getEndTime() != null) {
            // 已经结束流程获取原图
            ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionId(hpi.getProcessDefinitionId()).singleResult();
            picName = pd.getDiagramResourceName();
            inputStream = repositoryService.getResourceAsStream(pd.getDeploymentId(), pd.getDiagramResourceName());
        } else {
            pi = runtimeService.createProcessInstanceQuery().processInstanceId(id).singleResult();
            BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());

            List<String> highLightedActivities = new ArrayList<String>();
            // 高亮任务节点
            List<Task> tasks = taskService.createTaskQuery().processInstanceId(id).list();
            for (Task task : tasks) {
                highLightedActivities.add(task.getTaskDefinitionKey());
            }
            List<String> highLightedFlows = new ArrayList<String>();
            ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
            //"宋体"
            inputStream = diagramGenerator.generateDiagram(bpmnModel, "png", highLightedActivities, highLightedFlows,
                    "宋体", "宋体", "宋体",null, 1.0);
            picName = pi.getName()+".png";
        }
        try {
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(picName, "UTF-8"));
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(b, 0, 1024)) != -1) {
                response.getOutputStream().write(b, 0, len);
            }
            response.flushBuffer();
        } catch (IOException e) {
            log.error(e.toString());
            throw new JeecgBootException("读取流程图片失败");
        }
    }

总结

上面只是展示了部分代码和部分效果图,对于一个正常的审批流转业务来说,提交审批,申请人撤销,重新申请,审批人驳回,委托他人审批,会签,流程挂起,流程实时跟踪,并行网关,排他网关,监听,待办,已办消息通知等都是必须要的,我这就不一一展现了,如有需要,可以+q:2500564056。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐