1、依赖
<!--钉钉 api -->
<dependency><groupId>com.aliyun</groupId><artifactId>dingtalk</artifactId><version>2.0.14</version>
</dependency>
<!--钉钉 事件订阅-->
<dependency><groupId>com.dingtalk.open</groupId><artifactId>app-stream-client</artifactId><version>1.3.2</version>
</dependency>
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;
}
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;}
4、发起钉钉流程返回实例id
public String sendData(String originatorUserId, String processCode, Long deptId, List<FormComponentValuesDto> param) {String token = this.getToken();if (null == token) {throw new BusinessException("接口Token获取失败");}List<StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues> 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 = "控件名称")@NotBlankpublic String name;@ApiModelProperty(value = "控件值")@NotBlankpublic String value;@ApiModelProperty(value = "控件类型")public String componentType;
}
5、钉钉事件订阅–OA审批
@Slf4j
@Component
public class DingDingEventListener implements InitializingBean {@Resourceprivate InterfaceProperties interfaceProperties;@Resourceprivate DingDingInterface dingDingInterface;@Overridepublic 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);}}
}
6、参数获取
1、审批模板唯一ID
### 2、AppKey 、AppSecret