钉钉审批回调流程,曾经踩过的坑,如果是内网需要配置内网穿透才能回调到本地

首先说明一点:之前碰到钉钉审批回调接口的时候发现钉钉会瞬间调用2次具体什么原因不清楚。

我的解决办法会在下面说:

直接上代码:

     首先如果是内网,需要内网穿透一下把钉钉所有审批全部设置到本地,不然没办法本地DEBUG调试,(酌情考虑什么时候测试,不然同时进来的审批不是你自己的,是别人请假的或者是其他的审批流程。

【具体对不对我是这样理解的。。。如有误导请谅解】)

    内网穿透 链接:https://pan.baidu.com/s/174Xe1UAZYx-K5yhxTitzHg 
    提取码:2hc4 
    复制这段内容后打开百度网盘手机App,操作更方便哦

开启之后,切地址到本地

package com.jeecg.sapoading.controller;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiCallBackUpdateCallBackRequest;
import com.dingtalk.api.response.OapiCallBackUpdateCallBackResponse;
import com.jeecg.sapoading.entity.TDingOaSsqEntity;
import org.jeecgframework.core.annotation.JAuth;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.enums.Permission;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Controller
@RequestMapping("/tDi***UpdateCallBack")
@JAuth(auth= Permission.SKIP_AUTH)
public class TDingOAUpdateCallBack {
    @RequestMapping(params = "updateCallBack")
    @ResponseBody
    public AjaxJson updateCallBack(HttpServletRequest request) {
        String message = null;
        AjaxJson j = new AjaxJson();
        message = "OA更新回调成功";
        try{
            //需要开内网穿透
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/update_call_back");
                OapiCallBackUpdateCallBackRequest requestOa = new OapiCallBackUpdateCallBackRequest();
                requestOa.setUrl("http://**.**.**.**:8090/项目名/***BfController.do?insertBfFromDingding");
                
             //本地穿透地址requestOa.setUrl("http://cfb.vaiwan.com/tAPersonassetBfController.do?insertBfFromDingding");
                requestOa.setAesKey("WyGtmhbKapmTYjajodepyj86boeQctWrDP7YqyTTCUf");
    //key这个是一个随机字符串
                requestOa.setToken("123456");
   //这个好像就是123456
                requestOa.setCallBackTag(Arrays.asList("bpms_instance_change", "bpms_task_change"));
                //   OapiCallBackUpdateCallBackResponse response = client.execute(request,"7bc30097ee5d33b291ef96205dd216ee");
                OapiCallBackUpdateCallBackResponse response = client.execute(requestOa,"7bc30097ee5d33b291ef96205dd216ee");
                response.getErrcode();
                 System.out.println("code:"+response.getErrmsg());

        }catch(Exception e){
            e.printStackTrace();
            message = "OA更新回调失败";
            throw new BusinessException(e.getMessage());
        }
        j.setMsg(message);
        return j;
    }

}

  直接上切换代码

@RequestMapping(params = "insertBfFromDingding")
	@ResponseBody
	public Map insertBfFromDingding(@RequestParam(value = "signature", required = false) String signature,
													@RequestParam(value = "timestamp", required = false) String timestamp,
													@RequestParam(value = "nonce", required = false) String nonce,
													@RequestBody(required = false) JSONObject json,OapiCallBackRegisterCallBackRequest req ) throws DingTalkEncryptException {
 		DingTalkEncryptor dingTalkEncryptor = null;
 		List dataList = new ArrayList<>();
		Map headerMap = req.getHeaderMap();
		try {
			dingTalkEncryptor = new DingTalkEncryptor("123456", "WyGtmhbKapmTYja******rDP7YqyTTCUf",
					"dinge3******");

			//从post请求的body中获取回调信息的加密数据进行解密处理
			String encryptMsg = json.getString("encrypt");
			String plainText = dingTalkEncryptor.getDecryptMsg(signature, timestamp, nonce, encryptMsg);
			JSONObject obj = JSON.parseObject(plainText);

			//根据回调数据类型做不同的业务处理
			String eventType = obj.getString("EventType");

			String result = obj.getString("result");
			String type = obj.getString("type");
			if(result!=null && 审批PROCESS_CODE找钉钉配置审批模板的人要.equals(obj.getString("processCode"))){ //大致就是PROC-4361D700-D650---******
				TDingOaInfoEntity oaInfoEntity = new TDingOaInfoEntity();

				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
				if("finish".equals(type) && "agree".equals(result)&& "bpms_instance_change".equals(eventType)){ //审批通过
					System.out.println("回调eventType:=="+eventType);
					//获取审批任务返回详情,并进行解析 z组装数据
					System.out.println("-------------OA审批通过------开始------------");
					DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
					OapiProcessinstanceGetRequest request = new OapiProcessinstanceGetRequest();
					request.setProcessInstanceId(obj.getString("processInstanceId"));
					OapiProcessinstanceGetResponse response = client.execute(request,OaCustomerAccessTokenThread.accessToken);
					String body = response.getBody();
					JSONObject jsonBody = JSONObject.parseObject(body);
					System.out.println("jsonBody:::::"+jsonBody);
					//第一层解析
					String process_instance = jsonBody.getString("process_instance");
					System.out.println("process_instance::::"+process_instance);
					//第二层解析
					JSONObject jsonProcessInstance = JSONObject.parseObject(process_instance);
					String businessId = jsonProcessInstance.getString("business_id");
					System.out.println("审批编号-businessId ="+businessId);
					String originator_dept_id = jsonProcessInstance.getString("originator_dept_id");
					System.out.println("部门ID ="+originator_dept_id);
					String create_time = jsonProcessInstance.getString("create_time");
					System.out.println("创建时间 ="+create_time);
					oaInfoEntity.setCreateDate(new Date());
					String finish_time = jsonProcessInstance.getString("finish_time");
					System.out.println("审批完成时间 ="+finish_time);
					oaInfoEntity.setUpdateDate(new Date());
					String title = jsonProcessInstance.getString("title");
					System.out.println("标题 ="+title);
					oaInfoEntity.setTitle(title);
					String originator_dept_name = jsonProcessInstance.getString("originator_dept_name");
					System.out.println("部门名称 ="+originator_dept_name);
					oaInfoEntity.setApplicantDepartment(originator_dept_name);
					String originator_userid = jsonProcessInstance.getString("originator_userid");
					System.out.println("userID ="+originator_userid);
					oaInfoEntity.setApplyUserId(originator_userid);
					if("COMPLETED".equals(jsonProcessInstance.getString("status"))){
						String formValue = jsonProcessInstance.getString("form_component_values");
						//第三次解析
						JSONArray OaFormValueArry = JSONArray.parseArray(formValue);
//						for(int i=0;i

 

你可能感兴趣的:(钉钉审批回调流程,曾经踩过的坑,如果是内网需要配置内网穿透才能回调到本地)