• 创建钉钉审批流实例


    1、依赖

    
    
       com.aliyun
       dingtalk
       2.0.14
    
    
    
       com.dingtalk.open
       app-stream-client
       1.3.2
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2、参数

    @Configuration
    @ConfigurationProperties(prefix = InterfaceProperties.PREFIX)
    @Setter
    @Getter
    public class InterfaceProperties {
        public static final String PREFIX = "interface";
        private String dingDingEnable;
        private String appKey;
        private String appSecret;
        private Long agentId;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3、获取Token

    public String getToken() {
            try {
                com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(DingDingImpl.createConfig());
                com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
                        .setAppKey(interfaceProperties.getAppKey())
                        .setAppSecret(interfaceProperties.getAppSecret());
                com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse res = client.getAccessToken(getAccessTokenRequest);
                com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponseBody body = res.body;
                return body.getAccessToken();
            } catch (TeaException err) {
                log.error(err.code + err.message);
            } catch (Exception e) {
                log.error("获取token异常:", e);
                TeaException err = new TeaException(e.getMessage(), e);
                log.error(err.code + err.message);
            }
            return null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4、发起钉钉流程返回实例id

    public String sendData(String originatorUserId, String processCode, Long deptId, List param) {
            String token = this.getToken();
            if (null == token) {
                throw new BusinessException("接口Token获取失败");
            }
           List formComponentValues = paramToEntity(param);
            try {
                com.aliyun.dingtalkworkflow_1_0.Client client = new com.aliyun.dingtalkworkflow_1_0.Client(DingDingImpl.createConfig());
                com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceHeaders startProcessInstanceHeaders = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceHeaders();
                startProcessInstanceHeaders.xAcsDingtalkAccessToken = token;
                StartProcessInstanceRequest startProcessInstanceRequest = new StartProcessInstanceRequest()
                        //应用标识AgentId
                        .setMicroappAgentId(interfaceProperties.getAgentId())
                        //审批发起人的userId 必填
                        .setOriginatorUserId(originatorUserId)
                        //审批流的唯一码。process_code在审批模板编辑页面的URL中获取 必填
                        .setProcessCode(processCode)
                        //审批发起人所在的部门ID
                        .setDeptId(deptId)
                        //不使用审批流模板时,直接指定的审批人列表,最大列表长度:20
                        .setApprovers(null)
                        //使用审批流模板时,流程预测结果中节点规则上必填的自选操作人列表,最大列表长度:20
                        .setTargetSelectActioners(null)
                        //表单数据内容,控件列表,最大列表长度:150 必填
                        .setFormComponentValues(formComponentValues);
                StartProcessInstanceResponse response = client.startProcessInstanceWithOptions(startProcessInstanceRequest, startProcessInstanceHeaders, new RuntimeOptions());
                StartProcessInstanceResponseBody body = response.body;
                responseContent = body.instanceId;
                return body.instanceId;
            } catch (TeaException err) {
                log.error(err.code + err.message);
            } catch (Exception e) {
                log.error("发起钉钉流程异常:", e);
                TeaException err = new TeaException(e.getMessage(), e);
                log.error(err.code + err.message);
            } 
            throw new BusinessException("钉钉审批实例创建异常");
        }
    
    @Data
    @EqualsAndHashCode
    public class FormComponentValuesDto {
        @ApiModelProperty(value = "控件名称")
        @NotBlank
        public String name;
    
        @ApiModelProperty(value = "控件值")
        @NotBlank
        public String value;
    
        @ApiModelProperty(value = "控件类型")
        public String componentType;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    5、钉钉事件订阅–OA审批

    @Slf4j
    @Component
    public class DingDingEventListener implements InitializingBean {
    
        @Resource
        private InterfaceProperties interfaceProperties;
        @Resource
        private DingDingInterface dingDingInterface;
    
        @Override
        public void afterPropertiesSet() {
            try {
                OpenDingTalkStreamClientBuilder
                        .custom()
                        .credential(new AuthClientCredential(interfaceProperties.getAppKey(), interfaceProperties.getAppSecret()))
                        //注册事件监听
                        .registerAllEventListener(new GenericEventListener() {
                            public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                                try {
                                    if ("bpms_task_change".equals(event.getEventType())) {
                                        //获取事件体
                                        JSONObject bizData = event.getData();
                                        //处理事件
                                        log.info(event.getEventType() + bizData.toString());
                                         //保存审批记录
                                        .....
                                    }
                                    //审批实例开始,结束
                                    if ("bpms_instance_change".equals(event.getEventType())) {
                                        //获取事件体
                                        JSONObject bizData = event.getData();
                                        //处理事件
                                        log.info(event.getEventType() + bizData.toString());
                                        //保存审批记录
                                        .....
                                    }
                                    return EventAckStatus.SUCCESS;
                                } catch (Exception e) {
                                    log.error("钉钉订阅事件保存异常", e);
                                    //消费失败
                                    return EventAckStatus.LATER;
                                }
                            }
                        })
                        .build().start();
            } catch (Exception e) {
                log.error("钉钉事件订阅异常", e);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    6、参数获取

    1、审批模板唯一ID

    在这里插入图片描述### 2、AppKey 、AppSecret
    在这里插入图片描述

  • 相关阅读:
    java开发岗位面试
    torch.multiprocesssing
    python牛客网刷题查漏补缺1
    jQuery选择器方法总结
    Intent-Centric是Account Abstraction的新瓶装旧酒还是进化的最优路径?
    window 自启动程序并定时检测进程(SpringBoot 项目)
    大数据采集技术与预处理学习一:大数据概念、数据预处理、网络数据采集
    利用百度API进行视频翻译制作
    支付宝常用接口统一封装,可直接支付参数使用(适用于H5、PC、APP)
    服务器Centos7 静默安装Oracle Database 12.2
  • 原文地址:https://blog.csdn.net/weixin_40764017/article/details/137934774