java提交钉钉审批的一个流程例子

这个例子通过自己定义流程,根据钉钉定义的流程表单来实现

首先需要进行钉钉api流程接口实现

package com.nbcio.modules.estar.controller;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.nbcio.modules.estar.config.Constant;
import com.nbcio.modules.estar.config.URLConstant;
import com.nbcio.modules.estar.model.ProcessInstanceInputVO;
import com.nbcio.modules.estar.util.AccessTokenUtil;
import com.nbcio.modules.estar.util.LogFormatter;
import com.nbcio.modules.estar.util.ServiceResult;
import com.nbcio.modules.estar.util.ServiceResultCode;
import com.nbcio.modules.estar.util.LogFormatter.LogEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 企业 E应用审批解决方案示例代码
 * 实现了审批的基础功能
 * 内网穿透工具地址:https://open-doc.dingtalk.com/microapp/debug/ucof2g
 * 待办接口地址:https://open-doc.dingtalk.com/microapp/serverapi2/gdzay4
 */
@RestController
public class WorkRecordController {
    private static final Logger bizLogger = LoggerFactory.getLogger(WorkRecordController.class);
    private volatile Map localCache  = new HashMap();

    /**
     * 发起待办事项
     * 文档地址:https://open-doc.dingtalk.com/microapp/serverapi2/gdzay4
     */
    @RequestMapping(value = "/workrecord/start", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult startWorkRecord() {
        try {
            String userId = "manager7078";
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/add");
            OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest();
            req.setUserid(userId);
            req.setCreateTime(System.currentTimeMillis());
            req.setTitle("title");

            String id = userId + System.currentTimeMillis();
            req.setUrl(Constant.CALLBACK_URL_HOST + "/workrecord/" + id + "/update");
            List list2 = new ArrayList<>();
            OapiWorkrecordAddRequest.FormItemVo obj3 = new OapiWorkrecordAddRequest.FormItemVo();
            list2.add(obj3);
            obj3.setTitle("标题");
            obj3.setContent("内容");

            OapiWorkrecordAddRequest.FormItemVo obj4 = new OapiWorkrecordAddRequest.FormItemVo();
            list2.add(obj4);
            obj4.setTitle("发起时间");
            obj4.setContent(String.valueOf(System.currentTimeMillis()));
            req.setFormItemList(list2);
            OapiWorkrecordAddResponse rsp = client.execute(req, AccessTokenUtil.getToken());
            System.out.println(JSON.toJSONString(rsp));

            if (rsp.getErrcode().longValue() != 0) {
                return ServiceResult.failure(String.valueOf(rsp.getErrorCode()), rsp.getErrmsg());
            }
            localCache.put(id, rsp.getRecordId());
            return ServiceResult.success(rsp.getRecordId());

        } catch (Exception e) {
            String errLog = LogFormatter.getKVLogData(LogEvent.END,"startWorkRecord fail");
            bizLogger.info(errLog,e);
            return ServiceResult.failure(ServiceResultCode.SYS_ERROR.getErrCode(),ServiceResultCode.SYS_ERROR.getErrMsg());
        }
    }

    /**
     * 发起待办事项
     * 文档地址: https://open-doc.dingtalk.com/microapp/serverapi2/sltmwf
     */
    @RequestMapping(value = "/workrecord/{id}/update", method = RequestMethod.GET)
    @ResponseBody
    public String updateWorkRecord(@PathVariable String id) {
        try {
            if (!localCache.containsKey(id)) {
                return "can't find the workrecord";
            }
            String recordId = localCache.get(id);

            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/update");
            OapiWorkrecordUpdateRequest req = new OapiWorkrecordUpdateRequest();
            req.setUserid("manager7078");
            req.setRecordId(recordId);
            OapiWorkrecordUpdateResponse response = client.execute(req, AccessTokenUtil.getToken());
            System.out.println(JSON.toJSONString(response));

            if (response.getErrcode().longValue() != 0) {
                return "update workrecord fail";
            }
            return "待办事项更新完成";

        } catch (Exception e) {
            String errLog = LogFormatter.getKVLogData(LogEvent.END,"updateWorkRecord fail");
            bizLogger.info(errLog,e);
            return "update workrecord fail";
        }
    }

    /**
     * 获取待办事项
     * 文档地址:https://open-doc.dingtalk.com/microapp/serverapi2/ehn6bt
     * @return
     */
    @RequestMapping(value = "/workrecord/get", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getWorkRecordByUserId() {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/getbyuserid");
            OapiWorkrecordGetbyuseridRequest req = new OapiWorkrecordGetbyuseridRequest();
            req.setUserid("manager7078");
            req.setOffset(0L);
            req.setLimit(50L);
            req.setStatus(0L);
            OapiWorkrecordGetbyuseridResponse rsp = client.execute(req, AccessTokenUtil.getToken());
            System.out.println(rsp.getBody());

            return ServiceResult.success(rsp.getRecords());
        } catch (Exception e) {
            return ServiceResult.failure(ServiceResultCode.SYS_ERROR.getErrCode(),ServiceResultCode.SYS_ERROR.getErrMsg());
        }
    }
}

定义好流程

java提交钉钉审批的一个流程例子_第1张图片

 进行审批单定义

java提交钉钉审批的一个流程例子_第2张图片

java提交钉钉审批的一个流程例子_第3张图片

提交钉钉审批

java提交钉钉审批的一个流程例子_第4张图片

手机端收到的信息

 java提交钉钉审批的一个流程例子_第5张图片

你可能感兴趣的:(java开发,java,开发语言,钉钉)