package net.chenlin.dp.modules.api.controller; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.*; import com.dingtalk.api.response.*; import com.taobao.api.ApiException; import net.chenlin.dp.common.openapi4j.util.PropUtil; import net.chenlin.dp.common.utils.DateUtils; import net.chenlin.dp.common.utils.JSONUtils; import org.springframework.boot.configurationprocessor.json.JSONException; import org.springframework.boot.configurationprocessor.json.JSONObject; import java.util.*; /** * 此类用于钉钉公开接口方法调用 */ public class DingdingOpenInterface { /** * 企业内部应用的access_token,每20分钟改变一次 */ public static String gettoken = ""; /** * 获取钉钉企业内部应用的access_token */ public static void getToken(){ try { Properties prop = PropUtil.getProperties("/config.properties"); String appkey = prop.getProperty("ding_appkey"); String appsecret = prop.getProperty("ding_appsecret"); DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken"); OapiGettokenRequest req = new OapiGettokenRequest(); req.setHttpMethod("GET"); req.setAppkey(appkey); req.setAppsecret(appsecret); OapiGettokenResponse rsp = client.execute(req); Map map; map = toMap(rsp.getBody()); if(map.get("errcode").equals("0")){ gettoken = map.get("access_token"); }else{ gettoken = "error"; } } catch (ApiException | JSONException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取审批实例ID列表 * 备用金process_code:PROC-9331671D-265D-4A79-A6F2-93326D727673(备用金列表获取) * 销售订单process_code:PROC-40356B7F-6825-4DC3-83B9-8AD6D0D2CA49(销售订单列表获取) * 钉钉接口仅支持120天内数据 */ public List getOrderIdList(String processCode){ getToken();//获取token Map map = new HashMap<>(); List list = new ArrayList<>(); try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids"); OapiProcessinstanceListidsRequest req = new OapiProcessinstanceListidsRequest(); req.setProcessCode(processCode); Calendar nowTime = Calendar.getInstance(); nowTime.add(Calendar.DAY_OF_YEAR, -1); Date date = nowTime.getTime(); req.setStartTime(Long.valueOf(date.getTime()));//时间戳:例1586448000000 OapiProcessinstanceListidsResponse rsp = client.execute(req, gettoken);//access_token map = JSONUtils.jsonToMap(rsp.getBody()); if(map.get("errcode").equals(0)){ Map mapList = (Map) map.get("result"); list = (List) mapList.get("list"); } } catch (ApiException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return list; } /** * 获取审批实例详 */ public Map getOrderDetails(String processInstanceId){ Map map = new HashMap<>(); try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get"); OapiProcessinstanceGetRequest req = new OapiProcessinstanceGetRequest(); req.setProcessInstanceId(processInstanceId);//实例id OapiProcessinstanceGetResponse rsp = client.execute(req, gettoken);//access_token map = JSONUtils.jsonToMap(rsp.getBody()); if(map.get("errcode").equals(0)){ Map map1 = (Map) map.get("process_instance"); return map1; } } catch (ApiException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return map; } /** * 接口表格数据转换成可使用的map数据 * @param map1 * @return */ public static Map tableToMap(Map map1){ List> listMap = (List>) map1.get("form_component_values"); Map newMap = new HashMap<>(); for(Map o:listMap){ newMap.put((String) o.get("name"),o.get("value")); } return newMap; } public static void main(String[] args) { // for (Map.Entry entry : JSONUtils.jsonToMap(rsp.getBody()).entrySet()) { // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); // } Calendar nowTime = Calendar.getInstance(); // nowTime.add(Calendar.MINUTE, 5); nowTime.add(Calendar.DAY_OF_YEAR, -1); Date date = nowTime.getTime(); System.out.println(date.getTime()); System.out.println(nowTime.getTime()); } public static Map toMap(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Map result = new HashMap(); Iterator iterator = jsonObject.keys(); String key = null; String value = null; while (iterator.hasNext()) { key = (String) iterator.next(); value = jsonObject.getString(key); result.put(key, value); } return result; } }