package com.example.testdingding.component;
import com.alibaba.fastjson.JSONObject;
import com.example.testdingding.client.FyWebClient;
import com.example.testdingding.util.DingTalkParams;
import com.example.testdingding.util.DingTalkUrlConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.*;
@Component
public class DingShdwUtils {
@Autowired
private DingTalkParams dingTalkParams;
@Autowired
FyWebClient fyWebClient;
private static Logger log = LoggerFactory.getLogger(DingShdwUtils.class);
public void sendMarkdownMsg(String userIdList,String title,String markdown){
HashMap<String, Object> urlParams = new HashMap<>();
urlParams.put("access_token",checkToken());
Map<String, Object> body = new HashMap<>();
body.put("agent_id",dingTalkParams.getAgentId());
body.put("userid_list",userIdList);
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","markdown");
JSONObject markdownJson = new JSONObject();
markdownJson.put("title",title);
markdownJson.put("text",markdown);
jsonObject.put("markdown",markdownJson);
body.put("msg",jsonObject);
System.out.println(body.toString());
String s = fyWebClient.sendPostByParamsByJson(DingTalkUrlConstants.URL_SEND_WORK_NOTICE, urlParams, body);
System.out.println(JSONObject.parseObject(s));
}
public void sendMarkdownMsgByDeptId(String deptIdList,String title,String markdown){
HashMap<String, Object> urlParams = new HashMap<>();
urlParams.put("access_token",checkToken());
Map<String, Object> body = new HashMap<>();
body.put("agent_id",dingTalkParams.getAgentId());
body.put("dept_id_list",deptIdList);
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","markdown");
JSONObject markdownJson = new JSONObject();
markdownJson.put("title",title);
markdownJson.put("text",markdown);
jsonObject.put("markdown",markdownJson);
body.put("msg",jsonObject);
System.out.println(body.toString());
String s = fyWebClient.sendPostByParamsByJson(DingTalkUrlConstants.URL_SEND_WORK_NOTICE, urlParams, body);
System.out.println(JSONObject.parseObject(s));
}
public void sendMarkdownMsgByUserId(String deptIdList,String title,String markdown){
HashMap<String, Object> urlParams = new HashMap<>();
urlParams.put("access_token",checkToken());
Map<String, Object> body = new HashMap<>();
body.put("agent_id",dingTalkParams.getAgentId());
body.put("userid_list",deptIdList);
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","markdown");
JSONObject markdownJson = new JSONObject();
markdownJson.put("title",title);
markdownJson.put("text",markdown);
jsonObject.put("markdown",markdownJson);
body.put("msg",jsonObject);
System.out.println(body.toString());
String s = fyWebClient.sendPostByParamsByJson(DingTalkUrlConstants.URL_SEND_WORK_NOTICE, urlParams, body);
System.out.println(JSONObject.parseObject(s));
}
public void sendCardMsgByUserId(String userIdList,String title,String markdown,String url){
HashMap<String, Object> urlParams = new HashMap<>();
urlParams.put("access_token",checkToken());
Map<String, Object> body = new HashMap<>();
body.put("agent_id",dingTalkParams.getAgentId());
body.put("userid_list",userIdList);
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","action_card");
JSONObject actionCard = new JSONObject();
actionCard.put("title",title);
actionCard.put("markdown",markdown);
actionCard.put("btn_orientation",1);
JSONObject btnJsonList = new JSONObject();
btnJsonList.put("title","去巡查");
btnJsonList.put("action_url",url);
actionCard.put("btn_json_list",btnJsonList);
jsonObject.put("action_card",actionCard);
body.put("msg",jsonObject);
System.out.println(body.toString());
String s = fyWebClient.sendPostByParamsByJson(DingTalkUrlConstants.URL_SEND_WORK_NOTICE, urlParams, body);
System.out.println(JSONObject.parseObject(s));
}
public void sendCardMsgByDeptId(String deptIdList,String title,String markdown,String url,String btnTitle){
HashMap<String, Object> urlParams = new HashMap<>();
urlParams.put("access_token",checkToken());
Map<String, Object> body = new HashMap<>();
body.put("agent_id",dingTalkParams.getAgentId());
body.put("dept_id_list",deptIdList);
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","action_card");
JSONObject actionCard = new JSONObject();
actionCard.put("title",title);
actionCard.put("markdown",markdown);
actionCard.put("btn_orientation",1);
JSONObject btnJsonList = new JSONObject();
btnJsonList.put("title",btnTitle);
btnJsonList.put("action_url",url);
actionCard.put("btn_json_list",btnJsonList);
jsonObject.put("action_card",actionCard);
body.put("msg",jsonObject);
System.out.println(body.toString());
String s = fyWebClient.sendPostByParamsByJson(DingTalkUrlConstants.URL_SEND_WORK_NOTICE, urlParams, body);
System.out.println(JSONObject.parseObject(s));
}
public void sendZTCardMsgByDeptId(String deptIdList,String title,String markdown,String url,String btnTitle){
HashMap<String, Object> urlParams = new HashMap<>();
urlParams.put("access_token",checkToken());
Map<String, Object> body = new HashMap<>();
body.put("agent_id",dingTalkParams.getAgentId());
body.put("dept_id_list",deptIdList);
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","action_card");
JSONObject actionCard = new JSONObject();
actionCard.put("markdown",markdown);
actionCard.put("title",title);
actionCard.put("single_title",btnTitle);
actionCard.put("single_url",url);
// actionCard.put("btn_orientation",1);
// JSONObject btnJsonList = new JSONObject();
// btnJsonList.put("title",btnTitle);
// btnJsonList.put("action_url",url);
// actionCard.put("btn_json_list",btnJsonList);
jsonObject.put("action_card",actionCard);
body.put("msg",jsonObject);
System.out.println(body.toString());
String s = fyWebClient.sendPostByParamsByJson(DingTalkUrlConstants.URL_SEND_WORK_NOTICE, urlParams, body);
System.out.println(JSONObject.parseObject(s));
}
/**
* 获取指定用户的详细信息
*/
public JSONObject getDingDingUser(String userId){
String token = checkToken();
Map<String, Object> uriParams = new HashMap<>();
uriParams.put("access_token",token);
Map<String, Object> bodyJson = new HashMap<>();
bodyJson.put("userid",userId);
String s = fyWebClient.sendPostByParamsByJson(DingTalkUrlConstants.URL_USERINFO_GET, uriParams, bodyJson);
return JSONObject.parseObject(s);
}
/**
* 获取部门用户详情
*/
public JSONObject getDingDingUserList(Long deptId){
String token = checkToken();
Map<String, Object> uriParams = new HashMap<>();
uriParams.put("access_token",token);
Map<String, Object> bodyJson = new HashMap<>();
bodyJson.put("dept_id",deptId);
bodyJson.put("cursor",0);
bo
评论10